#define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
/* Some forward references */
-static yaffs_obj_t *yaffsfs_FindObject(yaffs_obj_t *relativeDirectory, const YCHAR *path, int symDepth);
+static yaffs_obj_t *yaffsfs_FindObject(yaffs_obj_t *relativeDirectory, const YCHAR *path, int symDepth, int getEquiv);
static void yaffsfs_RemoveObjectCallback(yaffs_obj_t *obj);
unsigned int yaffs_wr_attempts;
static yaffs_obj_t *yaffsfs_FollowLink(yaffs_obj_t *obj,int symDepth)
{
+ if(obj)
+ obj = yaffs_get_equivalent_obj(obj);
+
while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
YCHAR *alias = obj->variant.symlink_variant.alias;
if(yaffsfs_IsPathDivider(*alias))
/* Starts with a /, need to scan from root up */
- obj = yaffsfs_FindObject(NULL,alias,symDepth++);
+ obj = yaffsfs_FindObject(NULL,alias,symDepth++,1);
else
/* Relative to here, so use the parent of the symlink as a start */
- obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
+ obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1);
}
return obj;
}
else{
dir = yaffs_find_by_name(dir,str);
- while(dir && dir->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
- dir = yaffsfs_FollowLink(dir,symDepth);
-
+ dir = yaffsfs_FollowLink(dir,symDepth);
if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
dir = NULL;
/*
* yaffsfs_FindObject turns a path for an existing object into the object
*/
-static yaffs_obj_t *yaffsfs_FindObject(yaffs_obj_t *relativeDirectory, const YCHAR *path,int symDepth)
+static yaffs_obj_t *yaffsfs_FindObject(yaffs_obj_t *relativeDirectory, const YCHAR *path,int symDepth, int getEquiv)
{
yaffs_obj_t *dir;
+ yaffs_obj_t *obj;
YCHAR *name;
dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
if(dir && *name)
- return yaffs_find_by_name(dir,name);
+ obj = yaffs_find_by_name(dir,name);
+ else
+ obj = dir;
- return dir;
+ if(getEquiv)
+ obj = yaffs_get_equivalent_obj(obj);
+
+ return obj;
}
}
+
+
int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
{
yaffs_obj_t *obj = NULL;
yaffs_obj_t *dir = NULL;
YCHAR *name;
int handle = -1;
- yaffsfs_Handle *h = NULL;
+ yaffsfs_Handle *yh = NULL;
int openDenied = 0;
int symDepth = 0;
int errorReported = 0;
+ int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
__u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
__u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
__u8 sharedReadAllowed;
/* Todo: Are there any more flag combos to sanitise ? */
+ /* Figure out if reading or writing is requested */
+
+ readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
+ writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
yaffsfs_Lock();
handle = yaffsfs_GetNewHandle();
- if(handle >= 0){
+ if(handle < 0){
+ yaffsfs_SetError(-ENFILE);
+ errorReported = 1;
+ } else {
- h = yaffsfs_GetHandlePointer(handle);
+ yh = yaffsfs_GetHandlePointer(handle);
/* try to find the exisiting object */
- obj = yaffsfs_FindObject(NULL,path,0);
-
- if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
- obj = yaffsfs_FollowLink(obj,symDepth++);
+ obj = yaffsfs_FindObject(NULL,path,0,1);
- if(obj)
- obj = yaffs_get_equivalent_obj(obj);
+ obj = yaffsfs_FollowLink(obj,symDepth++);
if(obj &&
obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
}
/* Check file permissions */
- if( (oflag & (O_RDWR | O_WRONLY)) == 0 && /* ie O_RDONLY */
- !(obj->yst_mode & S_IREAD))
- openDenied = 1;
-
- if( (oflag & O_RDWR) &&
- !(obj->yst_mode & S_IREAD))
+ if( readRequested && !(obj->yst_mode & S_IREAD))
openDenied = 1;
- if( (oflag & (O_RDWR | O_WRONLY)) &&
- !(obj->yst_mode & S_IWRITE))
+ if( writeRequested && !(obj->yst_mode & S_IWRITE))
openDenied = 1;
/* Check sharing of an existing object. */
{
- yaffsfs_Handle *h;
+ yaffsfs_Handle *hx;
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)
+ hx = &yaffsfs_handle[i];
+ if(hx->useCount > 0 &&
+ hx->inodeId >= 0 &&
+ yaffsfs_inode[hx->inodeId].iObj == obj){
+ if(!hx->shareRead)
sharedReadAllowed = 0;
- if(!h->shareWrite)
+ if(!hx->shareWrite)
sharedWriteAllowed = 0;
- if(h->reading)
+ if(hx->reading)
alreadyReading = 1;
- if(h->writing)
- alreadyWriting = 0;
+ if(hx->writing)
+ alreadyWriting = 1;
}
}
- readRequested = (oflag & (O_RDWR | O_RDONLY)) ? 1 : 0;
- writeRequested = (oflag & (O_RDWR | O_WRONLY)) ? 1 : 0;
+
if((!sharedReadAllowed && readRequested)||
(!shareRead && alreadyReading) ||
*/
}
- h->inodeId = inodeId;
- 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;
+ yh->inodeId = inodeId;
+ yh->reading = readRequested;
+ yh->writing = writeRequested;
+ yh->append = (oflag & O_APPEND) ? 1 : 0;
+ yh->position = 0;
+ yh->shareRead = shareRead;
+ yh->shareWrite = shareWrite;
/* Hook inode to object */
obj->my_inode = (void*) &yaffsfs_inode[inodeId];
- if((oflag & O_TRUNC) && h->writing)
+ if((oflag & O_TRUNC) && yh->writing)
yaffs_resize_file(obj,0);
} else {
yaffsfs_PutHandle(handle);
if(!errorReported) {
- yaffsfs_SetError(-EACCES);
+ yaffsfs_SetError(!obj ? -ENOSPC : -EACCES);
errorReported = 1;
}
handle = -1;
/* bad handle */
yaffsfs_SetError(-EBADF);
totalRead = -1;
- } else if( h && obj){
+ } else if(!h->reading){
+ /* Not a reading handle */
+ yaffsfs_SetError(-EINVAL);
+ totalRead = -1;
+ } else {
if(isPread)
startPos = offset;
else
} else if( h && obj && (!h->writing || obj->my_dev->read_only)){
yaffsfs_SetError(-EINVAL);
totalWritten=-1;
- } else if( h && obj){
+ } else {
if(h->append)
startPos = yaffs_get_obj_length(obj);
else if(isPwrite)
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
- if(obj)
- obj = yaffs_get_equivalent_obj(obj);
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(!obj)
yaffsfs_SetError(-ENOENT);
h = yaffsfs_GetHandlePointer(fd);
obj = yaffsfs_GetHandleObject(fd);
- if(!h || !obj)
+ if(!h || !obj){
/* bad handle */
yaffsfs_SetError(-EBADF);
- else if(whence == SEEK_SET){
- if(offset >= 0)
- pos = offset;
- }
- else if(whence == SEEK_CUR) {
- if( (h->position + offset) >= 0)
- pos = (h->position + offset);
- }
- else if(whence == SEEK_END) {
- fSize = yaffs_get_obj_length(obj);
- if(fSize >= 0 && (fSize + offset) >= 0)
- pos = fSize + offset;
- }
-
- if(pos >= 0)
- h->position = pos;
- else {
- /* todo error */
+ } else {
+ if(whence == SEEK_SET){
+ if(offset >= 0)
+ pos = offset;
+ } else if(whence == SEEK_CUR) {
+ if( (h->position + offset) >= 0)
+ pos = (h->position + offset);
+ } else if(whence == SEEK_END) {
+ fSize = yaffs_get_obj_length(obj);
+ if(fSize >= 0 && (fSize + offset) >= 0)
+ pos = fSize + offset;
+ }
+
+ if(pos >= 0)
+ h->position = pos;
+ else
+ yaffsfs_SetError(-EINVAL);
}
-
yaffsfs_Unlock();
return pos;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+ obj = yaffsfs_FindObject(NULL,path,0,0);
dir = yaffsfs_FindDirectory(NULL,path,&name,0);
if(!dir)
yaffsfs_SetError(-ENOTDIR);
olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
- obj = yaffsfs_FindObject(NULL,oldPath,0);
+ obj = yaffsfs_FindObject(NULL,oldPath,0,0);
if(!olddir || !newdir || !obj) {
/* bad file */
{
int retVal = -1;
- if(obj)
- obj = yaffs_get_equivalent_obj(obj);
+ obj = yaffs_get_equivalent_obj(obj);
if(obj && buf){
buf->st_dev = (int)obj->my_dev->os_context;
int retVal = -1;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(!doLStat && obj)
obj = yaffsfs_FollowLink(obj,0);
int retVal = -1;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(follow)
obj = yaffsfs_FollowLink(obj,0);
int retVal = -1;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(follow)
obj = yaffsfs_FollowLink(obj,0);
int retVal = -1;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(follow)
obj = yaffsfs_FollowLink(obj,0);
int retVal = -1;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(follow)
obj = yaffsfs_FollowLink(obj,0);
int retval = 0;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(obj) {
int access_ok = 1;
int retVal = -1;
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(!obj)
yaffsfs_SetError(-ENOENT);
yaffs_obj_t *parent = NULL;
yaffs_obj_t *dir = NULL;
YCHAR *name;
+ YCHAR *use_path = NULL;
+ int path_length = 0;
int retVal= -1;
+ int i;
+
+ /*
+ * We don't have a definition for max path length.
+ * We will use 3 * max name length instead.
+ */
+
+ path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
+
+ /* If the last character is a path divider, then we need to
+ * trim it back so that the name look-up works properly.
+ * eg. /foo/new_dir/ -> /foo/newdir
+ * Curveball: Need to handle multiple path dividers:
+ * eg. /foof/sdfse///// -> /foo/sdfse
+ */
+ if(path_length > 0 &&
+ yaffsfs_IsPathDivider(path[path_length-1])){
+ use_path = YMALLOC(path_length + 1);
+ if(!use_path){
+ yaffsfs_SetError(-ENOMEM);
+ return -1;
+ }
+ strcpy(use_path, path);
+ for(i = path_length-1;
+ i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
+ i--)
+ use_path[i] = (YCHAR) 0;
+ path = use_path;
+ }
+
yaffsfs_Lock();
parent = yaffsfs_FindDirectory(NULL,path,&name,0);
if(parent && yaffs_strnlen(name,5) == 0){
yaffsfs_Unlock();
+ if(use_path)
+ YFREE(use_path);
+
return retVal;
}
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,dirname,0);
+ obj = yaffsfs_FindObject(NULL,dirname,0,1);
if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,path,0);
+ obj = yaffsfs_FindObject(NULL,path,0,1);
if(!obj) {
yaffsfs_SetError(-ENOENT);
yaffsfs_Lock();
- obj = yaffsfs_FindObject(NULL,oldpath,0);
- target = yaffsfs_FindObject(NULL,newpath,0);
+ obj = yaffsfs_FindObject(NULL,oldpath,0,1);
+ target = yaffsfs_FindObject(NULL,newpath,0,0);
if(!obj) {
yaffsfs_SetError(-ENOENT);
{
yaffs_obj_t *obj;
- obj = yaffsfs_FindObject(NULL,path,0);
- if(obj)
- obj = yaffs_get_equivalent_obj(obj);
+ obj = yaffsfs_FindObject(NULL,path,0,1);
return yaffsfs_CountHandles(obj);
}
+int yaffs_get_error(void)
+{
+ return yaffsfs_GetLastError();
+}
+
int yaffs_dump_dev(const YCHAR *path)
{
#if 0