X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs2.git;a=blobdiff_plain;f=direct%2Fyaffsfs.c;h=d87bc8073f476fb5e7fe38c3dc276cb6fa79d59e;hp=ddbdcdb65354743c00bc02efc70e8111044f6119;hb=93ea07d9b83e324f12b0e7c1a16a8cef34a29802;hpb=b0173df3b2c8cc1f68d877a5a64e0f0ca93c0bd6 diff --git a/direct/yaffsfs.c b/direct/yaffsfs.c index ddbdcdb..d87bc80 100644 --- a/direct/yaffsfs.c +++ b/direct/yaffsfs.c @@ -30,13 +30,6 @@ #define YAFFSFS_RW_SHIFT (13) #define YAFFSFS_RW_SIZE (1<=0){ in = &yaffsfs_inode[ret]; if(!in->iObj) @@ -152,6 +165,17 @@ static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj) return ret; } + +static int yaffsfs_CountHandles(yaffs_Object *obj) +{ + int i = yaffsfs_FindInodeIdForObject(obj); + + if(i >= 0) + return yaffsfs_inode[i].count; + else + return 0; +} + static void yaffsfs_ReleaseInode(yaffsfs_Inode *in) { yaffs_Object *obj; @@ -253,7 +277,7 @@ int yaffsfs_Match(YCHAR a, YCHAR b) int yaffsfs_IsPathDivider(YCHAR ch) { - YCHAR *str = YAFFS_PATH_DIVIDERS; + const YCHAR *str = YAFFS_PATH_DIVIDERS; while(*str){ if(*str == ch) @@ -264,6 +288,10 @@ int yaffsfs_IsPathDivider(YCHAR ch) return 0; } + + +YLIST_HEAD(yaffsfs_deviceList); + /* * yaffsfs_FindDevice * yaffsfs_FindRoot @@ -273,10 +301,11 @@ int yaffsfs_IsPathDivider(YCHAR ch) */ static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath) { - yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList; + struct ylist_head *cfg; const YCHAR *leftOver; const YCHAR *p; yaffs_Device *retval = NULL; + yaffs_Device *dev = NULL; int thisMatchLength; int longestMatch = -1; int matching; @@ -286,9 +315,10 @@ static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath) * 1) Actually matches a prefix (ie /a amd /abc will not match * 2) Matches the longest. */ - while(cfg && cfg->prefix && cfg->dev){ + ylist_for_each(cfg, &yaffsfs_deviceList){ + dev = ylist_entry(cfg, yaffs_Device, devList); leftOver = path; - p = cfg->prefix; + p = dev->param.name; thisMatchLength = 0; matching = 1; @@ -319,16 +349,23 @@ static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath) /* Skip over any /s in leftOver */ while(yaffsfs_IsPathDivider(*leftOver)) leftOver++; - - if( matching && (thisMatchLength > longestMatch)){ - /* Matched prefix */ + // Skip over any /s in p + while(yaffsfs_IsPathDivider(*p)) + p++; + + // p should now be at the end of the string (ie. fully matched) + if(*p) + matching = 0; + + if( matching && (thisMatchLength > longestMatch)) + { + // Matched prefix *restOfPath = (YCHAR *)leftOver; - retval = cfg->dev; + retval = dev; longestMatch = thisMatchLength; } - cfg++; } return retval; } @@ -527,7 +564,7 @@ int yaffs_dup(int fd) } -int yaffs_open(const YCHAR *path, int oflag, int mode) +int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing) { yaffs_Object *obj = NULL; yaffs_Object *dir = NULL; @@ -537,6 +574,14 @@ int yaffs_open(const YCHAR *path, int oflag, int mode) int openDenied = 0; int symDepth = 0; int errorReported = 0; + __u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0; + __u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0; + __u8 sharedReadAllowed; + __u8 sharedWriteAllowed; + __u8 alreadyReading; + __u8 alreadyWriting; + __u8 readRequested; + __u8 writeRequested; /* O_EXCL only has meaning if O_CREAT is specified */ if(!(oflag & O_CREAT)) @@ -557,7 +602,6 @@ int yaffs_open(const YCHAR *path, int oflag, int mode) h = yaffsfs_GetHandlePointer(handle); - /* try to find the exisiting object */ obj = yaffsfs_FindObject(NULL,path,0); @@ -567,12 +611,21 @@ int yaffs_open(const YCHAR *path, int oflag, int mode) if(obj) obj = yaffs_GetEquivalentObject(obj); - if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE) + if(obj && + obj->variantType != YAFFS_OBJECT_TYPE_FILE && + obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) obj = NULL; if(obj){ - /* The file already exists */ + /* The file already exists or it might be a directory */ + + /* If it is a directory then we can't open it as a file */ + if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){ + openDenied = 1; + yaffsfs_SetError(-EISDIR); + errorReported = 1; + } /* Open should fail if O_CREAT and O_EXCL are specified since * the file exists @@ -596,6 +649,43 @@ int yaffs_open(const YCHAR *path, int oflag, int mode) !(obj->yst_mode & S_IWRITE)) openDenied = 1; + /* Check sharing of an existing object. */ + { + yaffsfs_Handle *h; + int i; + sharedReadAllowed = 1; + sharedWriteAllowed = 1; + alreadyReading = 0; + alreadyWriting = 0; + for( i = 0; i < YAFFSFS_N_HANDLES; i++){ + h = &yaffsfs_handle[i]; + if(h->useCount > 0 && + h->inodeId >= 0 && + yaffsfs_inode[h->inodeId].iObj == obj){ + if(!h->shareRead) + sharedReadAllowed = 0; + if(!h->shareWrite) + sharedWriteAllowed = 0; + if(h->reading) + alreadyReading = 1; + if(h->writing) + alreadyWriting = 0; + } + } + + readRequested = (oflag & (O_RDWR | O_RDONLY)) ? 1 : 0; + writeRequested = (oflag & (O_RDWR | O_WRONLY)) ? 1 : 0; + + if((!sharedReadAllowed && readRequested)|| + (!shareRead && alreadyReading) || + (!sharedWriteAllowed && writeRequested) || + (!shareWrite && alreadyWriting)){ + openDenied = 1; + yaffsfs_SetError(-EBUSY); + errorReported=1; + } + } + } else if((oflag & O_CREAT)) { /* Let's see if we can create this file */ dir = yaffsfs_FindDirectory(NULL,path,&name,0); @@ -621,14 +711,17 @@ int yaffs_open(const YCHAR *path, int oflag, int mode) } h->inodeId = inodeId; - h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1; + h->reading = (oflag & (O_RDONLY | O_RDWR)) ? 1 : 0; + h->writing = (oflag & (O_WRONLY | O_RDWR)) ? 1 : 0; h->append = (oflag & O_APPEND) ? 1 : 0; h->position = 0; + h->shareRead = shareRead; + h->shareWrite = shareWrite; /* Hook inode to object */ obj->myInode = (void*) &yaffsfs_inode[inodeId]; - if((oflag & O_TRUNC) && !h->readOnly) + if((oflag & O_TRUNC) && h->writing) yaffs_ResizeFile(obj,0); } else { yaffsfs_PutHandle(handle); @@ -645,6 +738,11 @@ int yaffs_open(const YCHAR *path, int oflag, int mode) return handle; } +int yaffs_open(const YCHAR *path, int oflag, int mode) +{ + return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE); +} + int yaffs_Dofsync(int fd,int datasync) { yaffsfs_Handle *h = NULL; @@ -822,14 +920,14 @@ int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, /* bad handle */ yaffsfs_SetError(-EBADF); totalWritten = -1; - } else if( h && obj && (h->readOnly || obj->myDev->readOnly)){ + } else if( h && obj && (!h->writing || obj->myDev->readOnly)){ yaffsfs_SetError(-EINVAL); totalWritten=-1; } else if( h && obj){ - if(isPwrite) - startPos = offset; if(h->append) startPos = yaffs_GetObjectFileLength(obj); + else if(isPwrite) + startPos = offset; else startPos = h->position; @@ -1091,7 +1189,7 @@ static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf) obj = yaffs_GetEquivalentObject(obj); if(obj && buf){ - buf->st_dev = (int)obj->myDev->context; + buf->st_dev = (int)obj->myDev->osContext; buf->st_ino = obj->objectId; buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */ @@ -1180,6 +1278,262 @@ int yaffs_fstat(int fd, struct yaffs_stat *buf) return retVal; } +#ifndef CONFIG_YAFFS_WINCE +/* xattrib functions */ + + +static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_FindObject(NULL,path,0); + + if(follow) + obj = yaffsfs_FollowLink(obj,0); + + if(obj) { + retVal = yaffs_SetXAttribute(obj,name,data,size,flags); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + } else + /* todo error not found */ + yaffsfs_SetError(-ENOENT); + + yaffsfs_Unlock(); + + return retVal; + +} + +int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags) +{ + return yaffs_do_setxattr(path, name, data, size, flags, 1); +} + +int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags) +{ + return yaffs_do_setxattr(path, name, data, size, flags, 0); +} + + + +int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_GetHandleObject(fd); + + if(obj) { + retVal = yaffs_SetXAttribute(obj,name,data,size,flags); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + } else + /* bad handle */ + yaffsfs_SetError(-EBADF); + + yaffsfs_Unlock(); + + return retVal; +} + +static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_FindObject(NULL,path,0); + + if(follow) + obj = yaffsfs_FollowLink(obj,0); + + if(obj) { + retVal = yaffs_GetXAttribute(obj,name,data,size); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + } else + /* todo error not found */ + yaffsfs_SetError(-ENOENT); + + yaffsfs_Unlock(); + + return retVal; + +} + +int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size) +{ + return yaffs_do_getxattr( path, name, data, size, 1); +} +int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size) +{ + return yaffs_do_getxattr( path, name, data, size, 0); +} + + + +int yaffs_fgetxattr(int fd, const char *name, void *data, int size) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_GetHandleObject(fd); + + if(obj) { + retVal = yaffs_GetXAttribute(obj,name,data,size); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + } else + /* bad handle */ + yaffsfs_SetError(-EBADF); + + yaffsfs_Unlock(); + + return retVal; +} + +static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_FindObject(NULL,path,0); + + if(follow) + obj = yaffsfs_FollowLink(obj,0); + + if(obj) { + retVal = yaffs_ListXAttributes(obj, data,size); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + } else + /* todo error not found */ + yaffsfs_SetError(-ENOENT); + + yaffsfs_Unlock(); + + return retVal; + +} + +int yaffs_listxattr(const YCHAR *path, char *data, int size) +{ + return yaffs_do_listxattr(path, data, size, 1); +} + +int yaffs_llistxattr(const YCHAR *path, char *data, int size) +{ + return yaffs_do_listxattr(path, data, size, 0); +} + +int yaffs_flistxattr(int fd, char *data, int size) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_GetHandleObject(fd); + + if(obj) { + retVal = yaffs_ListXAttributes(obj,data,size); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + } else + /* bad handle */ + yaffsfs_SetError(-EBADF); + + yaffsfs_Unlock(); + + return retVal; +} + +static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_FindObject(NULL,path,0); + + if(follow) + obj = yaffsfs_FollowLink(obj,0); + + if(obj) { + retVal = yaffs_RemoveXAttribute(obj,name); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + } else + /* todo error not found */ + yaffsfs_SetError(-ENOENT); + + yaffsfs_Unlock(); + + return retVal; + +} + +int yaffs_removexattr(const YCHAR *path, const char *name) +{ + return yaffs_do_removexattr(path, name, 1); +} + +int yaffs_lremovexattr(const YCHAR *path, const char *name) +{ + return yaffs_do_removexattr(path, name, 0); +} + +int yaffs_fremovexattr(int fd, const char *name) +{ + yaffs_Object *obj; + + int retVal = -1; + + yaffsfs_Lock(); + obj = yaffsfs_GetHandleObject(fd); + + if(obj){ + retVal = yaffs_RemoveXAttribute(obj,name); + if(retVal< 0){ + yaffsfs_SetError(retVal); + retVal = -1; + } + }else + /* bad handle */ + yaffsfs_SetError(-EBADF); + + yaffsfs_Unlock(); + + return retVal; +} +#endif + #ifdef CONFIG_YAFFS_WINCE int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime) { @@ -1366,7 +1720,10 @@ int yaffs_mkdir(const YCHAR *path, mode_t mode) yaffsfs_Lock(); parent = yaffsfs_FindDirectory(NULL,path,&name,0); - if(parent && parent->myDev->readOnly){ + if(parent && yaffs_strnlen(name,5) == 0){ + /* Trying to make the root itself */ + yaffsfs_SetError(-EEXIST); + } else if(parent && parent->myDev->readOnly){ yaffsfs_SetError(-EINVAL); } else { if(parent) @@ -1389,6 +1746,14 @@ int yaffs_mkdir(const YCHAR *path, mode_t mode) return retVal; } +void * yaffs_getdev(const YCHAR *path) +{ + yaffs_Device *dev=NULL; + YCHAR *dummy; + dev = yaffsfs_FindDevice(path,&dummy); + return (void *)dev; +} + int yaffs_mount2(const YCHAR *path,int readOnly) { int retVal=-1; @@ -1399,6 +1764,9 @@ int yaffs_mount2(const YCHAR *path,int readOnly) T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path)); yaffsfs_Lock(); + + yaffsfs_InitHandles(); + dev = yaffsfs_FindDevice(path,&dummy); if(dev){ if(!dev->isMounted){ @@ -1440,7 +1808,7 @@ int yaffs_sync(const YCHAR *path) yaffs_FlushEntireDeviceCache(dev); yaffs_CheckpointSave(dev); - + retVal = 0; } else /* todo error - not mounted. */ @@ -1591,7 +1959,7 @@ int yaffs_inodecount(const YCHAR *path) yaffsfs_Lock(); dev = yaffsfs_FindDevice(path,&dummy); if(dev && dev->isMounted) { - int nObjects = dev->nObjectsCreated - dev->nFreeObjects; + int nObjects = dev->nObjects; if(nObjects > dev->nHardLinks) retVal = nObjects - dev->nHardLinks; } @@ -1604,26 +1972,23 @@ int yaffs_inodecount(const YCHAR *path) } - -void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList) +void yaffs_AddDevice(yaffs_Device *dev) { + dev->isMounted = 0; + dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback; - yaffsfs_DeviceConfiguration *cfg; - - yaffsfs_configurationList = cfgList; + if(!dev->devList.next) + YINIT_LIST_HEAD(&dev->devList); - yaffsfs_InitHandles(); - - cfg = yaffsfs_configurationList; + ylist_add(&dev->devList,&yaffsfs_deviceList); +} - while(cfg && cfg->prefix && cfg->dev){ - cfg->dev->isMounted = 0; - cfg->dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback; - cfg++; - } +void yaffs_RemoveDevice(yaffs_Device *dev) +{ + ylist_del_init(&dev->devList); +} -} /* Directory search stuff. */ @@ -1939,6 +2304,23 @@ int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev) return -1; } + + +/* + * yaffs_n_handles() + * Returns number of handles attached to the object + */ +int yaffs_n_handles(const YCHAR *path) +{ + yaffs_Object *obj; + + obj = yaffsfs_FindObject(NULL,path,0); + if(obj) + obj = yaffs_GetEquivalentObject(obj); + + return yaffsfs_CountHandles(obj); +} + int yaffs_DumpDevStruct(const YCHAR *path) { #if 0