Change yboot.c to LGPL as was intended
[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.7 2004-11-21 23:33:30 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 && (!*leftOver || *leftOver == '/'))
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         int errorReported = 0;
352         
353         int i;
354         
355         
356         // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
357         
358         
359         yaffsfs_Lock();
360         
361         handle = yaffsfs_GetHandle();
362         
363         if(handle >= 0)
364         {
365
366                 h = yaffsfs_GetHandlePointer(handle);
367         
368         
369                 // try to find the exisiting object
370                 obj = yaffsfs_FindObject(NULL,path,0);
371                 
372                 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
373                 {
374                 
375                         obj = yaffsfs_FollowLink(obj,symDepth++);
376                 }
377
378                 if(obj)
379                 {
380                         // Check if the object is already in use
381                         alreadyOpen = alreadyExclusive = 0;
382                         
383                         for(i = 0; i <= YAFFSFS_N_HANDLES; i++)
384                         {
385                                 
386                                 if(i != handle &&
387                                    yaffsfs_handle[i].inUse &&
388                                     obj == yaffsfs_handle[i].obj)
389                                  {
390                                         alreadyOpen = 1;
391                                         if(yaffsfs_handle[i].exclusive)
392                                         {
393                                                 alreadyExclusive = 1;
394                                         }
395                                  }
396                         }
397
398                         if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
399                         {
400                                 openDenied = 1;
401                         }
402                         
403                         // Open should fail if O_CREAT and O_EXCL are specified
404                         if((oflag & O_EXCL) && (oflag & O_CREAT))
405                         {
406                                 openDenied = 1;
407                                 yaffsfs_SetError(-EEXIST);
408                                 errorReported = 1;
409                         }
410                         
411                         // Check file permissions
412                         if( (oflag & (O_RDWR | O_WRONLY)) == 0 &&     // ie O_RDONLY
413                            !(obj->st_mode & S_IREAD))
414                         {
415                                 openDenied = 1;
416                         }
417
418                         if( (oflag & O_RDWR) && 
419                            !(obj->st_mode & S_IREAD))
420                         {
421                                 openDenied = 1;
422                         }
423
424                         if( (oflag & (O_RDWR | O_WRONLY)) && 
425                            !(obj->st_mode & S_IWRITE))
426                         {
427                                 openDenied = 1;
428                         }
429                         
430                 }
431                 
432                 else if((oflag & O_CREAT))
433                 {
434                         // Let's see if we can create this file
435                         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
436                         if(dir)
437                         {
438                                 obj = yaffs_MknodFile(dir,name,mode,0,0);       
439                         }
440                         else
441                         {
442                                 yaffsfs_SetError(-ENOTDIR);
443                         }
444                 }
445                 
446                 if(obj && !openDenied)
447                 {
448                         h->obj = obj;
449                         h->inUse = 1;
450                 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
451                         h->append =  (oflag & O_APPEND) ? 1 : 0;
452                         h->exclusive = (oflag & O_EXCL) ? 1 : 0;
453                         h->position = 0;
454                         
455                         obj->inUse++;
456                         if((oflag & O_TRUNC) && !h->readOnly)
457                         {
458                                 //todo truncate
459                                 yaffs_ResizeFile(obj,0);
460                         }
461                         
462                 }
463                 else
464                 {
465                         yaffsfs_PutHandle(handle);
466                         if(!errorReported)
467                         {
468                                 yaffsfs_SetError(-EACCESS);
469                                 errorReported = 1;
470                         }
471                         handle = -1;
472                 }
473                 
474         }
475         
476         yaffsfs_Unlock();
477         
478         return handle;          
479 }
480
481 int yaffs_close(int fd)
482 {
483         yaffsfs_Handle *h = NULL;
484         int retVal = 0;
485         
486         yaffsfs_Lock();
487
488         h = yaffsfs_GetHandlePointer(fd);
489         
490         if(h && h->inUse)
491         {
492                 // clean up
493                 yaffs_FlushFile(h->obj,1);
494                 h->obj->inUse--;
495                 if(h->obj->inUse <= 0 && h->obj->unlinked)
496                 {
497                         yaffs_DeleteFile(h->obj);
498                 }
499                 yaffsfs_PutHandle(fd);
500                 retVal = 0;
501         }
502         else
503         {
504                 // bad handle
505                 yaffsfs_SetError(-EBADF);               
506                 retVal = -1;
507         }
508         
509         yaffsfs_Unlock();
510         
511         return retVal;
512 }
513
514 int yaffs_read(int fd, void *buf, unsigned int nbyte)
515 {
516         yaffsfs_Handle *h = NULL;
517         yaffs_Object *obj = NULL;
518         int pos = 0;
519         int nRead = -1;
520         int maxRead;
521         
522         yaffsfs_Lock();
523         h = yaffsfs_GetHandlePointer(fd);
524         obj = yaffsfs_GetHandleObject(fd);
525         
526         if(!h || !obj)
527         {
528                 // bad handle
529                 yaffsfs_SetError(-EBADF);               
530         }
531         else if( h && obj)
532         {
533                 pos=  h->position;
534                 if(yaffs_GetObjectFileLength(obj) > pos)
535                 {
536                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
537                 }
538                 else
539                 {
540                         maxRead = 0;
541                 }
542
543                 if(nbyte > maxRead)
544                 {
545                         nbyte = maxRead;
546                 }
547
548                 
549                 if(nbyte > 0)
550                 {
551                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
552                         if(nRead >= 0)
553                         {
554                                 h->position = pos + nRead;
555                         }
556                         else
557                         {
558                                 //todo error
559                         }
560                 }
561                 else
562                 {
563                         nRead = 0;
564                 }
565                 
566         }
567         
568         yaffsfs_Unlock();
569         
570         
571         return (nRead >= 0) ? nRead : -1;
572                 
573 }
574
575 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
576 {
577         yaffsfs_Handle *h = NULL;
578         yaffs_Object *obj = NULL;
579         int pos = 0;
580         int nWritten = -1;
581         
582         yaffsfs_Lock();
583         h = yaffsfs_GetHandlePointer(fd);
584         obj = yaffsfs_GetHandleObject(fd);
585         
586         if(!h || !obj)
587         {
588                 // bad handle
589                 yaffsfs_SetError(-EBADF);               
590         }
591         else if( h && obj && h->readOnly)
592         {
593                 // todo error
594         }
595         else if( h && obj)
596         {
597                 if(h->append)
598                 {
599                         pos =  yaffs_GetObjectFileLength(obj);
600                 }
601                 else
602                 {
603                         pos = h->position;
604                 }
605                 
606                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte);
607                 
608                 if(nWritten >= 0)
609                 {
610                         h->position = pos + nWritten;
611                 }
612                 else
613                 {
614                         //todo error
615                 }
616                 
617         }
618         
619         yaffsfs_Unlock();
620         
621         
622         return (nWritten >= 0) ? nWritten : -1;
623
624 }
625
626 off_t yaffs_lseek(int fd, off_t offset, int whence) 
627 {
628         yaffsfs_Handle *h = NULL;
629         yaffs_Object *obj = NULL;
630         int pos = -1;
631         int fSize = -1;
632         
633         yaffsfs_Lock();
634         h = yaffsfs_GetHandlePointer(fd);
635         obj = yaffsfs_GetHandleObject(fd);
636         
637         if(!h || !obj)
638         {
639                 // bad handle
640                 yaffsfs_SetError(-EBADF);               
641         }
642         else if(whence == SEEK_SET)
643         {
644                 if(offset >= 0)
645                 {
646                         pos = offset;
647                 }
648         }
649         else if(whence == SEEK_CUR)
650         {
651                 if( (h->position + offset) >= 0)
652                 {
653                         pos = (h->position + offset);
654                 }
655         }
656         else if(whence == SEEK_END)
657         {
658                 fSize = yaffs_GetObjectFileLength(obj);
659                 if(fSize >= 0 && (fSize + offset) >= 0)
660                 {
661                         pos = fSize + offset;
662                 }
663         }
664         
665         if(pos >= 0)
666         {
667                 h->position = pos;
668         }
669         else
670         {
671                 // todo error
672         }
673
674         
675         yaffsfs_Unlock();
676         
677         return pos;
678 }
679
680
681 int yaffsfs_DoUnlink(const char *path,int isDirectory) 
682 {
683         yaffs_Object *dir = NULL;
684         yaffs_Object *obj = NULL;
685         char *name;
686         int result = YAFFS_FAIL;
687         
688         yaffsfs_Lock();
689
690         obj = yaffsfs_FindObject(NULL,path,0);
691         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
692         if(!dir)
693         {
694                 yaffsfs_SetError(-ENOTDIR);
695         }
696         else if(!obj)
697         {
698                 yaffsfs_SetError(-ENOENT);
699         }
700         else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
701         {
702                 yaffsfs_SetError(-EISDIR);
703         }
704         else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
705         {
706                 yaffsfs_SetError(-ENOTDIR);
707         }
708         else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
709         {
710                 yaffsfs_SetError(-ENOTDIR);
711         }
712         else
713         {
714                 result = yaffs_Unlink(dir,name);
715                 
716                 if(result == YAFFS_FAIL && isDirectory)
717                 {
718                         yaffsfs_SetError(-ENOTEMPTY);
719                 }
720         }
721         
722         yaffsfs_Unlock();
723         
724         // todo error
725         
726         return (result == YAFFS_FAIL) ? -1 : 0;
727 }
728 int yaffs_rmdir(const char *path) 
729 {
730         return yaffsfs_DoUnlink(path,1);
731 }
732
733 int yaffs_unlink(const char *path) 
734 {
735         return yaffsfs_DoUnlink(path,0);
736 }
737
738 int yaffs_rename(const char *oldPath, const char *newPath)
739 {
740         yaffs_Object *olddir = NULL;
741         yaffs_Object *newdir = NULL;
742         yaffs_Object *obj = NULL;
743         char *oldname;
744         char *newname;
745         int result= YAFFS_FAIL;
746         int renameAllowed = 1;
747         
748         yaffsfs_Lock();
749         
750         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
751         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
752         obj = yaffsfs_FindObject(NULL,oldPath,0);
753         
754         if(!olddir || !newdir || !obj)
755         {
756                 // bad file
757                 yaffsfs_SetError(-EBADF);       
758                 renameAllowed = 0;      
759         }
760         else if(olddir->myDev != newdir->myDev)
761         {
762                 // oops must be on same device
763                 // todo error
764                 yaffsfs_SetError(-EXDEV);
765                 renameAllowed = 0;      
766         }
767         else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
768         {
769                 // It is a directory, check that it is not being renamed to 
770                 // being its own decendent.
771                 // Do this by tracing from the new directory back to the root, checking for obj
772                 
773                 yaffs_Object *xx = newdir;
774                 
775                 while( renameAllowed && xx)
776                 {
777                         if(xx == obj)
778                         {
779                                 renameAllowed = 0;
780                         }
781                         xx = xx->parent;
782                 }
783                 if(!renameAllowed) yaffsfs_SetError(-EACCESS);
784         }
785         
786         if(renameAllowed)
787         {
788                 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
789         }
790         
791         yaffsfs_Unlock();
792         
793         return (result == YAFFS_FAIL) ? -1 : 0; 
794 }
795
796
797 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
798 {
799         int retVal = -1;
800
801         if(obj)
802         {
803                 obj = yaffs_GetEquivalentObject(obj);
804         }
805
806         if(obj && buf)
807         {
808         buf->st_dev = (int)obj->myDev->genericDevice;
809         buf->st_ino = obj->objectId;
810         buf->st_mode = obj->st_mode & ~S_IFMT; // clear out file type bits
811         
812                 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) 
813                 {
814                         buf->st_mode |= S_IFDIR;
815                 }
816                 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) 
817                 {
818                         buf->st_mode |= S_IFLNK;
819                 }
820                 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
821                 {
822                         buf->st_mode |= S_IFREG;
823                 }
824                 
825         buf->st_nlink = yaffs_GetObjectLinkCount(obj);
826         buf->st_uid = 0;    
827         buf->st_gid = 0;;     
828         buf->st_rdev = obj->st_rdev;
829         buf->st_size = yaffs_GetObjectFileLength(obj);
830                 buf->st_blksize = YAFFS_BYTES_PER_CHUNK;
831         buf->st_blocks = (buf->st_size + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
832         buf->st_atime = obj->st_atime; 
833         buf->st_ctime = obj->st_ctime; 
834         buf->st_mtime = obj->st_mtime; 
835                 retVal = 0;
836         }
837         return retVal;
838 }
839
840 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
841 {
842         yaffs_Object *obj;
843         
844         int retVal = -1;
845         
846         yaffsfs_Lock();
847         obj = yaffsfs_FindObject(NULL,path,0);
848         
849         if(!doLStat && obj)
850         {
851                 obj = yaffsfs_FollowLink(obj,0);
852         }
853         
854         if(obj)
855         {
856                 retVal = yaffsfs_DoStat(obj,buf);
857         }
858         else
859         {
860                 // todo error not found
861                 yaffsfs_SetError(-ENOENT);
862         }
863         
864         yaffsfs_Unlock();
865         
866         return retVal;
867         
868 }
869
870 int yaffs_stat(const char *path, struct yaffs_stat *buf)
871 {
872         return yaffsfs_DoStatOrLStat(path,buf,0);
873 }
874
875 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
876 {
877         return yaffsfs_DoStatOrLStat(path,buf,1);
878 }
879
880 int yaffs_fstat(int fd, struct yaffs_stat *buf)
881 {
882         yaffs_Object *obj;
883         
884         int retVal = -1;
885         
886         yaffsfs_Lock();
887         obj = yaffsfs_GetHandleObject(fd);
888         
889         if(obj)
890         {
891                 retVal = yaffsfs_DoStat(obj,buf);
892         }
893         else
894         {
895                 // bad handle
896                 yaffsfs_SetError(-EBADF);               
897         }
898         
899         yaffsfs_Unlock();
900         
901         return retVal;
902 }
903
904 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
905 {
906         int result;
907
908         if(obj)
909         {
910                 obj = yaffs_GetEquivalentObject(obj);
911         }
912         
913         if(obj)
914         {
915                 obj->st_mode = mode;
916                 obj->dirty = 1;
917                 result = yaffs_FlushFile(obj,0);
918         }
919         
920         return result == YAFFS_OK ? 0 : -1;
921 }
922
923
924 int yaffs_chmod(const char *path, mode_t mode)
925 {
926         yaffs_Object *obj;
927         
928         int retVal = -1;
929         
930         yaffsfs_Lock();
931         obj = yaffsfs_FindObject(NULL,path,0);
932         
933         if(obj)
934         {
935                 retVal = yaffsfs_DoChMod(obj,mode);
936         }
937         else
938         {
939                 // todo error not found
940                 yaffsfs_SetError(-ENOENT);
941         }
942         
943         yaffsfs_Unlock();
944         
945         return retVal;
946         
947 }
948
949
950 int yaffs_fchmod(int fd, mode_t mode)
951 {
952         yaffs_Object *obj;
953         
954         int retVal = -1;
955         
956         yaffsfs_Lock();
957         obj = yaffsfs_GetHandleObject(fd);
958         
959         if(obj)
960         {
961                 retVal = yaffsfs_DoChMod(obj,mode);
962         }
963         else
964         {
965                 // bad handle
966                 yaffsfs_SetError(-EBADF);               
967         }
968         
969         yaffsfs_Unlock();
970         
971         return retVal;
972 }
973
974
975 int yaffs_mkdir(const char *path, mode_t mode)
976 {
977         yaffs_Object *parent = NULL;
978         yaffs_Object *dir;
979         char *name;
980         int retVal= -1;
981         
982         yaffsfs_Lock();
983         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
984         dir = yaffs_MknodDirectory(parent,name,mode,0,0);
985         if(dir)
986         {
987                 retVal = 0;
988         }
989         else
990         {
991                 yaffsfs_SetError(-ENOSPC); // just assume no space for now
992                 retVal = -1;
993         }
994         
995         yaffsfs_Unlock();
996         
997         return retVal;
998 }
999
1000 int yaffs_mount(const char *path)
1001 {
1002         int retVal=-1;
1003         int result=YAFFS_FAIL;
1004         yaffs_Device *dev=NULL;
1005         char *dummy;
1006         
1007         yaffsfs_Lock();
1008         dev = yaffsfs_FindDevice(path,&dummy);
1009         if(dev)
1010         {
1011                 if(!dev->isMounted)
1012                 {
1013                         result = yaffs_GutsInitialise(dev);
1014                         if(result == YAFFS_FAIL)
1015                         {
1016                                 // todo error - mount failed
1017                                 yaffsfs_SetError(-ENOMEM);
1018                         }
1019                         retVal = result ? 0 : -1;
1020                         
1021                 }
1022                 else
1023                 {
1024                         //todo error - already mounted.
1025                         yaffsfs_SetError(-EBUSY);
1026                 }
1027         }
1028         else
1029         {
1030                 // todo error - no device
1031                 yaffsfs_SetError(-ENODEV);
1032         }
1033         yaffsfs_Unlock();
1034         return retVal;
1035         
1036 }
1037
1038 int yaffs_unmount(const char *path)
1039 {
1040         int retVal=-1;
1041         yaffs_Device *dev=NULL;
1042         char *dummy;
1043         
1044         yaffsfs_Lock();
1045         dev = yaffsfs_FindDevice(path,&dummy);
1046         if(dev)
1047         {
1048                 if(dev->isMounted)
1049                 {
1050                         int i;
1051                         int inUse;
1052                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1053                         {
1054                                 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1055                                 {
1056                                         inUse = 1; // the device is in use, can't unmount
1057                                 }
1058                         }
1059                         
1060                         if(!inUse)
1061                         {
1062                                 yaffs_Deinitialise(dev);
1063                                         
1064                                 retVal = 0;
1065                         }
1066                         else
1067                         {
1068                                 // todo error can't unmount as files are open
1069                                 yaffsfs_SetError(-EBUSY);
1070                         }
1071                         
1072                 }
1073                 else
1074                 {
1075                         //todo error - not mounted.
1076                         yaffsfs_SetError(-EINVAL);
1077                         
1078                 }
1079         }
1080         else
1081         {
1082                 // todo error - no device
1083                 yaffsfs_SetError(-ENODEV);
1084         }       
1085         yaffsfs_Unlock();
1086         return retVal;
1087         
1088 }
1089
1090 off_t yaffs_freespace(const char *path)
1091 {
1092         off_t retVal=-1;
1093         yaffs_Device *dev=NULL;
1094         char *dummy;
1095         
1096         yaffsfs_Lock();
1097         dev = yaffsfs_FindDevice(path,&dummy);
1098         if(dev)
1099         {
1100                 retVal = yaffs_GetNumberOfFreeChunks(dev);
1101                 retVal *= YAFFS_BYTES_PER_CHUNK;
1102                 
1103         }
1104         else
1105         {
1106                 yaffsfs_SetError(-EINVAL);
1107         }
1108         
1109         yaffsfs_Unlock();
1110         return retVal;  
1111 }
1112
1113 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1114 {
1115         
1116         yaffsfs_DeviceConfiguration *cfg;
1117         
1118         yaffsfs_configurationList = cfgList;
1119         
1120         yaffsfs_InitHandles();
1121         
1122         cfg = yaffsfs_configurationList;
1123         
1124         while(cfg && cfg->prefix && cfg->dev)
1125         {
1126                 cfg->dev->isMounted = 0;
1127                 cfg++;
1128         }
1129         
1130         
1131 }
1132
1133
1134 //
1135 // Directory search stuff.
1136
1137 yaffs_DIR *yaffs_opendir(const char *dirname)
1138 {
1139         yaffs_DIR *dir = NULL;
1140         yaffs_Object *obj = NULL;
1141         yaffsfs_DirectorySearchContext *dsc = NULL;
1142         
1143         yaffsfs_Lock();
1144         
1145         obj = yaffsfs_FindObject(NULL,dirname,0);
1146         
1147         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1148         {
1149                 
1150                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1151                 dir = (yaffs_DIR *)dsc;
1152                 if(dsc)
1153                 {
1154                         dsc->magic = YAFFS_MAGIC;
1155                         dsc->list = NULL;
1156                         memset(dsc->name,0,NAME_MAX+1);
1157                         strncpy(dsc->name,dirname,NAME_MAX);
1158                 }
1159         
1160         }
1161         
1162         yaffsfs_Unlock();
1163         
1164         return dir;
1165 }
1166
1167 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1168 {
1169         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1170         struct yaffs_dirent *retVal = NULL;
1171         struct list_head *i;    
1172         yaffs_Object *entry = NULL;
1173         int offset;
1174         yaffs_Object *obj = NULL;
1175         struct yaffsfs_ObjectListEntry *list = NULL;
1176         int inList = 0;
1177                 
1178         yaffsfs_Lock();
1179         
1180         offset = -1;
1181         
1182         if(dsc && dsc->magic == YAFFS_MAGIC)
1183         {
1184                 yaffsfs_SetError(0);
1185                 
1186                 obj = yaffsfs_FindObject(NULL,dsc->name,0);
1187         
1188                 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1189                 {
1190                         
1191                         list_for_each(i,&obj->variant.directoryVariant.children)
1192                         {               
1193                                         offset++;
1194                                         entry = (i) ?  list_entry(i, yaffs_Object,siblings) : NULL;
1195                         
1196                                         if(entry)
1197                                         {
1198                                                 list = dsc->list;
1199                                                 inList = 0;
1200                                                 while(list && !inList)
1201                                                 {
1202                                                         if(list->objectId == entry->objectId)
1203                                                         {
1204                                                                 inList = 1;
1205                                                         }
1206                                                         list = list->next;
1207                                                 }
1208                                                 
1209                                                 if(!inList) goto foundNew;
1210                                         }
1211                                 
1212                         }
1213                         
1214                         foundNew:
1215                         
1216                         if(!inList && entry)
1217                         {
1218                                 //This is the entry we're going to return;
1219                                 struct yaffsfs_ObjectListEntry *le;
1220                                 
1221                                 le = YMALLOC(sizeof(struct yaffsfs_ObjectListEntry));
1222                                 
1223                                 if(le)
1224                                 {
1225                                         le->next =  dsc->list;
1226                                         le->objectId = entry->objectId;
1227                                         dsc->list = le;
1228                                         
1229                                         dsc->de.d_ino = yaffs_GetEquivalentObject(entry)->objectId;
1230                                         dsc->de.d_off = offset;
1231                                         yaffs_GetObjectName(entry,dsc->de.d_name,NAME_MAX+1);
1232                                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1233                                         
1234                                         retVal = &dsc->de;
1235                                 }
1236                                 
1237                         }
1238                 }
1239
1240         }
1241         else
1242         {
1243                 yaffsfs_SetError(-EBADF);
1244         }
1245         
1246         yaffsfs_Unlock();
1247         
1248         return retVal;
1249         
1250 }
1251
1252 void yaffsfs_ListClear(yaffsfs_DirectorySearchContext *dsc) 
1253 {
1254         
1255         struct yaffsfs_ObjectListEntry *le;
1256         
1257         if(dsc && dsc->magic == YAFFS_MAGIC)
1258         {
1259                 while(dsc->list)
1260                 {
1261                         le = dsc->list;
1262                         dsc->list = dsc->list->next;
1263                         YFREE(le);
1264                 }
1265         }
1266         
1267 }
1268
1269 void yaffs_rewinddir(yaffs_DIR *dirp)
1270 {
1271         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1272                 
1273         yaffsfs_Lock();
1274         yaffsfs_ListClear(dsc);
1275         yaffsfs_Unlock();
1276 }
1277
1278
1279 int yaffs_closedir(yaffs_DIR *dirp)
1280 {
1281         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1282                 
1283         yaffsfs_Lock();
1284         yaffsfs_ListClear(dsc);
1285         dsc->magic = 0;
1286         YFREE(dsc);
1287         yaffsfs_Unlock();
1288         return 0;
1289 }
1290
1291
1292
1293 int yaffs_symlink(const char *oldpath, const char *newpath)
1294 {
1295         yaffs_Object *parent = NULL;
1296         yaffs_Object *obj;
1297         char *name;
1298         int retVal= -1;
1299         int mode = 0; // ignore for now
1300         
1301         yaffsfs_Lock();
1302         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1303         obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1304         if(obj)
1305         {
1306                 retVal = 0;
1307         }
1308         else
1309         {
1310                 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1311                 retVal = -1;
1312         }
1313         
1314         yaffsfs_Unlock();
1315         
1316         return retVal;
1317         
1318 }
1319
1320 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1321 {
1322         yaffs_Object *obj = NULL;
1323         int retVal;
1324
1325                 
1326         yaffsfs_Lock();
1327         
1328         obj = yaffsfs_FindObject(NULL,path,0);
1329         
1330         if(!obj)
1331         {
1332                 yaffsfs_SetError(-ENOENT);
1333                 retVal = -1;
1334         }
1335         else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1336         {
1337                 yaffsfs_SetError(-EINVAL);
1338                 retVal = -1;
1339         }
1340         else
1341         {
1342                 char *alias = obj->variant.symLinkVariant.alias;
1343                 memset(buf,0,bufsiz);
1344                 strncpy(buf,alias,bufsiz - 1);
1345                 retVal = 0;
1346         }
1347         yaffsfs_Unlock();
1348         return retVal;
1349 }
1350
1351 int yaffs_link(const char *oldpath, const char *newpath); 
1352 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1353
1354 int yaffs_DumpDevStruct(const char *path)
1355 {
1356         char *rest;
1357         
1358         yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1359         
1360         if(obj)
1361         {
1362                 yaffs_Device *dev = obj->myDev;
1363                 
1364                 printf("\n"
1365                            "nPageWrites.......... %d\n"
1366                            "nPageReads........... %d\n"
1367                            "nBlockErasures....... %d\n"
1368                            "nGCCopies............ %d\n"
1369                            "garbageCollections... %d\n"
1370                            "passiveGarbageColl'ns %d\n"
1371                            "\n",
1372                                 dev->nPageWrites,
1373                                 dev->nPageReads,
1374                                 dev->nBlockErasures,
1375                                 dev->nGCCopies,
1376                                 dev->garbageCollections,
1377                                 dev->passiveGarbageCollections
1378                 );
1379         }
1380         return 0;
1381 }
1382