2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
19 #include "yaffs_guts.h"
25 #include <string.h> // for memset
28 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
31 #define NULL ((void *)0)
35 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.18 2007/07/18 19:40:38 charles Exp $";
37 // configurationList is the list of devices that are supported
38 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
41 /* Some forward references */
42 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const char *path, int symDepth);
43 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
50 unsigned int yaffs_wr_attempts;
54 __u8 inUse:1; // this handle is in use
55 __u8 readOnly:1; // this handle is read only
56 __u8 append:1; // append only
57 __u8 exclusive:1; // exclusive
58 __u32 position; // current position in file
59 yaffs_Object *obj; // the object
63 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
66 /// Inilitalise handles on start-up.
68 static int yaffsfs_InitHandles(void)
71 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
73 yaffsfs_handle[i].inUse = 0;
74 yaffsfs_handle[i].obj = NULL;
79 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
81 if(h < 0 || h >= YAFFSFS_N_HANDLES)
86 return &yaffsfs_handle[h];
89 yaffs_Object *yaffsfs_GetHandleObject(int handle)
91 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
103 // Grab a handle (when opening a file)
106 static int yaffsfs_GetHandle(void)
111 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
113 h = yaffsfs_GetHandlePointer(i);
116 // todo bug: should never happen
120 memset(h,0,sizeof(yaffsfs_Handle));
129 // Let go of a handle (when closing a file)
131 static int yaffsfs_PutHandle(int handle)
133 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
145 // Stuff to search for a directory from a path
148 int yaffsfs_Match(char a, char b)
154 // yaffsfs_FindDevice
156 // Scan the configuration list to find the root.
157 // Curveballs: Should match paths that end in '/' too
158 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
159 static yaffs_Device *yaffsfs_FindDevice(const char *path, char **restOfPath)
161 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
162 const char *leftOver;
164 yaffs_Device *retval = NULL;
166 int longestMatch = -1;
168 // Check all configs, choose the one that:
169 // 1) Actually matches a prefix (ie /a amd /abc will not match
170 // 2) Matches the longest.
171 while(cfg && cfg->prefix && cfg->dev)
177 while(*p && //unmatched part of prefix
178 strcmp(p,"/") && // the rest of the prefix is not / (to catch / at end)
180 yaffsfs_Match(*p,*leftOver))
186 if((!*p || strcmp(p,"/") == 0) && // end of prefix
187 (!*leftOver || *leftOver == '/') && // no more in this path name part
188 (thisMatchLength > longestMatch))
191 *restOfPath = (char *)leftOver;
193 longestMatch = thisMatchLength;
200 static yaffs_Object *yaffsfs_FindRoot(const char *path, char **restOfPath)
205 dev= yaffsfs_FindDevice(path,restOfPath);
206 if(dev && dev->isMounted)
213 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
216 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
218 char *alias = obj->variant.symLinkVariant.alias;
222 // Starts with a /, need to scan from root up
223 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
227 // Relative to here, so use the parent of the symlink as a start
228 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
231 printf("yaffsfs_FollowLink %x\n",obj);
237 // yaffsfs_FindDirectory
238 // Parse a path to determine the directory and the name within the directory.
240 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
241 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const char *path,char **name,int symDepth)
245 char str[YAFFS_MAX_NAME_LENGTH+1];
248 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
256 restOfPath = (char *)path;
260 dir = yaffsfs_FindRoot(path,&restOfPath);
266 // curve ball: also throw away surplus '/'
267 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
268 while(*restOfPath == '/')
270 restOfPath++; // get rid of '/'
276 while(*restOfPath && *restOfPath != '/')
278 if (i < YAFFS_MAX_NAME_LENGTH)
280 str[i] = *restOfPath;
289 // got to the end of the string
294 if(strcmp(str,".") == 0)
298 else if(strcmp(str,"..") == 0)
304 dir = yaffs_FindObjectByName(dir,str);
306 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
309 dir = yaffsfs_FollowLink(dir,symDepth);
313 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
320 // directory did not exist.
324 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const char *path,char **name,int symDepth)
326 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
329 // yaffsfs_FindObject turns a path for an existing object into the object
331 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const char *path,int symDepth)
336 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
337 printf("yaffsfs_FindObject-0 dir %x \n", dir);
341 printf("yaffsfs_FindObject-0-0 dir %x \n", dir);
342 return yaffs_FindObjectByName(dir,name);
350 int yaffs_open(const char *path, int oflag, int mode)
352 yaffs_Object *obj = NULL;
353 yaffs_Object *dir = NULL;
356 yaffsfs_Handle *h = NULL;
358 int alreadyExclusive = 0;
361 int errorReported = 0;
366 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
371 handle = yaffsfs_GetHandle();
376 h = yaffsfs_GetHandlePointer(handle);
379 // try to find the exisiting object
380 obj = yaffsfs_FindObject(NULL,path,0);
382 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
385 obj = yaffsfs_FollowLink(obj,symDepth++);
390 // Check if the object is already in use
391 alreadyOpen = alreadyExclusive = 0;
393 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
397 yaffsfs_handle[i].inUse &&
398 obj == yaffsfs_handle[i].obj)
401 if(yaffsfs_handle[i].exclusive)
403 alreadyExclusive = 1;
408 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
413 // Open should fail if O_CREAT and O_EXCL are specified
414 if((oflag & O_EXCL) && (oflag & O_CREAT))
417 yaffsfs_SetError(-EEXIST);
421 // Check file permissions
422 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
423 !(obj->yst_mode & S_IREAD))
428 if( (oflag & O_RDWR) &&
429 !(obj->yst_mode & S_IREAD))
434 if( (oflag & (O_RDWR | O_WRONLY)) &&
435 !(obj->yst_mode & S_IWRITE))
442 else if((oflag & O_CREAT))
444 // Let's see if we can create this file
445 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
448 obj = yaffs_MknodFile(dir,name,mode,0,0);
452 yaffsfs_SetError(-ENOTDIR);
456 if(obj && !openDenied)
460 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
461 h->append = (oflag & O_APPEND) ? 1 : 0;
462 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
466 if((oflag & O_TRUNC) && !h->readOnly)
469 yaffs_ResizeFile(obj,0);
475 yaffsfs_PutHandle(handle);
478 yaffsfs_SetError(-EACCESS);
491 int yaffs_close(int fd)
493 yaffsfs_Handle *h = NULL;
498 h = yaffsfs_GetHandlePointer(fd);
503 yaffs_FlushFile(h->obj,1);
505 if(h->obj->inUse <= 0 && h->obj->unlinked)
507 yaffs_DeleteFile(h->obj);
509 yaffsfs_PutHandle(fd);
515 yaffsfs_SetError(-EBADF);
524 int yaffs_read(int fd, void *buf, unsigned int nbyte)
526 yaffsfs_Handle *h = NULL;
527 yaffs_Object *obj = NULL;
533 h = yaffsfs_GetHandlePointer(fd);
534 obj = yaffsfs_GetHandleObject(fd);
539 yaffsfs_SetError(-EBADF);
544 if(yaffs_GetObjectFileLength(obj) > pos)
546 maxRead = yaffs_GetObjectFileLength(obj) - pos;
561 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
564 h->position = pos + nRead;
581 return (nRead >= 0) ? nRead : -1;
585 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
587 yaffsfs_Handle *h = NULL;
588 yaffs_Object *obj = NULL;
591 int writeThrough = 0;
594 h = yaffsfs_GetHandlePointer(fd);
595 obj = yaffsfs_GetHandleObject(fd);
600 yaffsfs_SetError(-EBADF);
602 else if( h && obj && h->readOnly)
610 pos = yaffs_GetObjectFileLength(obj);
617 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
621 h->position = pos + nWritten;
633 return (nWritten >= 0) ? nWritten : -1;
637 int yaffs_truncate(int fd, off_t newSize)
639 yaffsfs_Handle *h = NULL;
640 yaffs_Object *obj = NULL;
644 h = yaffsfs_GetHandlePointer(fd);
645 obj = yaffsfs_GetHandleObject(fd);
650 yaffsfs_SetError(-EBADF);
655 result = yaffs_ResizeFile(obj,newSize);
660 return (result) ? 0 : -1;
664 off_t yaffs_lseek(int fd, off_t offset, int whence)
666 yaffsfs_Handle *h = NULL;
667 yaffs_Object *obj = NULL;
672 h = yaffsfs_GetHandlePointer(fd);
673 obj = yaffsfs_GetHandleObject(fd);
678 yaffsfs_SetError(-EBADF);
680 else if(whence == SEEK_SET)
687 else if(whence == SEEK_CUR)
689 if( (h->position + offset) >= 0)
691 pos = (h->position + offset);
694 else if(whence == SEEK_END)
696 fSize = yaffs_GetObjectFileLength(obj);
697 if(fSize >= 0 && (fSize + offset) >= 0)
699 pos = fSize + offset;
719 int yaffsfs_DoUnlink(const char *path,int isDirectory)
721 yaffs_Object *dir = NULL;
722 yaffs_Object *obj = NULL;
724 int result = YAFFS_FAIL;
728 obj = yaffsfs_FindObject(NULL,path,0);
729 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
732 yaffsfs_SetError(-ENOTDIR);
736 yaffsfs_SetError(-ENOENT);
738 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
740 yaffsfs_SetError(-EISDIR);
742 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
744 yaffsfs_SetError(-ENOTDIR);
748 result = yaffs_Unlink(dir,name);
750 if(result == YAFFS_FAIL && isDirectory)
752 yaffsfs_SetError(-ENOTEMPTY);
760 return (result == YAFFS_FAIL) ? -1 : 0;
762 int yaffs_rmdir(const char *path)
764 return yaffsfs_DoUnlink(path,1);
767 int yaffs_unlink(const char *path)
769 return yaffsfs_DoUnlink(path,0);
772 int yaffs_rename(const char *oldPath, const char *newPath)
774 yaffs_Object *olddir = NULL;
775 yaffs_Object *newdir = NULL;
776 yaffs_Object *obj = NULL;
779 int result= YAFFS_FAIL;
780 int renameAllowed = 1;
784 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
785 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
786 obj = yaffsfs_FindObject(NULL,oldPath,0);
788 if(!olddir || !newdir || !obj)
791 yaffsfs_SetError(-EBADF);
794 else if(olddir->myDev != newdir->myDev)
796 // oops must be on same device
798 yaffsfs_SetError(-EXDEV);
801 else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
803 // It is a directory, check that it is not being renamed to
804 // being its own decendent.
805 // Do this by tracing from the new directory back to the root, checking for obj
807 yaffs_Object *xx = newdir;
809 while( renameAllowed && xx)
817 if(!renameAllowed) yaffsfs_SetError(-EACCESS);
822 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
827 return (result == YAFFS_FAIL) ? -1 : 0;
831 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
835 printf("yaffsfs_DoStat-0 obj %x \n", obj);
839 obj = yaffs_GetEquivalentObject(obj);
840 printf("yaffsfs_DoStat-0-0 obj %x \n", obj);
845 buf->st_dev = (int)obj->myDev->genericDevice;
846 buf->st_ino = obj->objectId;
847 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
849 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
851 buf->st_mode |= S_IFDIR;
853 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
855 buf->st_mode |= S_IFLNK;
857 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
859 buf->st_mode |= S_IFREG;
862 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
865 buf->st_rdev = obj->yst_rdev;
866 buf->st_size = yaffs_GetObjectFileLength(obj);
867 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
868 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
869 buf->yst_atime = obj->yst_atime;
870 buf->yst_ctime = obj->yst_ctime;
871 buf->yst_mtime = obj->yst_mtime;
873 printf("yaffsfs_DoStat obj->variantType %x \n", obj->variantType);
874 printf("yaffsfs_DoStat buf %x\n", buf);
875 printf("yaffsfs_DoStat retValadd %x\n", &retVal);
876 printf("yaffsfs_DoStat buf->st_size %x\n", buf->st_size);
877 printf("yaffsfs_DoStat buf->st_blksize %x\n", buf->st_blksize);
878 printf("yaffsfs_DoStat buf->st_blocks %x\n", buf->st_blocks);
879 printf("yaffsfs_DoStat buf->yst_atime %x\n", buf->yst_atime);
880 printf("yaffsfs_DoStat buf->yst_ctime %x\n", buf->yst_ctime);
881 printf("yaffsfs_DoStat buf->yst_mtime %x\n", buf->yst_mtime);
887 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
894 obj = yaffsfs_FindObject(NULL,path,0);
896 printf("yaffsfs_DoStatOrLStat %x\n",obj);
900 obj = yaffsfs_FollowLink(obj,0);
902 printf("yaffsfs_DoStatOrLStat-1 %x\n",obj);
905 retVal = yaffsfs_DoStat(obj,buf);
909 // todo error not found
910 yaffsfs_SetError(-ENOENT);
919 int yaffs_stat(const char *path, struct yaffs_stat *buf)
921 return yaffsfs_DoStatOrLStat(path,buf,0);
924 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
926 return yaffsfs_DoStatOrLStat(path,buf,1);
929 int yaffs_fstat(int fd, struct yaffs_stat *buf)
936 obj = yaffsfs_GetHandleObject(fd);
940 retVal = yaffsfs_DoStat(obj,buf);
945 yaffsfs_SetError(-EBADF);
953 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
955 int result = YAFFS_FAIL;
959 obj = yaffs_GetEquivalentObject(obj);
964 obj->yst_mode = mode;
966 result = yaffs_FlushFile(obj,0);
969 return result == YAFFS_OK ? 0 : -1;
973 int yaffs_chmod(const char *path, mode_t mode)
980 obj = yaffsfs_FindObject(NULL,path,0);
984 retVal = yaffsfs_DoChMod(obj,mode);
988 // todo error not found
989 yaffsfs_SetError(-ENOENT);
999 int yaffs_fchmod(int fd, mode_t mode)
1006 obj = yaffsfs_GetHandleObject(fd);
1010 retVal = yaffsfs_DoChMod(obj,mode);
1015 yaffsfs_SetError(-EBADF);
1024 int yaffs_mkdir(const char *path, mode_t mode)
1026 yaffs_Object *parent = NULL;
1027 yaffs_Object *dir = NULL;
1032 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1034 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1041 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1050 int yaffs_mount(const char *path)
1053 int result=YAFFS_FAIL;
1054 yaffs_Device *dev=NULL;
1057 T(YAFFS_TRACE_ALWAYS,("yaffs: Mounting %s\n",path));
1060 dev = yaffsfs_FindDevice(path,&dummy);
1065 result = yaffs_GutsInitialise(dev);
1066 if(result == YAFFS_FAIL)
1068 // todo error - mount failed
1069 yaffsfs_SetError(-ENOMEM);
1071 retVal = result ? 0 : -1;
1076 //todo error - already mounted.
1077 yaffsfs_SetError(-EBUSY);
1082 // todo error - no device
1083 yaffsfs_SetError(-ENODEV);
1090 int yaffs_unmount(const char *path)
1093 yaffs_Device *dev=NULL;
1097 dev = yaffsfs_FindDevice(path,&dummy);
1105 yaffs_FlushEntireDeviceCache(dev);
1106 yaffs_CheckpointSave(dev);
1108 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1110 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1112 inUse = 1; // the device is in use, can't unmount
1118 yaffs_Deinitialise(dev);
1124 // todo error can't unmount as files are open
1125 yaffsfs_SetError(-EBUSY);
1131 //todo error - not mounted.
1132 yaffsfs_SetError(-EINVAL);
1138 // todo error - no device
1139 yaffsfs_SetError(-ENODEV);
1146 loff_t yaffs_freespace(const char *path)
1149 yaffs_Device *dev=NULL;
1153 dev = yaffsfs_FindDevice(path,&dummy);
1154 if(dev && dev->isMounted)
1156 retVal = yaffs_GetNumberOfFreeChunks(dev);
1157 retVal *= dev->nDataBytesPerChunk;
1162 yaffsfs_SetError(-EINVAL);
1171 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1174 yaffsfs_DeviceConfiguration *cfg;
1176 yaffsfs_configurationList = cfgList;
1178 yaffsfs_InitHandles();
1180 cfg = yaffsfs_configurationList;
1182 while(cfg && cfg->prefix && cfg->dev)
1184 cfg->dev->isMounted = 0;
1185 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1192 // Directory search stuff.
1195 // Directory search context
1197 // NB this is an opaque structure.
1203 yaffs_dirent de; /* directory entry being used by this dsc */
1204 char name[NAME_MAX+1]; /* name of directory being searched */
1205 yaffs_Object *dirObj; /* ptr to directory being searched */
1206 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1208 struct list_head others;
1209 } yaffsfs_DirectorySearchContext;
1213 static struct list_head search_contexts;
1216 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1220 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1224 if( list_empty(&dsc->dirObj->variant.directoryVariant.children)){
1225 dsc->nextReturn = NULL;
1227 dsc->nextReturn = list_entry(dsc->dirObj->variant.directoryVariant.children.next,
1228 yaffs_Object,siblings);
1231 /* Hey someone isn't playing nice! */
1235 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1239 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1241 if( dsc->nextReturn == NULL ||
1242 list_empty(&dsc->dirObj->variant.directoryVariant.children)){
1243 dsc->nextReturn = NULL;
1245 struct list_head *next = dsc->nextReturn->siblings.next;
1247 if( next == &dsc->dirObj->variant.directoryVariant.children)
1248 dsc->nextReturn = NULL; /* end of list */
1250 dsc->nextReturn = list_entry(next,yaffs_Object,siblings);
1253 /* Hey someone isn't playing nice! */
1257 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1260 struct list_head *i;
1261 yaffsfs_DirectorySearchContext *dsc;
1263 /* if search contexts not initilised then skip */
1264 if(!search_contexts.next)
1267 /* Iteratethrough the directory search contexts.
1268 * If any are the one being removed, then advance the dsc to
1269 * the next one to prevent a hanging ptr.
1271 list_for_each(i, &search_contexts) {
1273 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
1274 if(dsc->nextReturn == obj)
1275 yaffsfs_DirAdvance(dsc);
1281 yaffs_DIR *yaffs_opendir(const char *dirname)
1283 yaffs_DIR *dir = NULL;
1284 yaffs_Object *obj = NULL;
1285 yaffsfs_DirectorySearchContext *dsc = NULL;
1289 obj = yaffsfs_FindObject(NULL,dirname,0);
1291 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1294 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1295 dir = (yaffs_DIR *)dsc;
1298 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1299 dsc->magic = YAFFS_MAGIC;
1301 strncpy(dsc->name,dirname,NAME_MAX);
1302 INIT_LIST_HEAD(&dsc->others);
1304 if(!search_contexts.next)
1305 INIT_LIST_HEAD(&search_contexts);
1307 list_add(&dsc->others,&search_contexts);
1308 yaffsfs_SetDirRewound(dsc); }
1317 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1319 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1320 struct yaffs_dirent *retVal = NULL;
1324 if(dsc && dsc->magic == YAFFS_MAGIC){
1325 yaffsfs_SetError(0);
1326 if(dsc->nextReturn){
1327 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1328 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1329 dsc->de.d_off = dsc->offset++;
1330 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1331 if(strlen(dsc->de.d_name) == 0)
1333 // this should not happen!
1334 strcpy(dsc->de.d_name,"zz");
1336 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1338 yaffsfs_DirAdvance(dsc);
1344 yaffsfs_SetError(-EBADF);
1354 void yaffs_rewinddir(yaffs_DIR *dirp)
1356 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1360 yaffsfs_SetDirRewound(dsc);
1366 int yaffs_closedir(yaffs_DIR *dirp)
1368 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1372 list_del(&dsc->others); /* unhook from list */
1378 // end of directory stuff
1381 int yaffs_symlink(const char *oldpath, const char *newpath)
1383 yaffs_Object *parent = NULL;
1387 int mode = 0; // ignore for now
1390 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1391 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1398 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1408 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1410 yaffs_Object *obj = NULL;
1416 obj = yaffsfs_FindObject(NULL,path,0);
1420 yaffsfs_SetError(-ENOENT);
1423 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1425 yaffsfs_SetError(-EINVAL);
1430 char *alias = obj->variant.symLinkVariant.alias;
1431 memset(buf,0,bufsiz);
1432 strncpy(buf,alias,bufsiz - 1);
1439 int yaffs_link(const char *oldpath, const char *newpath)
1441 // Creates a link called newpath to existing oldpath
1442 yaffs_Object *obj = NULL;
1443 yaffs_Object *target = NULL;
1449 obj = yaffsfs_FindObject(NULL,oldpath,0);
1450 target = yaffsfs_FindObject(NULL,newpath,0);
1454 yaffsfs_SetError(-ENOENT);
1459 yaffsfs_SetError(-EEXIST);
1464 yaffs_Object *newdir = NULL;
1465 yaffs_Object *link = NULL;
1469 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1473 yaffsfs_SetError(-ENOTDIR);
1476 else if(newdir->myDev != obj->myDev)
1478 yaffsfs_SetError(-EXDEV);
1481 if(newdir && strlen(newname) > 0)
1483 link = yaffs_Link(newdir,newname,obj);
1488 yaffsfs_SetError(-ENOSPC);
1499 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1501 int yaffs_DumpDevStruct(const char *path)
1505 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1509 yaffs_Device *dev = obj->myDev;
1512 "nPageWrites.......... %d\n"
1513 "nPageReads........... %d\n"
1514 "nBlockErasures....... %d\n"
1515 "nGCCopies............ %d\n"
1516 "garbageCollections... %d\n"
1517 "passiveGarbageColl'ns %d\n"
1521 dev->nBlockErasures,
1523 dev->garbageCollections,
1524 dev->passiveGarbageCollections