c6eb37bebe3f6708787c6387c4fd0eac83cdfc24
[yaffs2.git] / direct / yaffsfs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
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 #include "yaffsfs.h"
15 #include "yaffs_guts.h"
16 #include "yaffscfg.h"
17 #include <string.h> // for memset
18 #include "yportenv.h"
19
20 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
21
22 #ifndef NULL
23 #define NULL ((void *)0)
24 #endif
25
26
27 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.27 2009-10-08 01:57:59 charles Exp $";
28
29 // configurationList is the list of devices that are supported
30 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
31
32
33 /* Some forward references */
34 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path, int symDepth);
35 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
36
37
38 // Handle management.
39 //
40
41
42 unsigned int yaffs_wr_attempts;
43
44 typedef struct
45 {
46         __u8  inUse:1;          // this handle is in use
47         __u8  readOnly:1;       // this handle is read only
48         __u8  append:1;         // append only
49         __u8  exclusive:1;      // exclusive
50         __u32 position;         // current position in file
51         yaffs_Object *obj;      // the object
52 }yaffsfs_Handle;
53
54
55 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
56
57 // yaffsfs_InitHandle
58 /// Inilitalise handles on start-up.
59 //
60 static int yaffsfs_InitHandles(void)
61 {
62         int i;
63         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
64         {
65                 yaffsfs_handle[i].inUse = 0;
66                 yaffsfs_handle[i].obj = NULL;
67         }
68         return 0;
69 }
70
71 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
72 {
73         if(h < 0 || h >= YAFFSFS_N_HANDLES)
74         {
75                 return NULL;
76         }
77
78         return &yaffsfs_handle[h];
79 }
80
81 yaffs_Object *yaffsfs_GetHandleObject(int handle)
82 {
83         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
84
85         if(h && h->inUse)
86         {
87                 return h->obj;
88         }
89
90         return NULL;
91 }
92
93
94 //yaffsfs_GetHandle
95 // Grab a handle (when opening a file)
96 //
97
98 static int yaffsfs_GetHandle(void)
99 {
100         int i;
101         yaffsfs_Handle *h;
102
103         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
104         {
105                 h = yaffsfs_GetHandlePointer(i);
106                 if(!h)
107                 {
108                         // todo bug: should never happen
109                 }
110                 if(!h->inUse)
111                 {
112                         memset(h,0,sizeof(yaffsfs_Handle));
113                         h->inUse=1;
114                         return i;
115                 }
116         }
117         return -1;
118 }
119
120 // yaffs_PutHandle
121 // Let go of a handle (when closing a file)
122 //
123 static int yaffsfs_PutHandle(int handle)
124 {
125         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
126
127         if(h)
128         {
129                 h->inUse = 0;
130                 h->obj = NULL;
131         }
132         return 0;
133 }
134
135
136
137 // Stuff to search for a directory from a path
138
139
140 int yaffsfs_Match(YCHAR a, YCHAR b)
141 {
142         // case sensitive
143         return (a == b);
144 }
145
146 int yaffsfs_IsPathDivider(YCHAR ch)
147 {
148         YCHAR *str = YAFFS_PATH_DIVIDERS;
149
150         while(*str){
151                 if(*str == ch)
152                         return 1;
153                 str++;
154         }
155
156         return 0;
157 }
158
159 // yaffsfs_FindDevice
160 // yaffsfs_FindRoot
161 // Scan the configuration list to find the root.
162 // Curveballs: Should match paths that end in '/' too
163 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
164 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
165 {
166         yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
167         const YCHAR *leftOver;
168         const YCHAR *p;
169         yaffs_Device *retval = NULL;
170         int thisMatchLength;
171         int longestMatch = -1;
172         int matching;
173
174         // Check all configs, choose the one that:
175         // 1) Actually matches a prefix (ie /a amd /abc will not match
176         // 2) Matches the longest.
177         while(cfg && cfg->prefix && cfg->dev)
178         {
179                 leftOver = path;
180                 p = cfg->prefix;
181                 thisMatchLength = 0;
182                 matching = 1;
183
184
185                 while(matching && *p && *leftOver){
186                         // Skip over any /s
187                         while(yaffsfs_IsPathDivider(*p))
188                               p++;
189
190                         // Skip over any /s
191                         while(yaffsfs_IsPathDivider(*leftOver))
192                               leftOver++;
193
194                         // Now match the text part
195                         while(matching &&
196                               *p && !yaffsfs_IsPathDivider(*p) &&
197                               *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
198                                 if(yaffsfs_Match(*p,*leftOver)){
199                                         p++;
200                                         leftOver++;
201                                         thisMatchLength++;
202                                 } else {
203                                         matching = 0;
204                                 }
205                         }
206                 }
207
208                 // Skip over any /s in leftOver
209                 while(yaffsfs_IsPathDivider(*leftOver))
210                       leftOver++;
211                 
212
213                 if( matching && (thisMatchLength > longestMatch))
214                 {
215                         // Matched prefix
216                         *restOfPath = (YCHAR *)leftOver;
217                         retval = cfg->dev;
218                         longestMatch = thisMatchLength;
219                 }
220
221                 cfg++;
222         }
223         return retval;
224 }
225
226 #if 0
227 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
228 {
229         yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
230         const YCHAR *leftOver;
231         const YCHAR *p;
232         yaffs_Device *retval = NULL;
233         int thisMatchLength;
234         int longestMatch = -1;
235
236         // Check all configs, choose the one that:
237         // 1) Actually matches a prefix (ie /a amd /abc will not match
238         // 2) Matches the longest.
239         while(cfg && cfg->prefix && cfg->dev)
240         {
241                 leftOver = path;
242                 p = cfg->prefix;
243                 thisMatchLength = 0;
244
245                 while(*p &&  //unmatched part of prefix
246                       !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
247                       *leftOver &&
248                       yaffsfs_Match(*p,*leftOver))
249                 {
250                         p++;
251                         leftOver++;
252                         thisMatchLength++;
253                 }
254
255
256                 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) &&      // end of prefix
257                    (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
258                    (thisMatchLength > longestMatch))
259                 {
260                         // Matched prefix
261                         *restOfPath = (YCHAR *)leftOver;
262                         retval = cfg->dev;
263                         longestMatch = thisMatchLength;
264                 }
265                 cfg++;
266         }
267         return retval;
268 }
269 #endif
270
271 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
272 {
273
274         yaffs_Device *dev;
275
276         dev= yaffsfs_FindDevice(path,restOfPath);
277         if(dev && dev->isMounted)
278         {
279                 return dev->rootDir;
280         }
281         return NULL;
282 }
283
284 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
285 {
286
287         while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
288         {
289                 YCHAR *alias = obj->variant.symLinkVariant.alias;
290
291                 if(yaffsfs_IsPathDivider(*alias))
292                 {
293                         // Starts with a /, need to scan from root up
294                         obj = yaffsfs_FindObject(NULL,alias,symDepth++);
295                 }
296                 else
297                 {
298                         // Relative to here, so use the parent of the symlink as a start
299                         obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
300                 }
301         }
302         return obj;
303 }
304
305
306 // yaffsfs_FindDirectory
307 // Parse a path to determine the directory and the name within the directory.
308 //
309 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
310 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
311 {
312         yaffs_Object *dir;
313         YCHAR *restOfPath;
314         YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
315         int i;
316
317         if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
318         {
319                 return NULL;
320         }
321
322         if(startDir)
323         {
324                 dir = startDir;
325                 restOfPath = (YCHAR *)path;
326         }
327         else
328         {
329                 dir = yaffsfs_FindRoot(path,&restOfPath);
330         }
331
332         while(dir)
333         {
334                 // parse off /.
335                 // curve ball: also throw away surplus '/'
336                 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
337                 while(yaffsfs_IsPathDivider(*restOfPath))
338                 {
339                         restOfPath++; // get rid of '/'
340                 }
341
342                 *name = restOfPath;
343                 i = 0;
344
345                 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath))
346                 {
347                         if (i < YAFFS_MAX_NAME_LENGTH)
348                         {
349                                 str[i] = *restOfPath;
350                                 str[i+1] = '\0';
351                                 i++;
352                         }
353                         restOfPath++;
354                 }
355
356                 if(!*restOfPath)
357                 {
358                         // got to the end of the string
359                         return dir;
360                 }
361                 else
362                 {
363                         if(yaffs_strcmp(str,_Y(".")) == 0)
364                         {
365                                 // Do nothing
366                         }
367                         else if(yaffs_strcmp(str,_Y("..")) == 0)
368                         {
369                                 dir = dir->parent;
370                         }
371                         else
372                         {
373                                 dir = yaffs_FindObjectByName(dir,str);
374
375                                 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
376                                 {
377
378                                         dir = yaffsfs_FollowLink(dir,symDepth);
379
380                                 }
381
382                                 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
383                                 {
384                                         dir = NULL;
385                                 }
386                         }
387                 }
388         }
389         // directory did not exist.
390         return NULL;
391 }
392
393 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
394 {
395         return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
396 }
397
398 // yaffsfs_FindObject turns a path for an existing object into the object
399 //
400 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
401 {
402         yaffs_Object *dir;
403         YCHAR *name;
404
405         dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
406
407         if(dir && *name)
408         {
409                 return yaffs_FindObjectByName(dir,name);
410         }
411
412         return dir;
413 }
414
415
416 int yaffs_dup(int fd)
417 {
418         int newHandle = -1;
419         yaffsfs_Handle *oldPtr = NULL;
420         yaffsfs_Handle *newPtr = NULL;
421
422         yaffsfs_Lock();
423
424         oldPtr = yaffsfs_GetHandlePointer(fd);
425         if(oldPtr && oldPtr->inUse)
426                 newHandle = yaffsfs_GetHandle();
427         if(newHandle >= 0)
428                 newPtr = yaffsfs_GetHandlePointer(newHandle);
429
430         if(newPtr){
431                 *newPtr = *oldPtr;
432                 return newHandle;
433         }
434
435         if(!oldPtr)
436                 yaffsfs_SetError(-EBADF);
437         else
438                 yaffsfs_SetError(-ENOMEM);
439
440         return -1;
441
442 }
443
444 int yaffs_open(const YCHAR *path, int oflag, int mode)
445 {
446         yaffs_Object *obj = NULL;
447         yaffs_Object *dir = NULL;
448         YCHAR *name;
449         int handle = -1;
450         yaffsfs_Handle *h = NULL;
451         int alreadyOpen = 0;
452         int alreadyExclusive = 0;
453         int openDenied = 0;
454         int symDepth = 0;
455         int errorReported = 0;
456
457         int i;
458
459
460         // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
461
462
463         yaffsfs_Lock();
464
465         handle = yaffsfs_GetHandle();
466
467         if(handle >= 0)
468         {
469
470                 h = yaffsfs_GetHandlePointer(handle);
471
472
473                 // try to find the exisiting object
474                 obj = yaffsfs_FindObject(NULL,path,0);
475
476                 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
477                 {
478
479                         obj = yaffsfs_FollowLink(obj,symDepth++);
480                 }
481
482                 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
483                 {
484                         obj = NULL;
485                 }
486
487                 if(obj)
488                 {
489                         // Check if the object is already in use
490                         alreadyOpen = alreadyExclusive = 0;
491
492                         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
493                         {
494
495                                 if(i != handle &&
496                                    yaffsfs_handle[i].inUse &&
497                                     obj == yaffsfs_handle[i].obj)
498                                  {
499                                         alreadyOpen = 1;
500                                         if(yaffsfs_handle[i].exclusive)
501                                         {
502                                                 alreadyExclusive = 1;
503                                         }
504                                  }
505                         }
506
507                         if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
508                         {
509                                 openDenied = 1;
510                         }
511
512                         // Open should fail if O_CREAT and O_EXCL are specified
513                         if((oflag & O_EXCL) && (oflag & O_CREAT))
514                         {
515                                 openDenied = 1;
516                                 yaffsfs_SetError(-EEXIST);
517                                 errorReported = 1;
518                         }
519
520                         // Check file permissions
521                         if( (oflag & (O_RDWR | O_WRONLY)) == 0 &&     // ie O_RDONLY
522                            !(obj->yst_mode & S_IREAD))
523                         {
524                                 openDenied = 1;
525                         }
526
527                         if( (oflag & O_RDWR) &&
528                            !(obj->yst_mode & S_IREAD))
529                         {
530                                 openDenied = 1;
531                         }
532
533                         if( (oflag & (O_RDWR | O_WRONLY)) &&
534                            !(obj->yst_mode & S_IWRITE))
535                         {
536                                 openDenied = 1;
537                         }
538
539                 }
540
541                 else if((oflag & O_CREAT))
542                 {
543                         // Let's see if we can create this file
544                         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
545                         if(dir)
546                         {
547                                 obj = yaffs_MknodFile(dir,name,mode,0,0);
548                         }
549                         else
550                         {
551                                 yaffsfs_SetError(-ENOTDIR);
552                                 errorReported = 1;
553                         }
554                 }
555
556                 if(obj && !openDenied)
557                 {
558                         h->obj = obj;
559                         h->inUse = 1;
560                 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
561                         h->append =  (oflag & O_APPEND) ? 1 : 0;
562                         h->exclusive = (oflag & O_EXCL) ? 1 : 0;
563                         h->position = 0;
564
565                         obj->inUse++;
566                         if((oflag & O_TRUNC) && !h->readOnly)
567                         {
568                                 yaffs_ResizeFile(obj,0);
569                         }
570
571                 }
572                 else
573                 {
574                         yaffsfs_PutHandle(handle);
575                         if(!errorReported)
576                         {
577                                 yaffsfs_SetError(-EACCES);
578                                 errorReported = 1;
579                         }
580                         handle = -1;
581                 }
582
583         }
584
585         yaffsfs_Unlock();
586
587         return handle;
588 }
589
590 int yaffs_Dofsync(int fd,int datasync)
591 {
592         yaffsfs_Handle *h = NULL;
593         int retVal = 0;
594
595         yaffsfs_Lock();
596
597         h = yaffsfs_GetHandlePointer(fd);
598
599         if(h && h->inUse)
600         {
601                 // flush the file
602                 yaffs_FlushFile(h->obj,1,datasync);
603         }
604         else
605         {
606                 // bad handle
607                 yaffsfs_SetError(-EBADF);
608                 retVal = -1;
609         }
610
611         yaffsfs_Unlock();
612
613         return retVal;
614 }
615
616 int yaffs_fsync(int fd)
617 {
618         return yaffs_Dofsync(fd,0);
619 }
620
621 int yaffs_flush(int fd)
622 {
623         return yaffs_fsync(fd);
624 }
625
626 int yaffs_fdatasync(int fd)
627 {
628         return yaffs_Dofsync(fd,1);
629 }
630
631 int yaffs_close(int fd)
632 {
633         yaffsfs_Handle *h = NULL;
634         int retVal = 0;
635
636         yaffsfs_Lock();
637
638         h = yaffsfs_GetHandlePointer(fd);
639
640         if(h && h->inUse)
641         {
642                 // clean up
643                 yaffs_FlushFile(h->obj,1,0);
644                 h->obj->inUse--;
645                 if(h->obj->inUse <= 0 && h->obj->unlinked)
646                 {
647                         yaffs_DeleteObject(h->obj);
648                 }
649                 yaffsfs_PutHandle(fd);
650                 retVal = 0;
651         }
652         else
653         {
654                 // bad handle
655                 yaffsfs_SetError(-EBADF);
656                 retVal = -1;
657         }
658
659         yaffsfs_Unlock();
660
661         return retVal;
662 }
663
664 int yaffs_read(int fd, void *buf, unsigned int nbyte)
665 {
666         yaffsfs_Handle *h = NULL;
667         yaffs_Object *obj = NULL;
668         int pos = 0;
669         int nRead = -1;
670         unsigned int maxRead;
671
672         yaffsfs_Lock();
673         h = yaffsfs_GetHandlePointer(fd);
674         obj = yaffsfs_GetHandleObject(fd);
675
676         if(!h || !obj)
677         {
678                 // bad handle
679                 yaffsfs_SetError(-EBADF);
680         }
681         else if( h && obj)
682         {
683                 pos=  h->position;
684                 if(yaffs_GetObjectFileLength(obj) > pos)
685                 {
686                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
687                 }
688                 else
689                 {
690                         maxRead = 0;
691                 }
692
693                 if(nbyte > maxRead)
694                 {
695                         nbyte = maxRead;
696                 }
697
698
699                 if(nbyte > 0)
700                 {
701                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
702                         if(nRead >= 0)
703                         {
704                                 h->position = pos + nRead;
705                         }
706                         else
707                         {
708                                 //todo error
709                         }
710                 }
711                 else
712                 {
713                         nRead = 0;
714                 }
715
716         }
717
718         yaffsfs_Unlock();
719
720
721         return (nRead >= 0) ? nRead : -1;
722
723 }
724
725 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
726 {
727         yaffsfs_Handle *h = NULL;
728         yaffs_Object *obj = NULL;
729         int pos = 0;
730         int nRead = -1;
731         unsigned int maxRead;
732
733         yaffsfs_Lock();
734         h = yaffsfs_GetHandlePointer(fd);
735         obj = yaffsfs_GetHandleObject(fd);
736
737         if(!h || !obj)
738         {
739                 // bad handle
740                 yaffsfs_SetError(-EBADF);
741         }
742         else if( h && obj)
743         {
744                 pos= offset;
745                 if(yaffs_GetObjectFileLength(obj) > pos)
746                 {
747                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
748                 }
749                 else
750                 {
751                         maxRead = 0;
752                 }
753
754                 if(nbyte > maxRead)
755                 {
756                         nbyte = maxRead;
757                 }
758
759
760                 if(nbyte > 0)
761                 {
762                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
763                 }
764                 else
765                 {
766                         nRead = 0;
767                 }
768
769         }
770
771         yaffsfs_Unlock();
772
773
774         return (nRead >= 0) ? nRead : -1;
775
776 }
777
778 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
779 {
780         yaffsfs_Handle *h = NULL;
781         yaffs_Object *obj = NULL;
782         int pos = 0;
783         int nWritten = -1;
784         int writeThrough = 0;
785
786         yaffsfs_Lock();
787         h = yaffsfs_GetHandlePointer(fd);
788         obj = yaffsfs_GetHandleObject(fd);
789
790         if(!h || !obj)
791         {
792                 // bad handle
793                 yaffsfs_SetError(-EBADF);
794         }
795         else if( h && obj && h->readOnly)
796         {
797                 // todo error
798         }
799         else if( h && obj)
800         {
801                 if(h->append)
802                 {
803                         pos =  yaffs_GetObjectFileLength(obj);
804                 }
805                 else
806                 {
807                         pos = h->position;
808                 }
809
810                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
811
812                 if(nWritten >= 0)
813                 {
814                         h->position = pos + nWritten;
815                 }
816                 else
817                 {
818                         //todo error
819                 }
820
821         }
822
823         yaffsfs_Unlock();
824
825
826         return (nWritten >= 0) ? nWritten : -1;
827
828 }
829
830 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
831 {
832         yaffsfs_Handle *h = NULL;
833         yaffs_Object *obj = NULL;
834         int pos = 0;
835         int nWritten = -1;
836         int writeThrough = 0;
837
838         yaffsfs_Lock();
839         h = yaffsfs_GetHandlePointer(fd);
840         obj = yaffsfs_GetHandleObject(fd);
841
842         if(!h || !obj)
843         {
844                 // bad handle
845                 yaffsfs_SetError(-EBADF);
846         }
847         else if( h && obj && h->readOnly)
848         {
849                 // todo error
850         }
851         else if( h && obj)
852         {
853                 pos = offset;
854
855                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
856
857                 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
858                         yaffsfs_SetError(-ENOSPC);
859
860         }
861
862         yaffsfs_Unlock();
863
864
865         return (nWritten >= 0) ? nWritten : -1;
866
867 }
868
869
870 int yaffs_truncate(const YCHAR *path,off_t newSize)
871 {
872         yaffs_Object *obj = NULL;
873         int result = YAFFS_FAIL;
874
875         yaffsfs_Lock();
876
877         obj = yaffsfs_FindObject(NULL,path,0);
878         if(obj)
879                 obj = yaffs_GetEquivalentObject(obj);
880
881         if(!obj)
882         {
883                 yaffsfs_SetError(-ENOENT);
884         }
885         else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
886         {
887                 yaffsfs_SetError(-EISDIR);
888         }
889         else
890         {
891                 result = yaffs_ResizeFile(obj,newSize);
892         }
893
894         yaffsfs_Unlock();
895
896
897         return (result) ? 0 : -1;
898 }
899
900 int yaffs_ftruncate(int fd, off_t newSize)
901 {
902         yaffsfs_Handle *h = NULL;
903         yaffs_Object *obj = NULL;
904         int result = 0;
905
906         yaffsfs_Lock();
907         h = yaffsfs_GetHandlePointer(fd);
908         obj = yaffsfs_GetHandleObject(fd);
909
910         if(!h || !obj)
911         {
912                 // bad handle
913                 yaffsfs_SetError(-EBADF);
914         }
915         else
916         {
917                 // resize the file
918                 result = yaffs_ResizeFile(obj,newSize);
919         }
920         yaffsfs_Unlock();
921
922
923         return (result) ? 0 : -1;
924
925 }
926
927 off_t yaffs_lseek(int fd, off_t offset, int whence)
928 {
929         yaffsfs_Handle *h = NULL;
930         yaffs_Object *obj = NULL;
931         int pos = -1;
932         int fSize = -1;
933
934         yaffsfs_Lock();
935         h = yaffsfs_GetHandlePointer(fd);
936         obj = yaffsfs_GetHandleObject(fd);
937
938         if(!h || !obj)
939         {
940                 // bad handle
941                 yaffsfs_SetError(-EBADF);
942         }
943         else if(whence == SEEK_SET)
944         {
945                 if(offset >= 0)
946                 {
947                         pos = offset;
948                 }
949         }
950         else if(whence == SEEK_CUR)
951         {
952                 if( (h->position + offset) >= 0)
953                 {
954                         pos = (h->position + offset);
955                 }
956         }
957         else if(whence == SEEK_END)
958         {
959                 fSize = yaffs_GetObjectFileLength(obj);
960                 if(fSize >= 0 && (fSize + offset) >= 0)
961                 {
962                         pos = fSize + offset;
963                 }
964         }
965
966         if(pos >= 0)
967         {
968                 h->position = pos;
969         }
970         else
971         {
972                 // todo error
973         }
974
975
976         yaffsfs_Unlock();
977
978         return pos;
979 }
980
981
982 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
983 {
984         yaffs_Object *dir = NULL;
985         yaffs_Object *obj = NULL;
986         YCHAR *name;
987         int result = YAFFS_FAIL;
988
989         yaffsfs_Lock();
990
991         obj = yaffsfs_FindObject(NULL,path,0);
992         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
993         if(!dir)
994         {
995                 yaffsfs_SetError(-ENOTDIR);
996         }
997         else if(!obj)
998         {
999                 yaffsfs_SetError(-ENOENT);
1000         }
1001         else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1002         {
1003                 yaffsfs_SetError(-EISDIR);
1004         }
1005         else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1006         {
1007                 yaffsfs_SetError(-ENOTDIR);
1008         }
1009         else
1010         {
1011                 result = yaffs_Unlink(dir,name);
1012
1013                 if(result == YAFFS_FAIL && isDirectory)
1014                 {
1015                         yaffsfs_SetError(-ENOTEMPTY);
1016                 }
1017         }
1018
1019         yaffsfs_Unlock();
1020
1021         // todo error
1022
1023         return (result == YAFFS_FAIL) ? -1 : 0;
1024 }
1025
1026
1027 int yaffs_rmdir(const YCHAR *path)
1028 {
1029         return yaffsfs_DoUnlink(path,1);
1030 }
1031
1032 int yaffs_unlink(const YCHAR *path)
1033 {
1034         return yaffsfs_DoUnlink(path,0);
1035 }
1036
1037 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1038 {
1039         yaffs_Object *olddir = NULL;
1040         yaffs_Object *newdir = NULL;
1041         yaffs_Object *obj = NULL;
1042         YCHAR *oldname;
1043         YCHAR *newname;
1044         int result= YAFFS_FAIL;
1045         int renameAllowed = 1;
1046
1047         yaffsfs_Lock();
1048
1049         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1050         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1051         obj = yaffsfs_FindObject(NULL,oldPath,0);
1052
1053         if(!olddir || !newdir || !obj)
1054         {
1055                 // bad file
1056                 yaffsfs_SetError(-EBADF);
1057                 renameAllowed = 0;
1058         }
1059         else if(olddir->myDev != newdir->myDev)
1060         {
1061                 // oops must be on same device
1062                 // todo error
1063                 yaffsfs_SetError(-EXDEV);
1064                 renameAllowed = 0;
1065         }
1066         else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1067         {
1068                 // It is a directory, check that it is not being renamed to
1069                 // being its own decendent.
1070                 // Do this by tracing from the new directory back to the root, checking for obj
1071
1072                 yaffs_Object *xx = newdir;
1073
1074                 while( renameAllowed && xx)
1075                 {
1076                         if(xx == obj)
1077                         {
1078                                 renameAllowed = 0;
1079                         }
1080                         xx = xx->parent;
1081                 }
1082                 if(!renameAllowed) yaffsfs_SetError(-EACCES);
1083         }
1084
1085         if(renameAllowed)
1086         {
1087                 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1088         }
1089
1090         yaffsfs_Unlock();
1091
1092         return (result == YAFFS_FAIL) ? -1 : 0;
1093 }
1094
1095
1096 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1097 {
1098         int retVal = -1;
1099
1100         if(obj)
1101         {
1102                 obj = yaffs_GetEquivalentObject(obj);
1103         }
1104
1105         if(obj && buf)
1106         {
1107         buf->st_dev = (int)obj->myDev->genericDevice;
1108         buf->st_ino = obj->objectId;
1109         buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1110
1111                 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1112                 {
1113                         buf->st_mode |= S_IFDIR;
1114                 }
1115                 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1116                 {
1117                         buf->st_mode |= S_IFLNK;
1118                 }
1119                 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1120                 {
1121                         buf->st_mode |= S_IFREG;
1122                 }
1123
1124         buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1125         buf->st_uid = 0;
1126         buf->st_gid = 0;;
1127         buf->st_rdev = obj->yst_rdev;
1128         buf->st_size = yaffs_GetObjectFileLength(obj);
1129                 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1130         buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1131 #if CONFIG_YAFFS_WINCE
1132                 buf->yst_wince_atime[0] = obj->win_atime[0];
1133                 buf->yst_wince_atime[1] = obj->win_atime[1];
1134                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1135                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1136                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1137                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1138 #else
1139         buf->yst_atime = obj->yst_atime;
1140         buf->yst_ctime = obj->yst_ctime;
1141         buf->yst_mtime = obj->yst_mtime;
1142 #endif
1143                 retVal = 0;
1144         }
1145         return retVal;
1146 }
1147
1148 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1149 {
1150         yaffs_Object *obj;
1151
1152         int retVal = -1;
1153
1154         yaffsfs_Lock();
1155         obj = yaffsfs_FindObject(NULL,path,0);
1156
1157         if(!doLStat && obj)
1158         {
1159                 obj = yaffsfs_FollowLink(obj,0);
1160         }
1161
1162         if(obj)
1163         {
1164                 retVal = yaffsfs_DoStat(obj,buf);
1165         }
1166         else
1167         {
1168                 // todo error not found
1169                 yaffsfs_SetError(-ENOENT);
1170         }
1171
1172         yaffsfs_Unlock();
1173
1174         return retVal;
1175
1176 }
1177
1178 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1179 {
1180         return yaffsfs_DoStatOrLStat(path,buf,0);
1181 }
1182
1183 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1184 {
1185         return yaffsfs_DoStatOrLStat(path,buf,1);
1186 }
1187
1188 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1189 {
1190         yaffs_Object *obj;
1191
1192         int retVal = -1;
1193
1194         yaffsfs_Lock();
1195         obj = yaffsfs_GetHandleObject(fd);
1196
1197         if(obj)
1198         {
1199                 retVal = yaffsfs_DoStat(obj,buf);
1200         }
1201         else
1202         {
1203                 // bad handle
1204                 yaffsfs_SetError(-EBADF);
1205         }
1206
1207         yaffsfs_Unlock();
1208
1209         return retVal;
1210 }
1211
1212 #ifdef CONFIG_YAFFS_WINCE
1213 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1214 {
1215         yaffs_Object *obj;
1216
1217         int retVal = -1;
1218
1219         yaffsfs_Lock();
1220         obj = yaffsfs_GetHandleObject(fd);
1221
1222         if(obj)
1223         {
1224
1225                 if(wctime){
1226                         wctime[0] = obj->win_ctime[0];
1227                         wctime[1] = obj->win_ctime[1];
1228                 }
1229                 if(watime){
1230                         watime[0] = obj->win_atime[0];
1231                         watime[1] = obj->win_atime[1];
1232                 }
1233                 if(wmtime){
1234                         wmtime[0] = obj->win_mtime[0];
1235                         wmtime[1] = obj->win_mtime[1];
1236                 }
1237
1238
1239                 retVal = 0;
1240         }
1241         else
1242         {
1243                 // bad handle
1244                 yaffsfs_SetError(-EBADF);               
1245         }
1246         
1247         yaffsfs_Unlock();
1248         
1249         return retVal;
1250 }
1251
1252
1253 int yaffs_set_wince_times(int fd, 
1254                                                   const unsigned *wctime, 
1255                                                   const unsigned *watime, 
1256                                                   const unsigned *wmtime)
1257 {
1258         yaffs_Object *obj;
1259         int result;
1260         int retVal = -1;
1261
1262         yaffsfs_Lock();
1263         obj = yaffsfs_GetHandleObject(fd);
1264
1265         if(obj)
1266         {
1267
1268                 if(wctime){
1269                         obj->win_ctime[0] = wctime[0];
1270                         obj->win_ctime[1] = wctime[1];
1271                 }
1272                 if(watime){
1273                         obj->win_atime[0] = watime[0];
1274                         obj->win_atime[1] = watime[1];
1275                 }
1276                 if(wmtime){
1277                         obj->win_mtime[0] = wmtime[0];
1278                         obj->win_mtime[1] = wmtime[1];
1279                 }
1280
1281                 obj->dirty = 1;
1282                 result = yaffs_FlushFile(obj,0,0);
1283                 retVal = 0;
1284         }
1285         else
1286         {
1287                 // bad handle
1288                 yaffsfs_SetError(-EBADF);
1289         }
1290
1291         yaffsfs_Unlock();
1292
1293         return retVal;
1294 }
1295
1296 #endif
1297
1298
1299 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1300 {
1301         int result = -1;
1302
1303         if(obj)
1304         {
1305                 obj = yaffs_GetEquivalentObject(obj);
1306         }
1307
1308         if(obj)
1309         {
1310                 obj->yst_mode = mode;
1311                 obj->dirty = 1;
1312                 result = yaffs_FlushFile(obj,0,0);
1313         }
1314
1315         return result == YAFFS_OK ? 0 : -1;
1316 }
1317
1318
1319 int yaffs_access(const YCHAR *path, int amode)
1320 {
1321         yaffs_Object *obj;
1322
1323         int retval = 0;
1324
1325         yaffsfs_Lock();
1326         obj = yaffsfs_FindObject(NULL,path,0);
1327
1328         if(obj)
1329         {
1330                 int access_ok = 1;
1331
1332                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1333                         access_ok = 0;
1334                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1335                         access_ok = 0;
1336                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1337                         access_ok = 0;
1338
1339                 if(!access_ok) {
1340                         yaffsfs_SetError(-EACCES);
1341                         retval = -1;
1342                 }
1343         }
1344         else
1345         {
1346                 // todo error not found
1347                 yaffsfs_SetError(-ENOENT);
1348                 retval = -1;
1349         }
1350
1351         yaffsfs_Unlock();
1352
1353         return retval;
1354
1355 }
1356
1357
1358 int yaffs_chmod(const YCHAR *path, mode_t mode)
1359 {
1360         yaffs_Object *obj;
1361
1362         int retVal = -1;
1363
1364         yaffsfs_Lock();
1365         obj = yaffsfs_FindObject(NULL,path,0);
1366
1367         if(obj)
1368         {
1369                 retVal = yaffsfs_DoChMod(obj,mode);
1370         }
1371         else
1372         {
1373                 // todo error not found
1374                 yaffsfs_SetError(-ENOENT);
1375         }
1376
1377         yaffsfs_Unlock();
1378
1379         return retVal;
1380
1381 }
1382
1383
1384 int yaffs_fchmod(int fd, mode_t mode)
1385 {
1386         yaffs_Object *obj;
1387
1388         int retVal = -1;
1389
1390         yaffsfs_Lock();
1391         obj = yaffsfs_GetHandleObject(fd);
1392
1393         if(obj)
1394         {
1395                 retVal = yaffsfs_DoChMod(obj,mode);
1396         }
1397         else
1398         {
1399                 // bad handle
1400                 yaffsfs_SetError(-EBADF);
1401         }
1402
1403         yaffsfs_Unlock();
1404
1405         return retVal;
1406 }
1407
1408
1409 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1410 {
1411         yaffs_Object *parent = NULL;
1412         yaffs_Object *dir = NULL;
1413         YCHAR *name;
1414         int retVal= -1;
1415
1416         yaffsfs_Lock();
1417         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1418         if(parent)
1419                 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1420         if(dir)
1421         {
1422                 retVal = 0;
1423         }
1424         else
1425         {
1426                 if(!parent){
1427                         yaffsfs_SetError(-ENOENT); // missing path
1428                 }
1429                 else if (yaffs_FindObjectByName(parent,name)){
1430                         yaffsfs_SetError(-EEXIST); // the name already exists
1431                 }
1432                 else
1433                         yaffsfs_SetError(-ENOSPC); // just assume no space
1434                 retVal = -1;
1435         }
1436
1437         yaffsfs_Unlock();
1438
1439         return retVal;
1440 }
1441
1442 int yaffs_mount(const YCHAR *path)
1443 {
1444         int retVal=-1;
1445         int result=YAFFS_FAIL;
1446         yaffs_Device *dev=NULL;
1447         YCHAR *dummy;
1448
1449         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1450
1451         yaffsfs_Lock();
1452         dev = yaffsfs_FindDevice(path,&dummy);
1453         if(dev)
1454         {
1455                 if(!dev->isMounted)
1456                 {
1457                         result = yaffs_GutsInitialise(dev);
1458                         if(result == YAFFS_FAIL)
1459                         {
1460                                 // todo error - mount failed
1461                                 yaffsfs_SetError(-ENOMEM);
1462                         }
1463                         retVal = result ? 0 : -1;
1464
1465                 }
1466                 else
1467                 {
1468                         //todo error - already mounted.
1469                         yaffsfs_SetError(-EBUSY);
1470                 }
1471         }
1472         else
1473         {
1474                 // todo error - no device
1475                 yaffsfs_SetError(-ENODEV);
1476         }
1477         yaffsfs_Unlock();
1478         return retVal;
1479
1480 }
1481
1482 int yaffs_sync(const YCHAR *path)
1483 {
1484         int retVal=-1;
1485         yaffs_Device *dev=NULL;
1486         YCHAR *dummy;
1487         
1488         yaffsfs_Lock();
1489         dev = yaffsfs_FindDevice(path,&dummy);
1490         if(dev)
1491         {
1492                 if(dev->isMounted)
1493                 {
1494                         
1495                         yaffs_FlushEntireDeviceCache(dev);
1496                         yaffs_CheckpointSave(dev);
1497                         
1498                         
1499                 }
1500                 else
1501                 {
1502                         //todo error - not mounted.
1503                         yaffsfs_SetError(-EINVAL);
1504                         
1505                 }
1506         }
1507         else
1508         {
1509                 // todo error - no device
1510                 yaffsfs_SetError(-ENODEV);
1511         }       
1512         yaffsfs_Unlock();
1513         return retVal;  
1514 }
1515
1516
1517 int yaffs_unmount(const YCHAR *path)
1518 {
1519         int retVal=-1;
1520         yaffs_Device *dev=NULL;
1521         YCHAR *dummy;
1522
1523         yaffsfs_Lock();
1524         dev = yaffsfs_FindDevice(path,&dummy);
1525         if(dev)
1526         {
1527                 if(dev->isMounted)
1528                 {
1529                         int i;
1530                         int inUse;
1531
1532                         yaffs_FlushEntireDeviceCache(dev);
1533                         yaffs_CheckpointSave(dev);
1534
1535                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1536                         {
1537                                 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1538                                 {
1539                                         inUse = 1; // the device is in use, can't unmount
1540                                 }
1541                         }
1542
1543                         if(!inUse)
1544                         {
1545                                 yaffs_Deinitialise(dev);
1546
1547                                 retVal = 0;
1548                         }
1549                         else
1550                         {
1551                                 // todo error can't unmount as files are open
1552                                 yaffsfs_SetError(-EBUSY);
1553                         }
1554
1555                 }
1556                 else
1557                 {
1558                         //todo error - not mounted.
1559                         yaffsfs_SetError(-EINVAL);
1560
1561                 }
1562         }
1563         else
1564         {
1565                 // todo error - no device
1566                 yaffsfs_SetError(-ENODEV);
1567         }
1568         yaffsfs_Unlock();
1569         return retVal;
1570
1571 }
1572
1573 loff_t yaffs_freespace(const YCHAR *path)
1574 {
1575         loff_t retVal=-1;
1576         yaffs_Device *dev=NULL;
1577         YCHAR *dummy;
1578
1579         yaffsfs_Lock();
1580         dev = yaffsfs_FindDevice(path,&dummy);
1581         if(dev  && dev->isMounted)
1582         {
1583                 retVal = yaffs_GetNumberOfFreeChunks(dev);
1584                 retVal *= dev->nDataBytesPerChunk;
1585
1586         }
1587         else
1588         {
1589                 yaffsfs_SetError(-EINVAL);
1590         }
1591
1592         yaffsfs_Unlock();
1593         return retVal;
1594 }
1595
1596 loff_t yaffs_totalspace(const YCHAR *path)
1597 {
1598         loff_t retVal=-1;
1599         yaffs_Device *dev=NULL;
1600         YCHAR *dummy;
1601
1602         yaffsfs_Lock();
1603         dev = yaffsfs_FindDevice(path,&dummy);
1604         if(dev  && dev->isMounted)
1605         {
1606                 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1607                 retVal *= dev->nChunksPerBlock;
1608                 retVal *= dev->nDataBytesPerChunk;
1609
1610         }
1611         else
1612         {
1613                 yaffsfs_SetError(-EINVAL);
1614         }
1615
1616         yaffsfs_Unlock();
1617         return retVal;
1618 }
1619
1620 int yaffs_inodecount(const YCHAR *path)
1621 {
1622         loff_t retVal= -1;
1623         yaffs_Device *dev=NULL;
1624         YCHAR *dummy;
1625
1626         yaffsfs_Lock();
1627         dev = yaffsfs_FindDevice(path,&dummy);
1628         if(dev  && dev->isMounted) {
1629            int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1630            if(nObjects > dev->nHardLinks)
1631                 retVal = nObjects - dev->nHardLinks;
1632         }
1633         
1634         if(retVal < 0){
1635                 yaffsfs_SetError(-EINVAL);
1636         }
1637         
1638         yaffsfs_Unlock();
1639         return retVal;  
1640 }
1641
1642
1643
1644 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1645 {
1646
1647         yaffsfs_DeviceConfiguration *cfg;
1648
1649         yaffsfs_configurationList = cfgList;
1650
1651         yaffsfs_InitHandles();
1652
1653         cfg = yaffsfs_configurationList;
1654
1655         while(cfg && cfg->prefix && cfg->dev)
1656         {
1657                 cfg->dev->isMounted = 0;
1658                 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1659                 cfg++;
1660         }
1661
1662
1663 }
1664
1665
1666 //
1667 // Directory search stuff.
1668
1669 //
1670 // Directory search context
1671 //
1672 // NB this is an opaque structure.
1673
1674
1675 typedef struct
1676 {
1677         __u32 magic;
1678         yaffs_dirent de;                /* directory entry being used by this dsc */
1679         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
1680         yaffs_Object *dirObj;           /* ptr to directory being searched */
1681         yaffs_Object *nextReturn;       /* obj to be returned by next readddir */
1682         int offset;
1683         struct ylist_head others;       
1684 } yaffsfs_DirectorySearchContext;
1685
1686
1687
1688 static struct ylist_head search_contexts;
1689
1690
1691 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1692 {
1693         if(dsc &&
1694            dsc->dirObj &&
1695            dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1696
1697            dsc->offset = 0;
1698
1699            if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1700                 dsc->nextReturn = NULL;
1701            } else {
1702                 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1703                                                 yaffs_Object,siblings);
1704            }
1705         } else {
1706                 /* Hey someone isn't playing nice! */
1707         }
1708 }
1709
1710 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1711 {
1712         if(dsc &&
1713            dsc->dirObj &&
1714            dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1715
1716            if( dsc->nextReturn == NULL ||
1717                ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1718                 dsc->nextReturn = NULL;
1719            } else {
1720                    struct ylist_head *next = dsc->nextReturn->siblings.next;
1721
1722                    if( next == &dsc->dirObj->variant.directoryVariant.children)
1723                         dsc->nextReturn = NULL; /* end of list */
1724                    else
1725                         dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1726            }
1727         } else {
1728                 /* Hey someone isn't playing nice! */
1729         }
1730 }
1731
1732 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1733 {
1734
1735         struct ylist_head *i;
1736         yaffsfs_DirectorySearchContext *dsc;
1737
1738         /* if search contexts not initilised then skip */
1739         if(!search_contexts.next)
1740                 return;
1741
1742         /* Iterate through the directory search contexts.
1743          * If any are the one being removed, then advance the dsc to
1744          * the next one to prevent a hanging ptr.
1745          */
1746          ylist_for_each(i, &search_contexts) {
1747                 if (i) {
1748                         dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1749                         if(dsc->nextReturn == obj)
1750                                 yaffsfs_DirAdvance(dsc);
1751                 }
1752         }
1753
1754 }
1755
1756 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1757 {
1758         yaffs_DIR *dir = NULL;
1759         yaffs_Object *obj = NULL;
1760         yaffsfs_DirectorySearchContext *dsc = NULL;
1761
1762         yaffsfs_Lock();
1763
1764         obj = yaffsfs_FindObject(NULL,dirname,0);
1765
1766         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1767         {
1768
1769                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1770                 dir = (yaffs_DIR *)dsc;
1771                 if(dsc)
1772                 {
1773                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1774                         dsc->magic = YAFFS_MAGIC;
1775                         dsc->dirObj = obj;
1776                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1777                         YINIT_LIST_HEAD(&dsc->others);
1778
1779                         if(!search_contexts.next)
1780                                 YINIT_LIST_HEAD(&search_contexts);
1781
1782                         ylist_add(&dsc->others,&search_contexts);       
1783                         yaffsfs_SetDirRewound(dsc);             }
1784
1785         }
1786
1787         yaffsfs_Unlock();
1788
1789         return dir;
1790 }
1791
1792 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1793 {
1794         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1795         struct yaffs_dirent *retVal = NULL;
1796
1797         yaffsfs_Lock();
1798
1799         if(dsc && dsc->magic == YAFFS_MAGIC){
1800                 yaffsfs_SetError(0);
1801                 if(dsc->nextReturn){
1802                         dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1803                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1804                         dsc->de.d_off = dsc->offset++;
1805                         yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1806                         if(yaffs_strlen(dsc->de.d_name) == 0)
1807                         {
1808                                 // this should not happen!
1809                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1810                         }
1811                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1812                         retVal = &dsc->de;
1813                         yaffsfs_DirAdvance(dsc);
1814                 } else
1815                         retVal = NULL;
1816         }
1817         else
1818         {
1819                 yaffsfs_SetError(-EBADF);
1820         }
1821
1822         yaffsfs_Unlock();
1823
1824         return retVal;
1825
1826 }
1827
1828
1829 void yaffs_rewinddir(yaffs_DIR *dirp)
1830 {
1831         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1832
1833         yaffsfs_Lock();
1834
1835         yaffsfs_SetDirRewound(dsc);
1836
1837         yaffsfs_Unlock();
1838 }
1839
1840
1841 int yaffs_closedir(yaffs_DIR *dirp)
1842 {
1843         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1844
1845         yaffsfs_Lock();
1846         dsc->magic = 0;
1847         ylist_del(&dsc->others); /* unhook from list */
1848         YFREE(dsc);
1849         yaffsfs_Unlock();
1850         return 0;
1851 }
1852
1853 // end of directory stuff
1854
1855
1856 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1857 {
1858         yaffs_Object *parent = NULL;
1859         yaffs_Object *obj;
1860         YCHAR *name;
1861         int retVal= -1;
1862         int mode = 0; // ignore for now
1863
1864         yaffsfs_Lock();
1865         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1866         if(parent){
1867                 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1868                 if(obj)
1869                 {
1870                         retVal = 0;
1871                 }
1872                 else
1873                 {
1874                         yaffsfs_SetError(-ENOSPC); // just assume no space for now
1875                         retVal = -1;
1876                 }
1877         } else {
1878                 yaffsfs_SetError(-EINVAL);
1879                 retVal = -1;
1880         }
1881
1882         yaffsfs_Unlock();
1883
1884         return retVal;
1885
1886 }
1887
1888 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1889 {
1890         yaffs_Object *obj = NULL;
1891         int retVal;
1892
1893
1894         yaffsfs_Lock();
1895
1896         obj = yaffsfs_FindObject(NULL,path,0);
1897
1898         if(!obj)
1899         {
1900                 yaffsfs_SetError(-ENOENT);
1901                 retVal = -1;
1902         }
1903         else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1904         {
1905                 yaffsfs_SetError(-EINVAL);
1906                 retVal = -1;
1907         }
1908         else
1909         {
1910                 YCHAR *alias = obj->variant.symLinkVariant.alias;
1911                 memset(buf,0,bufsiz);
1912                 yaffs_strncpy(buf,alias,bufsiz - 1);
1913                 retVal = 0;
1914         }
1915         yaffsfs_Unlock();
1916         return retVal;
1917 }
1918
1919 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1920 {
1921         // Creates a link called newpath to existing oldpath
1922         yaffs_Object *obj = NULL;
1923         yaffs_Object *target = NULL;
1924         int retVal = 0;
1925
1926
1927         yaffsfs_Lock();
1928
1929         obj = yaffsfs_FindObject(NULL,oldpath,0);
1930         target = yaffsfs_FindObject(NULL,newpath,0);
1931
1932         if(!obj)
1933         {
1934                 yaffsfs_SetError(-ENOENT);
1935                 retVal = -1;
1936         }
1937         else if(target)
1938         {
1939                 yaffsfs_SetError(-EEXIST);
1940                 retVal = -1;
1941         }
1942         else
1943         {
1944                 yaffs_Object *newdir = NULL;
1945                 yaffs_Object *link = NULL;
1946
1947                 YCHAR *newname;
1948
1949                 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1950
1951                 if(!newdir)
1952                 {
1953                         yaffsfs_SetError(-ENOTDIR);
1954                         retVal = -1;
1955                 }
1956                 else if(newdir->myDev != obj->myDev)
1957                 {
1958                         yaffsfs_SetError(-EXDEV);
1959                         retVal = -1;
1960                 }
1961                 if(newdir && yaffs_strlen(newname) > 0)
1962                 {
1963                         link = yaffs_Link(newdir,newname,obj);
1964                         if(link)
1965                                 retVal = 0;
1966                         else
1967                         {
1968                                 yaffsfs_SetError(-ENOSPC);
1969                                 retVal = -1;
1970                         }
1971
1972                 }
1973         }
1974         yaffsfs_Unlock();
1975
1976         return retVal;
1977 }
1978
1979 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1980 {
1981         return -1;
1982 }
1983
1984 int yaffs_DumpDevStruct(const YCHAR *path)
1985 {
1986 #if 0
1987         YCHAR *rest;
1988
1989         yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1990
1991         if(obj)
1992         {
1993                 yaffs_Device *dev = obj->myDev;
1994
1995                 printf("\n"
1996                            "nPageWrites.......... %d\n"
1997                            "nPageReads........... %d\n"
1998                            "nBlockErasures....... %d\n"
1999                            "nGCCopies............ %d\n"
2000                            "garbageCollections... %d\n"
2001                            "passiveGarbageColl'ns %d\n"
2002                            "\n",
2003                                 dev->nPageWrites,
2004                                 dev->nPageReads,
2005                                 dev->nBlockErasures,
2006                                 dev->nGCCopies,
2007                                 dev->garbageCollections,
2008                                 dev->passiveGarbageCollections
2009                 );
2010
2011         }
2012
2013 #endif
2014         return 0;
2015 }
2016