X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs2.git;a=blobdiff_plain;f=direct%2Fyaffsfs.c;h=753bf4fa63c5d14c5dab03931b8949b53840bbb5;hp=15d5aeb7b0ed35911f3edb8ea12fc090636981a6;hb=86e364669f775e70f32204769217ecf035508c1d;hpb=d84af1d4633120e3dea173c7f91aa8cfc3421706 diff --git a/direct/yaffsfs.c b/direct/yaffsfs.c index 15d5aeb..753bf4f 100644 --- a/direct/yaffsfs.c +++ b/direct/yaffsfs.c @@ -413,6 +413,22 @@ static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPat return retval; } +static int yaffsfs_CheckPath(const YCHAR *path) +{ + int n=0; + int divs=0; + while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){ + if(yaffsfs_IsPathDivider(*path)){ + n=0; + divs++; + } else + n++; + path++; + } + + return (*path) ? -1 : 0; +} + /* FindMountPoint only returns a dev entry if the path is a mount point */ static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path) { @@ -609,6 +625,11 @@ int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing) u8 readRequested; u8 writeRequested; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + /* O_EXCL only has meaning if O_CREAT is specified */ if(!(oflag & O_CREAT)) oflag &= ~(O_EXCL); @@ -878,6 +899,9 @@ int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int off /* Not a reading handle */ yaffsfs_SetError(-EINVAL); totalRead = -1; + } else if(nbyte > YAFFS_MAX_FILE_SIZE){ + yaffsfs_SetError(-EINVAL); + totalRead = -1; } else { if(isPread) startPos = offset; @@ -1063,6 +1087,11 @@ int yaffs_truncate(const YCHAR *path,off_t new_size) struct yaffs_obj *dir = NULL; int result = YAFFS_FAIL; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,path,0,1,&dir); @@ -1122,10 +1151,11 @@ off_t yaffs_lseek(int fd, off_t offset, int whence) h = yaffsfs_GetHandlePointer(fd); obj = yaffsfs_GetHandleObject(fd); - if(!h || !obj){ - /* bad handle */ + if(!h || !obj) yaffsfs_SetError(-EBADF); - } else { + else if(offset > YAFFS_MAX_FILE_SIZE) + yaffsfs_SetError(-EINVAL); + else { if(whence == SEEK_SET){ if(offset >= 0) pos = offset; @@ -1157,6 +1187,11 @@ int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory) YCHAR *name; int result = YAFFS_FAIL; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,path,0,0,NULL); @@ -1187,11 +1222,6 @@ int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory) } -int yaffs_rmdir(const YCHAR *path) -{ - return yaffsfs_DoUnlink(path,1); -} - int yaffs_unlink(const YCHAR *path) { return yaffsfs_DoUnlink(path,0); @@ -1209,6 +1239,11 @@ int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath) yaffsfs_Lock(); + if(yaffsfs_CheckPath(newPath) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0); newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0); obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL); @@ -1302,6 +1337,11 @@ static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int d int retVal = -1; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,path,0,1,&dir); @@ -1363,6 +1403,11 @@ static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *da int retVal = -1; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,path,0,1,&dir); @@ -1431,6 +1476,11 @@ static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, in int retVal = -1; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,path,0,1,&dir); @@ -1497,6 +1547,11 @@ static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follo int retVal = -1; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,path,0,1,&dir); @@ -1563,6 +1618,11 @@ static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow) int retVal = -1; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,path,0,1, &dir); @@ -1724,7 +1784,12 @@ int yaffs_access(const YCHAR *path, int amode) struct yaffs_obj *obj=NULL; struct yaffs_obj *dir=NULL; - int retval = 0; + int retval = -1; + + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } if(amode & ~(R_OK | W_OK | X_OK)){ yaffsfs_SetError(-EINVAL); @@ -1749,10 +1814,10 @@ int yaffs_access(const YCHAR *path, int amode) if((amode & X_OK) && !(obj->yst_mode & S_IEXEC)) access_ok = 0; - if(!access_ok) { + if(!access_ok) yaffsfs_SetError(-EACCES); - retval = -1; - } + else + retval = 0; } yaffsfs_Unlock(); @@ -1768,6 +1833,11 @@ int yaffs_chmod(const YCHAR *path, mode_t mode) struct yaffs_obj *dir=NULL; int retVal = -1; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + if(mode & ~(0777)){ yaffsfs_SetError(-EINVAL); return -1; @@ -1819,22 +1889,17 @@ int yaffs_fchmod(int fd, mode_t mode) } -int yaffs_mkdir(const YCHAR *path, mode_t mode) +static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path) { - struct yaffs_obj *parent = NULL; - struct yaffs_obj *dir = NULL; - YCHAR *name; - YCHAR *use_path = NULL; - int path_length = 0; - int retVal= -1; + YCHAR *alt_path = NULL; + int path_length; int i; - /* * We don't have a definition for max path length. * We will use 3 * max name length instead. */ - + *ret_path = NULL; path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1); /* If the last character is a path divider, then we need to @@ -1845,50 +1910,89 @@ int yaffs_mkdir(const YCHAR *path, mode_t mode) */ if(path_length > 0 && yaffsfs_IsPathDivider(path[path_length-1])){ - use_path = YMALLOC(path_length + 1); - if(!use_path){ - yaffsfs_SetError(-ENOMEM); + alt_path = YMALLOC(path_length + 1); + if(!alt_path) return -1; - } - strcpy(use_path, path); + strcpy(alt_path, path); for(i = path_length-1; - i >= 0 && yaffsfs_IsPathDivider(use_path[i]); + i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--) - use_path[i] = (YCHAR) 0; - path = use_path; + alt_path[i] = (YCHAR) 0; + } + *ret_path = alt_path; + return 0; +} + +int yaffs_mkdir(const YCHAR *path, mode_t mode) +{ + struct yaffs_obj *parent = NULL; + struct yaffs_obj *dir = NULL; + YCHAR *name; + YCHAR *alt_path = NULL; + int retVal= -1; + + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + + if(yaffsfs_alt_dir_path(path, &alt_path) < 0){ + yaffsfs_SetError(-ENOMEM); + return -1; } + if(alt_path) + path = alt_path; yaffsfs_Lock(); parent = yaffsfs_FindDirectory(NULL,path,&name,0); - if(parent && yaffs_strnlen(name,5) == 0){ + if(!parent) + yaffsfs_SetError(-ENOTDIR); + else if(parent && yaffs_strnlen(name,5) == 0){ /* Trying to make the root itself */ yaffsfs_SetError(-EEXIST); - } else if(parent && parent->my_dev->read_only){ - yaffsfs_SetError(-EINVAL); - } else { - if(parent) - dir = yaffs_create_dir(parent,name,mode,0,0); + } else if(parent && parent->my_dev->read_only) + yaffsfs_SetError(-EROFS); + else { + dir = yaffs_create_dir(parent,name,mode,0,0); if(dir) retVal = 0; - else { - if(!parent) - yaffsfs_SetError(-ENOENT); /* missing path */ - else if (yaffs_find_by_name(parent,name)) - yaffsfs_SetError(-EEXIST); /* the name already exists */ - else - yaffsfs_SetError(-ENOSPC); /* just assume no space */ - retVal = -1; - } + else if (yaffs_find_by_name(parent,name)) + yaffsfs_SetError(-EEXIST); /* the name already exists */ + else + yaffsfs_SetError(-ENOSPC); /* just assume no space */ } yaffsfs_Unlock(); - if(use_path) - YFREE(use_path); + if(alt_path) + YFREE(alt_path); return retVal; } +int yaffs_rmdir(const YCHAR *path) +{ + int result; + YCHAR *alt_path; + + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + + if(yaffsfs_alt_dir_path(path, &alt_path) < 0){ + yaffsfs_SetError(-ENOMEM); + return -1; + } + if(alt_path) + path = alt_path; + result = yaffsfs_DoUnlink(path,1); + if(alt_path) + YFREE(alt_path); + return result; +} + + void * yaffs_getdev(const YCHAR *path) { struct yaffs_dev *dev=NULL; @@ -1905,6 +2009,11 @@ int yaffs_mount2(const YCHAR *path,int read_only) T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path)); + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); yaffsfs_InitHandles(); @@ -1942,6 +2051,11 @@ int yaffs_sync(const YCHAR *path) int retVal=-1; struct yaffs_dev *dev=NULL; YCHAR *dummy; + + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } yaffsfs_Lock(); dev = yaffsfs_FindDevice(path,&dummy); @@ -1971,6 +2085,11 @@ int yaffs_remount(const YCHAR *path, int force, int read_only) struct yaffs_dev *dev=NULL; yaffsfs_Handle *yh; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); dev = yaffsfs_FindMountPoint(path); if(dev){ @@ -2012,6 +2131,11 @@ int yaffs_unmount2(const YCHAR *path, int force) int retVal=-1; struct yaffs_dev *dev=NULL; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); dev = yaffsfs_FindMountPoint(path); if(dev){ @@ -2062,6 +2186,11 @@ loff_t yaffs_freespace(const YCHAR *path) struct yaffs_dev *dev=NULL; YCHAR *dummy; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); dev = yaffsfs_FindDevice(path,&dummy); if(dev && dev->is_mounted){ @@ -2081,10 +2210,16 @@ loff_t yaffs_totalspace(const YCHAR *path) struct yaffs_dev *dev=NULL; YCHAR *dummy; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); dev = yaffsfs_FindDevice(path,&dummy); if(dev && dev->is_mounted){ - retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks; + retVal = (dev->param.end_block - dev->param.start_block + 1) - + dev->param.n_reserved_blocks; retVal *= dev->param.chunks_per_block; retVal *= dev->data_bytes_per_chunk; @@ -2101,6 +2236,11 @@ int yaffs_inodecount(const YCHAR *path) struct yaffs_dev *dev=NULL; YCHAR *dummy; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); dev = yaffsfs_FindDevice(path,&dummy); if(dev && dev->is_mounted) { @@ -2231,6 +2371,11 @@ yaffs_DIR *yaffs_opendir(const YCHAR *dirname) struct yaffs_obj *obj = NULL; yaffsfs_DirectorySearchContext *dsc = NULL; + if(yaffsfs_CheckPath(dirname) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL); @@ -2330,10 +2475,18 @@ int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath) int retVal= -1; int mode = 0; /* ignore for now */ + if(yaffsfs_CheckPath(newpath) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } yaffsfs_Lock(); parent = yaffsfs_FindDirectory(NULL,newpath,&name,0); - if(parent && parent->my_dev->read_only) - yaffsfs_SetError(-EINVAL); + if(!parent) + yaffsfs_SetError(-ENOTDIR); + else if( strlen(name) < 1) + yaffsfs_SetError(-ENOENT); + else if(parent->my_dev->read_only) + yaffsfs_SetError(-EROFS); else if(parent){ obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath); if(obj) @@ -2342,9 +2495,6 @@ int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath) yaffsfs_SetError(-ENOSPC); /* just assume no space for now */ retVal = -1; } - } else { - yaffsfs_SetError(-EINVAL); - retVal = -1; } yaffsfs_Unlock(); @@ -2386,6 +2536,11 @@ int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath) struct yaffs_obj *lnk = NULL; int retVal = -1; + if(yaffsfs_CheckPath(linkpath) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + yaffsfs_Lock(); obj = yaffsfs_FindObject(NULL,oldpath,0,1,NULL); @@ -2433,6 +2588,8 @@ int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev) pathname=pathname; mode=mode; dev=dev; + + yaffsfs_SetError(-EINVAL); return -1; } @@ -2446,9 +2603,17 @@ int yaffs_n_handles(const YCHAR *path) { struct yaffs_obj *obj; + if(yaffsfs_CheckPath(path) < 0){ + yaffsfs_SetError(-ENAMETOOLONG); + return -1; + } + obj = yaffsfs_FindObject(NULL,path,0,1,NULL); - return yaffsfs_CountHandles(obj); + if(obj) + return yaffsfs_CountHandles(obj); + else + return -1; } int yaffs_get_error(void)