*** empty log message ***
[yaffs/.git] / direct / yaffsfs.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  * yaffsfs.c  The interface functions for using YAFFS via a "direct" interface.
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  *
13  */
14  
15 #include "yaffsfs.h"
16 #include "yaffs_guts.h"
17 #include "yaffscfg.h"
18 #include <string.h> // for memset
19 #include "yportenv.h"
20
21 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
22
23 #ifndef NULL
24 #define NULL ((void *)0)
25 #endif
26
27
28 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.1 2003-01-21 03:32:17 charles Exp $";
29
30 // configurationList is the list of devices that are supported
31 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
32
33
34 //
35 // Directory search context
36 //
37 // NB this is an opaque structure.
38
39 struct yaffsfs_ObjectListEntry
40 {
41         int objectId;
42         struct yaffsfs_ObjectListEntry *next;
43 };
44
45
46 typedef struct
47 {
48         __u32 magic;
49         yaffs_dirent de;
50         struct yaffsfs_ObjectListEntry *list;
51         char name[NAME_MAX+1];
52         
53 } yaffsfs_DirectorySearchContext;
54
55
56 // Handle management.
57 // 
58
59 typedef struct
60 {
61         __u8  inUse:1;          // this handle is in use
62         __u8  readOnly:1;       // this handle is read only
63         __u8  append:1;         // append only
64         __u8  exclusive:1;      // exclusive
65         __u32 position;         // current position in file
66         yaffs_Object *obj;      // the object
67 }yaffsfs_Handle;
68
69
70 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const char *path, int symDepth);
71
72
73
74 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
75
76 // yaffsfs_InitHandle
77 /// Inilitalise handles on start-up.
78 //
79 static int yaffsfs_InitHandles(void)
80 {
81         int i;
82         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
83         {
84                 yaffsfs_handle[i].inUse = 0;
85                 yaffsfs_handle[i].obj = NULL;
86         }
87         return 0;
88 }
89
90 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
91 {
92         if(h < 0 || h >= YAFFSFS_N_HANDLES)
93         {
94                 return NULL;
95         }
96         
97         return &yaffsfs_handle[h];
98 }
99
100 yaffs_Object *yaffsfs_GetHandleObject(int handle)
101 {
102         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
103
104         if(h && h->inUse)
105         {
106                 return h->obj;
107         }
108         
109         return NULL;
110 }
111
112
113 //yaffsfs_GetHandle
114 // Grab a handle (when opening a file)
115 //
116
117 static int yaffsfs_GetHandle(void)
118 {
119         int i;
120         yaffsfs_Handle *h;
121         
122         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
123         {
124                 h = yaffsfs_GetHandlePointer(i);
125                 if(!h)
126                 {
127                         // todo bug: should never happen
128                 }
129                 if(!h->inUse)
130                 {
131                         memset(h,0,sizeof(yaffsfs_Handle));
132                         h->inUse=1;
133                         return i;
134                 }
135         }
136         return -1;
137 }
138
139 // yaffs_PutHandle
140 // Let go of a handle (when closing a file)
141 //
142 static int yaffsfs_PutHandle(int handle)
143 {
144         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
145         
146         if(h)
147         {
148                 h->inUse = 0;
149                 h->obj = NULL;
150         }
151         return 0;
152 }
153
154
155
156 // Stuff to search for a directory from a path
157
158
159 int yaffsfs_Match(char a, char b)
160 {
161         // case sensitive
162         return (a == b);
163 }
164
165 // yaffsfs_FindDevice
166 // yaffsfs_FindRoot
167 // Scan the configuration list to find the root.
168 static yaffs_Device *yaffsfs_FindDevice(const char *path, char **restOfPath)
169 {
170         yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
171         const char *leftOver;
172         const char *p;
173         
174         while(cfg && cfg->prefix && cfg->dev)
175         {
176                 leftOver = path;
177                 p = cfg->prefix;
178                 while(*p && *leftOver && yaffsfs_Match(*p,*leftOver))
179                 {
180                         p++;
181                         leftOver++;
182                 }
183                 if(!*p)
184                 {
185                         // Matched prefix
186                         *restOfPath = (char *)leftOver;
187                         return cfg->dev;
188                 }
189                 cfg++;
190         }
191         return NULL;
192 }
193
194 static yaffs_Object *yaffsfs_FindRoot(const char *path, char **restOfPath)
195 {
196
197         yaffs_Device *dev;
198         
199         dev= yaffsfs_FindDevice(path,restOfPath);
200         if(dev && dev->isMounted)
201         {
202                 return dev->rootDir;
203         }
204         return NULL;
205 }
206
207 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
208 {
209
210         while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
211         {
212                 char *alias = obj->variant.symLinkVariant.alias;
213                                                 
214                 if(*alias == '/')
215                 {
216                         // Starts with a /, need to scan from root up
217                         obj = yaffsfs_FindObject(NULL,alias,symDepth++);
218                 }
219                 else
220                 {
221                         // Relative to here, so use the parent of the symlink as a start
222                         obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
223                 }
224         }
225         return obj;
226 }
227
228
229 // yaffsfs_FindDirectory
230 // Parse a path to determine the directory and the name within the directory.
231 //
232 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
233 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const char *path,char **name,int symDepth)
234 {
235         yaffs_Object *dir;
236         char *restOfPath;
237         char str[YAFFS_MAX_NAME_LENGTH+1];
238         int i;
239         
240         if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
241         {
242                 return NULL;
243         }
244         
245         if(startDir)
246         {
247                 dir = startDir;
248                 restOfPath = (char *)path;
249         }
250         else
251         {
252                 dir = yaffsfs_FindRoot(path,&restOfPath);
253         }
254         
255         while(dir)
256         {       
257                 // parse off /.
258                 // curve ball: also throw away surplus '/' 
259                 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
260                 while(*restOfPath == '/')
261                 {
262                         restOfPath++; // get rid of '/'
263                 }
264                 
265                 *name = restOfPath;
266                 i = 0;
267                 
268                 while(*restOfPath && *restOfPath != '/')
269                 {
270                         if (i < YAFFS_MAX_NAME_LENGTH)
271                         {
272                                 str[i] = *restOfPath;
273                                 str[i+1] = '\0';
274                                 i++;
275                         }
276                         restOfPath++;
277                 }
278                 
279                 if(!*restOfPath)
280                 {
281                         // got to the end of the string
282                         return dir;
283                 }
284                 else
285                 {
286                         if(strcmp(str,".") == 0)
287                         {
288                                 // Do nothing
289                         }
290                         else if(strcmp(str,"..") == 0)
291                         {
292                                 dir = dir->parent;
293                         }
294                         else
295                         {
296                                 dir = yaffs_FindObjectByName(dir,str);
297                                 
298                                 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
299                                 {
300                                 
301                                         dir = yaffsfs_FollowLink(dir,symDepth);
302                 
303                                 }
304                                 
305                                 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
306                                 {
307                                         dir = NULL;
308                                 }
309                         }
310                 }
311         }
312         // directory did not exist.
313         return NULL;
314 }
315
316 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const char *path,char **name,int symDepth)
317 {
318         return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
319 }
320
321 // yaffsfs_FindObject turns a path for an existing object into the object
322 // 
323 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const char *path,int symDepth)
324 {
325         yaffs_Object *dir;
326         char *name;
327         
328         dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
329         
330         if(dir && *name)
331         {
332                 return yaffs_FindObjectByName(dir,name);
333         }
334         
335         return dir;
336 }
337
338
339
340 int yaffs_open(const char *path, int oflag, int mode)
341 {
342         yaffs_Object *obj = NULL;
343         yaffs_Object *dir = NULL;
344         char *name;
345         int handle = -1;
346         yaffsfs_Handle *h = NULL;
347         int alreadyOpen = 0;
348         int alreadyExclusive = 0;
349         int openDenied = 0;
350         int symDepth = 0;
351         
352         int i;
353         
354         
355         // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
356         
357         
358         yaffsfs_Lock();
359         
360         handle = yaffsfs_GetHandle();
361         
362         if(handle >= 0)
363         {
364
365                 h = yaffsfs_GetHandlePointer(handle);
366         
367         
368                 // try to find the exisiting object
369                 obj = yaffsfs_FindObject(NULL,path,0);
370                 
371                 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
372                 {
373                 
374                         obj = yaffsfs_FollowLink(obj,symDepth++);
375                 }
376
377                 if(obj)
378                 {
379                         // Check if the object is already in use
380                         alreadyOpen = alreadyExclusive = 0;
381                         
382                         for(i = 0; i <= YAFFSFS_N_HANDLES; i++)
383                         {
384                                 
385                                 if(i != handle &&
386                                    yaffsfs_handle[i].inUse &&
387                                     obj == yaffsfs_handle[i].obj)
388                                  {
389                                         alreadyOpen = 1;
390                                         if(yaffsfs_handle[i].exclusive)
391                                         {
392                                                 alreadyExclusive = 1;
393                                         }
394                                  }
395                         }
396
397                         if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
398                         {
399                                 openDenied = 1;
400                         }
401                         
402                         // Check file permissions
403                         if( (oflag & (O_RDWR | O_WRONLY)) == 0 &&     // ie O_RDONLY
404                            !(obj->st_mode & S_IREAD))
405                         {
406                                 openDenied = 1;
407                         }
408
409                         if( (oflag & O_RDWR) && 
410                            !(obj->st_mode & S_IREAD))
411                         {
412                                 openDenied = 1;
413                         }
414
415                         if( (oflag & (O_RDWR | O_WRONLY)) && 
416                            !(obj->st_mode & S_IWRITE))
417                         {
418                                 openDenied = 1;
419                         }
420                         
421                 }
422                 
423                 else if((oflag & O_CREAT))
424                 {
425                         // Let's see if we can create this file
426                         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
427                         obj = yaffs_MknodFile(dir,name,mode,0,0);       
428                 }
429                 
430                 if(obj && !openDenied)
431                 {
432                         h->obj = obj;
433                         h->inUse = 1;
434                 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
435                         h->append =  (oflag & O_APPEND) ? 1 : 0;
436                         h->exclusive = (oflag & O_EXCL) ? 1 : 0;
437                         h->position = 0;
438                         
439                         obj->inUse++;
440                         if((oflag & O_TRUNC) && !h->readOnly)
441                         {
442                                 //todo truncate
443                                 yaffs_ResizeFile(obj,0);
444                         }
445                         
446                 }
447                 else
448                 {
449                         yaffsfs_PutHandle(handle);
450                         yaffsfs_SetError(-EACCESS);
451                         handle = -1;
452                 }
453                 
454         }
455         
456         yaffsfs_Unlock();
457         
458         return handle;          
459 }
460
461 int yaffs_close(int fd)
462 {
463         yaffsfs_Handle *h = NULL;
464         int retVal = 0;
465         
466         yaffsfs_Lock();
467
468         h = yaffsfs_GetHandlePointer(fd);
469         
470         if(h && h->inUse)
471         {
472                 // clean up
473                 yaffs_FlushFile(h->obj,1);
474                 h->obj->inUse--;
475                 if(h->obj->inUse <= 0 && h->obj->unlinked)
476                 {
477                         yaffs_DeleteFile(h->obj);
478                 }
479                 yaffsfs_PutHandle(fd);
480                 retVal = 0;
481         }
482         else
483         {
484                 // bad handle
485                 yaffsfs_SetError(-EBADF);               
486                 retVal = -1;
487         }
488         
489         yaffsfs_Unlock();
490         
491         return retVal;
492 }
493
494 int yaffs_read(int fd, void *buf, unsigned int nbyte)
495 {
496         yaffsfs_Handle *h = NULL;
497         yaffs_Object *obj = NULL;
498         int pos = 0;
499         int nRead = -1;
500         int maxRead;
501         
502         yaffsfs_Lock();
503         h = yaffsfs_GetHandlePointer(fd);
504         obj = yaffsfs_GetHandleObject(fd);
505         
506         if(!h || !obj)
507         {
508                 // bad handle
509                 yaffsfs_SetError(-EBADF);               
510         }
511         else if( h && obj)
512         {
513                 pos=  h->position;
514                 if(yaffs_GetObjectFileLength(obj) > pos)
515                 {
516                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
517                 }
518                 else
519                 {
520                         maxRead = 0;
521                 }
522
523                 if(nbyte > maxRead)
524                 {
525                         nbyte = maxRead;
526                 }
527
528                 
529                 if(nbyte > 0)
530                 {
531                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
532                         if(nRead >= 0)
533                         {
534                                 h->position = pos + nRead;
535                         }
536                         else
537                         {
538                                 //todo error
539                         }
540                 }
541                 else
542                 {
543                         //todo error
544                 }
545                 
546         }
547         
548         yaffsfs_Unlock();
549         
550         
551         return (nRead >= 0) ? nRead : -1;
552                 
553 }
554
555 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
556 {
557         yaffsfs_Handle *h = NULL;
558         yaffs_Object *obj = NULL;
559         int pos = 0;
560         int nWritten = -1;
561         
562         yaffsfs_Lock();
563         h = yaffsfs_GetHandlePointer(fd);
564         obj = yaffsfs_GetHandleObject(fd);
565         
566         if(!h || !obj)
567         {
568                 // bad handle
569                 yaffsfs_SetError(-EBADF);               
570         }
571         else if( h && obj && h->readOnly)
572         {
573                 // todo error
574         }
575         else if( h && obj)
576         {
577                 if(h->append)
578                 {
579                         pos =  yaffs_GetObjectFileLength(obj);
580                 }
581                 else
582                 {
583                         pos = h->position;
584                 }
585                 
586                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte);
587                 
588                 if(nWritten >= 0)
589                 {
590                         h->position = pos + nWritten;
591                 }
592                 else
593                 {
594                         //todo error
595                 }
596                 
597         }
598         
599         yaffsfs_Unlock();
600         
601         
602         return (nWritten >= 0) ? nWritten : -1;
603
604 }
605
606 off_t yaffs_lseek(int fd, off_t offset, int whence) 
607 {
608         yaffsfs_Handle *h = NULL;
609         yaffs_Object *obj = NULL;
610         int pos = -1;
611         int fSize = -1;
612         
613         yaffsfs_Lock();
614         h = yaffsfs_GetHandlePointer(fd);
615         obj = yaffsfs_GetHandleObject(fd);
616         
617         if(!h || !obj)
618         {
619                 // bad handle
620                 yaffsfs_SetError(-EBADF);               
621         }
622         else if(whence == SEEK_SET)
623         {
624                 if(offset >= 0)
625                 {
626                         pos = offset;
627                 }
628         }
629         else if(whence == SEEK_CUR)
630         {
631                 if( (h->position + offset) >= 0)
632                 {
633                         pos = (h->position + offset);
634                 }
635         }
636         else if(whence == SEEK_END)
637         {
638                 fSize = yaffs_GetObjectFileLength(obj);
639                 if(fSize >= 0 && (fSize + offset) >= 0)
640                 {
641                         pos = fSize + offset;
642                 }
643         }
644         
645         if(pos >= 0)
646         {
647                 h->position = pos;
648         }
649         else
650         {
651                 // todo error
652         }
653
654         
655         yaffsfs_Unlock();
656         
657         return pos;
658 }
659
660
661 int yaffsfs_DoUnlink(const char *path,int isDirectory) 
662 {
663         yaffs_Object *dir = NULL;
664         yaffs_Object *obj = NULL;
665         char *name;
666         int result = YAFFS_FAIL;
667         
668         yaffsfs_Lock();
669
670         obj = yaffsfs_FindObject(NULL,path,0);
671         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
672         if(!dir)
673         {
674                 yaffsfs_SetError(-ENOTDIR);
675         }
676         else if(!obj)
677         {
678                 yaffsfs_SetError(-ENOENT);
679         }
680         else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
681         {
682                 yaffsfs_SetError(-EISDIR);
683         }
684         else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
685         {
686                 yaffsfs_SetError(-ENOTDIR);
687         }
688         else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
689         {
690                 yaffsfs_SetError(-ENOTDIR);
691         }
692         else
693         {
694                 result = yaffs_Unlink(dir,name);
695                 
696                 if(result == YAFFS_FAIL && isDirectory)
697                 {
698                         yaffsfs_SetError(-ENOTEMPTY);
699                 }
700         }
701         
702         yaffsfs_Unlock();
703         
704         // todo error
705         
706         return (result == YAFFS_FAIL) ? -1 : 0;
707 }
708 int yaffs_rmdir(const char *path) 
709 {
710         return yaffsfs_DoUnlink(path,1);
711 }
712
713 int yaffs_unlink(const char *path) 
714 {
715         return yaffsfs_DoUnlink(path,0);
716 }
717
718 int yaffs_rename(const char *oldPath, const char *newPath)
719 {
720         yaffs_Object *olddir = NULL;
721         yaffs_Object *newdir = NULL;
722         char *oldname;
723         char *newname;
724         int result= YAFFS_FAIL;
725         
726         yaffsfs_Lock();
727         
728         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
729         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
730         
731         if(!olddir || !newdir)
732         {
733                 // bad handle
734                 yaffsfs_SetError(-EBADF);               
735         }
736         else if(olddir->myDev != newdir->myDev)
737         {
738                 // oops must be on same device
739                 // todo error
740                 yaffsfs_SetError(-EXDEV);
741         }
742         else
743         {
744                 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
745         }
746         
747         yaffsfs_Unlock();
748         
749         return (result == YAFFS_FAIL) ? -1 : 0; 
750 }
751
752
753 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
754 {
755         int retVal = -1;
756
757         if(obj)
758         {
759                 obj = yaffs_GetEquivalentObject(obj);
760         }
761
762         if(obj && buf)
763         {
764         buf->st_dev = (int)obj->myDev->genericDevice;
765         buf->st_ino = obj->objectId;
766         buf->st_mode = obj->st_mode & ~S_IFMT; // clear out file type bits
767         
768                 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) 
769                 {
770                         buf->st_mode |= S_IFDIR;
771                 }
772                 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) 
773                 {
774                         buf->st_mode |= S_IFLNK;
775                 }
776                 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
777                 {
778                         buf->st_mode |= S_IFREG;
779                 }
780                 
781         buf->st_nlink = yaffs_GetObjectLinkCount(obj);
782         buf->st_uid = 0;    
783         buf->st_gid = 0;;     
784         buf->st_rdev = obj->st_rdev;
785         buf->st_size = yaffs_GetObjectFileLength(obj);
786                 buf->st_blksize = YAFFS_BYTES_PER_CHUNK;
787         buf->st_blocks = (buf->st_size + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
788         buf->st_atime = obj->st_atime; 
789         buf->st_ctime = obj->st_ctime; 
790         buf->st_mtime = obj->st_mtime; 
791                 retVal = 0;
792         }
793         return retVal;
794 }
795
796 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
797 {
798         yaffs_Object *obj;
799         
800         int retVal = -1;
801         
802         yaffsfs_Lock();
803         obj = yaffsfs_FindObject(NULL,path,0);
804         
805         if(!doLStat && obj)
806         {
807                 obj = yaffsfs_FollowLink(obj,0);
808         }
809         
810         if(obj)
811         {
812                 retVal = yaffsfs_DoStat(obj,buf);
813         }
814         else
815         {
816                 // todo error not found
817                 yaffsfs_SetError(-ENOENT);
818         }
819         
820         yaffsfs_Unlock();
821         
822         return retVal;
823         
824 }
825
826 int yaffs_stat(const char *path, struct yaffs_stat *buf)
827 {
828         return yaffsfs_DoStatOrLStat(path,buf,0);
829 }
830
831 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
832 {
833         return yaffsfs_DoStatOrLStat(path,buf,1);
834 }
835
836 int yaffs_fstat(int fd, struct yaffs_stat *buf)
837 {
838         yaffs_Object *obj;
839         
840         int retVal = -1;
841         
842         yaffsfs_Lock();
843         obj = yaffsfs_GetHandleObject(fd);
844         
845         if(obj)
846         {
847                 retVal = yaffsfs_DoStat(obj,buf);
848         }
849         else
850         {
851                 // bad handle
852                 yaffsfs_SetError(-EBADF);               
853         }
854         
855         yaffsfs_Unlock();
856         
857         return retVal;
858 }
859
860 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
861 {
862         int result;
863
864         if(obj)
865         {
866                 obj = yaffs_GetEquivalentObject(obj);
867         }
868         
869         if(obj)
870         {
871                 obj->st_mode = mode;
872                 obj->dirty = 1;
873                 result = yaffs_FlushFile(obj,0);
874         }
875         
876         return result == YAFFS_OK ? 0 : -1;
877 }
878
879
880 int yaffs_chmod(const char *path, mode_t mode)
881 {
882         yaffs_Object *obj;
883         
884         int retVal = -1;
885         
886         yaffsfs_Lock();
887         obj = yaffsfs_FindObject(NULL,path,0);
888         
889         if(obj)
890         {
891                 retVal = yaffsfs_DoChMod(obj,mode);
892         }
893         else
894         {
895                 // todo error not found
896                 yaffsfs_SetError(-ENOENT);
897         }
898         
899         yaffsfs_Unlock();
900         
901         return retVal;
902         
903 }
904
905
906 int yaffs_fchmod(int fd, mode_t mode)
907 {
908         yaffs_Object *obj;
909         
910         int retVal = -1;
911         
912         yaffsfs_Lock();
913         obj = yaffsfs_GetHandleObject(fd);
914         
915         if(obj)
916         {
917                 retVal = yaffsfs_DoChMod(obj,mode);
918         }
919         else
920         {
921                 // bad handle
922                 yaffsfs_SetError(-EBADF);               
923         }
924         
925         yaffsfs_Unlock();
926         
927         return retVal;
928 }
929
930
931 int yaffs_mkdir(const char *path, mode_t mode)
932 {
933         yaffs_Object *parent = NULL;
934         yaffs_Object *dir;
935         char *name;
936         int retVal= -1;
937         
938         yaffsfs_Lock();
939         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
940         dir = yaffs_MknodDirectory(parent,name,mode,0,0);
941         if(dir)
942         {
943                 retVal = 0;
944         }
945         else
946         {
947                 yaffsfs_SetError(-ENOSPC); // just assume no space for now
948                 retVal = -1;
949         }
950         
951         yaffsfs_Unlock();
952         
953         return retVal;
954 }
955
956 int yaffs_mount(const char *path)
957 {
958         int retVal=-1;
959         int result=YAFFS_FAIL;
960         yaffs_Device *dev=NULL;
961         char *dummy;
962         
963         yaffsfs_Lock();
964         dev = yaffsfs_FindDevice(path,&dummy);
965         if(dev)
966         {
967                 if(!dev->isMounted)
968                 {
969                         result = yaffs_GutsInitialise(dev);
970                         if(result == YAFFS_FAIL)
971                         {
972                                 // todo error - mount failed
973                                 yaffsfs_SetError(-ENOMEM);
974                         }
975                         retVal = result ? 0 : -1;
976                         
977                 }
978                 else
979                 {
980                         //todo error - already mounted.
981                         yaffsfs_SetError(-EBUSY);
982                 }
983         }
984         else
985         {
986                 // todo error - no device
987                 yaffsfs_SetError(-ENODEV);
988         }
989         yaffsfs_Unlock();
990         return retVal;
991         
992 }
993
994 int yaffs_unmount(const char *path)
995 {
996         int retVal=-1;
997         yaffs_Device *dev=NULL;
998         char *dummy;
999         
1000         yaffsfs_Lock();
1001         dev = yaffsfs_FindDevice(path,&dummy);
1002         if(dev)
1003         {
1004                 if(dev->isMounted)
1005                 {
1006                         int i;
1007                         int inUse;
1008                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1009                         {
1010                                 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1011                                 {
1012                                         inUse = 1; // the device is in use, can't unmount
1013                                 }
1014                         }
1015                         
1016                         if(!inUse)
1017                         {
1018                                 yaffs_Deinitialise(dev);
1019                                         
1020                                 retVal = 0;
1021                         }
1022                         else
1023                         {
1024                                 // todo error can't unmount as files are open
1025                                 yaffsfs_SetError(-EBUSY);
1026                         }
1027                         
1028                 }
1029                 else
1030                 {
1031                         //todo error - not mounted.
1032                         yaffsfs_SetError(-EINVAL);
1033                         
1034                 }
1035         }
1036         else
1037         {
1038                 // todo error - no device
1039                 yaffsfs_SetError(-ENODEV);
1040         }       
1041         yaffsfs_Unlock();
1042         return retVal;
1043         
1044 }
1045
1046 off_t yaffs_freespace(const char *path)
1047 {
1048         off_t retVal=-1;
1049         yaffs_Device *dev=NULL;
1050         char *dummy;
1051         
1052         yaffsfs_Lock();
1053         dev = yaffsfs_FindDevice(path,&dummy);
1054         if(dev)
1055         {
1056                 retVal = yaffs_GetNumberOfFreeChunks(dev);
1057                 retVal *= YAFFS_BYTES_PER_CHUNK;
1058                 
1059         }
1060         else
1061         {
1062                 yaffsfs_SetError(-EINVAL);
1063         }
1064         
1065         yaffsfs_Unlock();
1066         return retVal;  
1067 }
1068
1069 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1070 {
1071         
1072         yaffsfs_DeviceConfiguration *cfg;
1073         
1074         yaffsfs_configurationList = cfgList;
1075         
1076         yaffsfs_InitHandles();
1077         
1078         cfg = yaffsfs_configurationList;
1079         
1080         while(cfg && cfg->prefix && cfg->dev)
1081         {
1082                 cfg->dev->isMounted = 0;
1083                 cfg++;
1084         }
1085         
1086         
1087 }
1088
1089
1090 //
1091 // Directory search stuff.
1092
1093 yaffs_DIR *yaffs_opendir(const char *dirname)
1094 {
1095         yaffs_DIR *dir = NULL;
1096         yaffs_Object *obj = NULL;
1097         yaffsfs_DirectorySearchContext *dsc = NULL;
1098         
1099         yaffsfs_Lock();
1100         
1101         obj = yaffsfs_FindObject(NULL,dirname,0);
1102         
1103         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1104         {
1105                 
1106                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1107                 dir = (yaffs_DIR *)dsc;
1108                 if(dsc)
1109                 {
1110                         dsc->magic = YAFFS_MAGIC;
1111                         dsc->list = NULL;
1112                         memset(dsc->name,0,NAME_MAX+1);
1113                         strncpy(dsc->name,dirname,NAME_MAX);
1114                 }
1115         
1116         }
1117         
1118         yaffsfs_Unlock();
1119         
1120         return dir;
1121 }
1122
1123 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1124 {
1125         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1126         struct yaffs_dirent *retVal = NULL;
1127         struct list_head *i;    
1128         yaffs_Object *entry = NULL;
1129         int offset;
1130         yaffs_Object *obj = NULL;
1131         struct yaffsfs_ObjectListEntry *list = NULL;
1132         int inList = 0;
1133                 
1134         yaffsfs_Lock();
1135         
1136         offset = -1;
1137         
1138         if(dsc && dsc->magic == YAFFS_MAGIC)
1139         {
1140                 yaffsfs_SetError(0);
1141                 
1142                 obj = yaffsfs_FindObject(NULL,dsc->name,0);
1143         
1144                 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1145                 {
1146                         
1147                         list_for_each(i,&obj->variant.directoryVariant.children)
1148                         {               
1149                                         offset++;
1150                                         entry = (i) ?  list_entry(i, yaffs_Object,siblings) : NULL;
1151                         
1152                                         if(entry)
1153                                         {
1154                                                 list = dsc->list;
1155                                                 inList = 0;
1156                                                 while(list && !inList)
1157                                                 {
1158                                                         if(list->objectId == entry->objectId)
1159                                                         {
1160                                                                 inList = 1;
1161                                                         }
1162                                                         list = list->next;
1163                                                 }
1164                                                 
1165                                                 if(!inList) goto foundNew;
1166                                         }
1167                                 
1168                         }
1169                         
1170                         foundNew:
1171                         
1172                         if(!inList && entry)
1173                         {
1174                                 //This is the entry we're going to return;
1175                                 struct yaffsfs_ObjectListEntry *le;
1176                                 
1177                                 le = YMALLOC(sizeof(struct yaffsfs_ObjectListEntry));
1178                                 
1179                                 if(le)
1180                                 {
1181                                         le->next =  dsc->list;
1182                                         le->objectId = entry->objectId;
1183                                         dsc->list = le;
1184                                         
1185                                         dsc->de.d_ino = yaffs_GetEquivalentObject(entry)->objectId;
1186                                         dsc->de.d_off = offset;
1187                                         yaffs_GetObjectName(entry,dsc->de.d_name,NAME_MAX+1);
1188                                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1189                                         
1190                                         retVal = &dsc->de;
1191                                 }
1192                                 
1193                         }
1194                 }
1195
1196         }
1197         else
1198         {
1199                 yaffsfs_SetError(-EBADF);
1200         }
1201         
1202         yaffsfs_Unlock();
1203         
1204         return retVal;
1205         
1206 }
1207
1208 void yaffsfs_ListClear(yaffsfs_DirectorySearchContext *dsc) 
1209 {
1210         
1211         struct yaffsfs_ObjectListEntry *le;
1212         
1213         if(dsc && dsc->magic == YAFFS_MAGIC)
1214         {
1215                 while(dsc->list)
1216                 {
1217                         le = dsc->list;
1218                         dsc->list = dsc->list->next;
1219                         YFREE(le);
1220                 }
1221         }
1222         
1223 }
1224
1225 void yaffs_rewinddir(yaffs_DIR *dirp)
1226 {
1227         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1228                 
1229         yaffsfs_Lock();
1230         yaffsfs_ListClear(dsc);
1231         yaffsfs_Unlock();
1232 }
1233
1234
1235 int yaffs_closedir(yaffs_DIR *dirp)
1236 {
1237         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1238                 
1239         yaffsfs_Lock();
1240         yaffsfs_ListClear(dsc);
1241         dsc->magic = 0;
1242         YFREE(dsc);
1243         yaffsfs_Unlock();
1244         return 0;
1245 }
1246
1247
1248
1249 int yaffs_symlink(const char *oldpath, const char *newpath)
1250 {
1251         yaffs_Object *parent = NULL;
1252         yaffs_Object *obj;
1253         char *name;
1254         int retVal= -1;
1255         int mode = 0; // ignore for now
1256         
1257         yaffsfs_Lock();
1258         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1259         obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1260         if(obj)
1261         {
1262                 retVal = 0;
1263         }
1264         else
1265         {
1266                 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1267                 retVal = -1;
1268         }
1269         
1270         yaffsfs_Unlock();
1271         
1272         return retVal;
1273         
1274 }
1275
1276 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1277 {
1278         yaffs_Object *obj = NULL;
1279         int retVal;
1280
1281                 
1282         yaffsfs_Lock();
1283         
1284         obj = yaffsfs_FindObject(NULL,path,0);
1285         
1286         if(!obj)
1287         {
1288                 yaffsfs_SetError(-ENOENT);
1289                 retVal = -1;
1290         }
1291         else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1292         {
1293                 yaffsfs_SetError(-EINVAL);
1294                 retVal = -1;
1295         }
1296         else
1297         {
1298                 char *alias = obj->variant.symLinkVariant.alias;
1299                 memset(buf,0,bufsiz);
1300                 strncpy(buf,alias,bufsiz - 1);
1301                 retVal = 0;
1302         }
1303         yaffsfs_Unlock();
1304         return retVal;
1305 }
1306
1307 int yaffs_link(const char *oldpath, const char *newpath); 
1308 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1309
1310 int yaffs_DumpDevStruct(const char *path)
1311 {
1312         char *rest;
1313         
1314         yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1315         
1316         if(obj)
1317         {
1318                 yaffs_Device *dev = obj->myDev;
1319                 
1320                 printf("\n"
1321                            "nPageWrites.......... %d\n"
1322                            "nPageReads........... %d\n"
1323                            "nBlockErasures....... %d\n"
1324                            "nGCCopies............ %d\n"
1325                            "garbageCollections... %d\n"
1326                            "passiveGarbageColl'ns %d\n"
1327                            "\n",
1328                                 dev->nPageWrites,
1329                                 dev->nPageReads,
1330                                 dev->nBlockErasures,
1331                                 dev->nGCCopies,
1332                                 dev->garbageCollections,
1333                                 dev->passiveGarbageCollections
1334                 );
1335         }
1336 }
1337