1da4d059049e4e3dc1acd238f2b04a9a7b4bfc67
[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.26 2009-09-23 23:24:55 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_fdatasync(int fd)
622 {
623         return yaffs_Dofsync(fd,1);
624 }
625
626 int yaffs_close(int fd)
627 {
628         yaffsfs_Handle *h = NULL;
629         int retVal = 0;
630
631         yaffsfs_Lock();
632
633         h = yaffsfs_GetHandlePointer(fd);
634
635         if(h && h->inUse)
636         {
637                 // clean up
638                 yaffs_FlushFile(h->obj,1,0);
639                 h->obj->inUse--;
640                 if(h->obj->inUse <= 0 && h->obj->unlinked)
641                 {
642                         yaffs_DeleteObject(h->obj);
643                 }
644                 yaffsfs_PutHandle(fd);
645                 retVal = 0;
646         }
647         else
648         {
649                 // bad handle
650                 yaffsfs_SetError(-EBADF);
651                 retVal = -1;
652         }
653
654         yaffsfs_Unlock();
655
656         return retVal;
657 }
658
659 int yaffs_read(int fd, void *buf, unsigned int nbyte)
660 {
661         yaffsfs_Handle *h = NULL;
662         yaffs_Object *obj = NULL;
663         int pos = 0;
664         int nRead = -1;
665         unsigned int maxRead;
666
667         yaffsfs_Lock();
668         h = yaffsfs_GetHandlePointer(fd);
669         obj = yaffsfs_GetHandleObject(fd);
670
671         if(!h || !obj)
672         {
673                 // bad handle
674                 yaffsfs_SetError(-EBADF);
675         }
676         else if( h && obj)
677         {
678                 pos=  h->position;
679                 if(yaffs_GetObjectFileLength(obj) > pos)
680                 {
681                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
682                 }
683                 else
684                 {
685                         maxRead = 0;
686                 }
687
688                 if(nbyte > maxRead)
689                 {
690                         nbyte = maxRead;
691                 }
692
693
694                 if(nbyte > 0)
695                 {
696                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
697                         if(nRead >= 0)
698                         {
699                                 h->position = pos + nRead;
700                         }
701                         else
702                         {
703                                 //todo error
704                         }
705                 }
706                 else
707                 {
708                         nRead = 0;
709                 }
710
711         }
712
713         yaffsfs_Unlock();
714
715
716         return (nRead >= 0) ? nRead : -1;
717
718 }
719
720 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
721 {
722         yaffsfs_Handle *h = NULL;
723         yaffs_Object *obj = NULL;
724         int pos = 0;
725         int nRead = -1;
726         unsigned int maxRead;
727
728         yaffsfs_Lock();
729         h = yaffsfs_GetHandlePointer(fd);
730         obj = yaffsfs_GetHandleObject(fd);
731
732         if(!h || !obj)
733         {
734                 // bad handle
735                 yaffsfs_SetError(-EBADF);
736         }
737         else if( h && obj)
738         {
739                 pos= offset;
740                 if(yaffs_GetObjectFileLength(obj) > pos)
741                 {
742                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
743                 }
744                 else
745                 {
746                         maxRead = 0;
747                 }
748
749                 if(nbyte > maxRead)
750                 {
751                         nbyte = maxRead;
752                 }
753
754
755                 if(nbyte > 0)
756                 {
757                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
758                 }
759                 else
760                 {
761                         nRead = 0;
762                 }
763
764         }
765
766         yaffsfs_Unlock();
767
768
769         return (nRead >= 0) ? nRead : -1;
770
771 }
772
773 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
774 {
775         yaffsfs_Handle *h = NULL;
776         yaffs_Object *obj = NULL;
777         int pos = 0;
778         int nWritten = -1;
779         int writeThrough = 0;
780
781         yaffsfs_Lock();
782         h = yaffsfs_GetHandlePointer(fd);
783         obj = yaffsfs_GetHandleObject(fd);
784
785         if(!h || !obj)
786         {
787                 // bad handle
788                 yaffsfs_SetError(-EBADF);
789         }
790         else if( h && obj && h->readOnly)
791         {
792                 // todo error
793         }
794         else if( h && obj)
795         {
796                 if(h->append)
797                 {
798                         pos =  yaffs_GetObjectFileLength(obj);
799                 }
800                 else
801                 {
802                         pos = h->position;
803                 }
804
805                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
806
807                 if(nWritten >= 0)
808                 {
809                         h->position = pos + nWritten;
810                 }
811                 else
812                 {
813                         //todo error
814                 }
815
816         }
817
818         yaffsfs_Unlock();
819
820
821         return (nWritten >= 0) ? nWritten : -1;
822
823 }
824
825 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
826 {
827         yaffsfs_Handle *h = NULL;
828         yaffs_Object *obj = NULL;
829         int pos = 0;
830         int nWritten = -1;
831         int writeThrough = 0;
832
833         yaffsfs_Lock();
834         h = yaffsfs_GetHandlePointer(fd);
835         obj = yaffsfs_GetHandleObject(fd);
836
837         if(!h || !obj)
838         {
839                 // bad handle
840                 yaffsfs_SetError(-EBADF);
841         }
842         else if( h && obj && h->readOnly)
843         {
844                 // todo error
845         }
846         else if( h && obj)
847         {
848                 pos = offset;
849
850                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
851
852                 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
853                         yaffsfs_SetError(-ENOSPC);
854
855         }
856
857         yaffsfs_Unlock();
858
859
860         return (nWritten >= 0) ? nWritten : -1;
861
862 }
863
864
865 int yaffs_truncate(const YCHAR *path,off_t newSize)
866 {
867         yaffs_Object *obj = NULL;
868         int result = YAFFS_FAIL;
869
870         yaffsfs_Lock();
871
872         obj = yaffsfs_FindObject(NULL,path,0);
873         if(obj)
874                 obj = yaffs_GetEquivalentObject(obj);
875
876         if(!obj)
877         {
878                 yaffsfs_SetError(-ENOENT);
879         }
880         else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
881         {
882                 yaffsfs_SetError(-EISDIR);
883         }
884         else
885         {
886                 result = yaffs_ResizeFile(obj,newSize);
887         }
888
889         yaffsfs_Unlock();
890
891
892         return (result) ? 0 : -1;
893 }
894
895 int yaffs_ftruncate(int fd, off_t newSize)
896 {
897         yaffsfs_Handle *h = NULL;
898         yaffs_Object *obj = NULL;
899         int result = 0;
900
901         yaffsfs_Lock();
902         h = yaffsfs_GetHandlePointer(fd);
903         obj = yaffsfs_GetHandleObject(fd);
904
905         if(!h || !obj)
906         {
907                 // bad handle
908                 yaffsfs_SetError(-EBADF);
909         }
910         else
911         {
912                 // resize the file
913                 result = yaffs_ResizeFile(obj,newSize);
914         }
915         yaffsfs_Unlock();
916
917
918         return (result) ? 0 : -1;
919
920 }
921
922 off_t yaffs_lseek(int fd, off_t offset, int whence)
923 {
924         yaffsfs_Handle *h = NULL;
925         yaffs_Object *obj = NULL;
926         int pos = -1;
927         int fSize = -1;
928
929         yaffsfs_Lock();
930         h = yaffsfs_GetHandlePointer(fd);
931         obj = yaffsfs_GetHandleObject(fd);
932
933         if(!h || !obj)
934         {
935                 // bad handle
936                 yaffsfs_SetError(-EBADF);
937         }
938         else if(whence == SEEK_SET)
939         {
940                 if(offset >= 0)
941                 {
942                         pos = offset;
943                 }
944         }
945         else if(whence == SEEK_CUR)
946         {
947                 if( (h->position + offset) >= 0)
948                 {
949                         pos = (h->position + offset);
950                 }
951         }
952         else if(whence == SEEK_END)
953         {
954                 fSize = yaffs_GetObjectFileLength(obj);
955                 if(fSize >= 0 && (fSize + offset) >= 0)
956                 {
957                         pos = fSize + offset;
958                 }
959         }
960
961         if(pos >= 0)
962         {
963                 h->position = pos;
964         }
965         else
966         {
967                 // todo error
968         }
969
970
971         yaffsfs_Unlock();
972
973         return pos;
974 }
975
976
977 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
978 {
979         yaffs_Object *dir = NULL;
980         yaffs_Object *obj = NULL;
981         YCHAR *name;
982         int result = YAFFS_FAIL;
983
984         yaffsfs_Lock();
985
986         obj = yaffsfs_FindObject(NULL,path,0);
987         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
988         if(!dir)
989         {
990                 yaffsfs_SetError(-ENOTDIR);
991         }
992         else if(!obj)
993         {
994                 yaffsfs_SetError(-ENOENT);
995         }
996         else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
997         {
998                 yaffsfs_SetError(-EISDIR);
999         }
1000         else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1001         {
1002                 yaffsfs_SetError(-ENOTDIR);
1003         }
1004         else
1005         {
1006                 result = yaffs_Unlink(dir,name);
1007
1008                 if(result == YAFFS_FAIL && isDirectory)
1009                 {
1010                         yaffsfs_SetError(-ENOTEMPTY);
1011                 }
1012         }
1013
1014         yaffsfs_Unlock();
1015
1016         // todo error
1017
1018         return (result == YAFFS_FAIL) ? -1 : 0;
1019 }
1020
1021
1022 int yaffs_rmdir(const YCHAR *path)
1023 {
1024         return yaffsfs_DoUnlink(path,1);
1025 }
1026
1027 int yaffs_unlink(const YCHAR *path)
1028 {
1029         return yaffsfs_DoUnlink(path,0);
1030 }
1031
1032 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1033 {
1034         yaffs_Object *olddir = NULL;
1035         yaffs_Object *newdir = NULL;
1036         yaffs_Object *obj = NULL;
1037         YCHAR *oldname;
1038         YCHAR *newname;
1039         int result= YAFFS_FAIL;
1040         int renameAllowed = 1;
1041
1042         yaffsfs_Lock();
1043
1044         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1045         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1046         obj = yaffsfs_FindObject(NULL,oldPath,0);
1047
1048         if(!olddir || !newdir || !obj)
1049         {
1050                 // bad file
1051                 yaffsfs_SetError(-EBADF);
1052                 renameAllowed = 0;
1053         }
1054         else if(olddir->myDev != newdir->myDev)
1055         {
1056                 // oops must be on same device
1057                 // todo error
1058                 yaffsfs_SetError(-EXDEV);
1059                 renameAllowed = 0;
1060         }
1061         else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1062         {
1063                 // It is a directory, check that it is not being renamed to
1064                 // being its own decendent.
1065                 // Do this by tracing from the new directory back to the root, checking for obj
1066
1067                 yaffs_Object *xx = newdir;
1068
1069                 while( renameAllowed && xx)
1070                 {
1071                         if(xx == obj)
1072                         {
1073                                 renameAllowed = 0;
1074                         }
1075                         xx = xx->parent;
1076                 }
1077                 if(!renameAllowed) yaffsfs_SetError(-EACCES);
1078         }
1079
1080         if(renameAllowed)
1081         {
1082                 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1083         }
1084
1085         yaffsfs_Unlock();
1086
1087         return (result == YAFFS_FAIL) ? -1 : 0;
1088 }
1089
1090
1091 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1092 {
1093         int retVal = -1;
1094
1095         if(obj)
1096         {
1097                 obj = yaffs_GetEquivalentObject(obj);
1098         }
1099
1100         if(obj && buf)
1101         {
1102         buf->st_dev = (int)obj->myDev->genericDevice;
1103         buf->st_ino = obj->objectId;
1104         buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1105
1106                 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1107                 {
1108                         buf->st_mode |= S_IFDIR;
1109                 }
1110                 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1111                 {
1112                         buf->st_mode |= S_IFLNK;
1113                 }
1114                 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1115                 {
1116                         buf->st_mode |= S_IFREG;
1117                 }
1118
1119         buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1120         buf->st_uid = 0;
1121         buf->st_gid = 0;;
1122         buf->st_rdev = obj->yst_rdev;
1123         buf->st_size = yaffs_GetObjectFileLength(obj);
1124                 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1125         buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1126 #if CONFIG_YAFFS_WINCE
1127                 buf->yst_wince_atime[0] = obj->win_atime[0];
1128                 buf->yst_wince_atime[1] = obj->win_atime[1];
1129                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1130                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1131                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1132                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1133 #else
1134         buf->yst_atime = obj->yst_atime;
1135         buf->yst_ctime = obj->yst_ctime;
1136         buf->yst_mtime = obj->yst_mtime;
1137 #endif
1138                 retVal = 0;
1139         }
1140         return retVal;
1141 }
1142
1143 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1144 {
1145         yaffs_Object *obj;
1146
1147         int retVal = -1;
1148
1149         yaffsfs_Lock();
1150         obj = yaffsfs_FindObject(NULL,path,0);
1151
1152         if(!doLStat && obj)
1153         {
1154                 obj = yaffsfs_FollowLink(obj,0);
1155         }
1156
1157         if(obj)
1158         {
1159                 retVal = yaffsfs_DoStat(obj,buf);
1160         }
1161         else
1162         {
1163                 // todo error not found
1164                 yaffsfs_SetError(-ENOENT);
1165         }
1166
1167         yaffsfs_Unlock();
1168
1169         return retVal;
1170
1171 }
1172
1173 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1174 {
1175         return yaffsfs_DoStatOrLStat(path,buf,0);
1176 }
1177
1178 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1179 {
1180         return yaffsfs_DoStatOrLStat(path,buf,1);
1181 }
1182
1183 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1184 {
1185         yaffs_Object *obj;
1186
1187         int retVal = -1;
1188
1189         yaffsfs_Lock();
1190         obj = yaffsfs_GetHandleObject(fd);
1191
1192         if(obj)
1193         {
1194                 retVal = yaffsfs_DoStat(obj,buf);
1195         }
1196         else
1197         {
1198                 // bad handle
1199                 yaffsfs_SetError(-EBADF);
1200         }
1201
1202         yaffsfs_Unlock();
1203
1204         return retVal;
1205 }
1206
1207 #ifdef CONFIG_YAFFS_WINCE
1208 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1209 {
1210         yaffs_Object *obj;
1211
1212         int retVal = -1;
1213
1214         yaffsfs_Lock();
1215         obj = yaffsfs_GetHandleObject(fd);
1216
1217         if(obj)
1218         {
1219
1220                 if(wctime){
1221                         wctime[0] = obj->win_ctime[0];
1222                         wctime[1] = obj->win_ctime[1];
1223                 }
1224                 if(watime){
1225                         watime[0] = obj->win_atime[0];
1226                         watime[1] = obj->win_atime[1];
1227                 }
1228                 if(wmtime){
1229                         wmtime[0] = obj->win_mtime[0];
1230                         wmtime[1] = obj->win_mtime[1];
1231                 }
1232
1233
1234                 retVal = 0;
1235         }
1236         else
1237         {
1238                 // bad handle
1239                 yaffsfs_SetError(-EBADF);               
1240         }
1241         
1242         yaffsfs_Unlock();
1243         
1244         return retVal;
1245 }
1246
1247
1248 int yaffs_set_wince_times(int fd, 
1249                                                   const unsigned *wctime, 
1250                                                   const unsigned *watime, 
1251                                                   const unsigned *wmtime)
1252 {
1253         yaffs_Object *obj;
1254         int result;
1255         int retVal = -1;
1256
1257         yaffsfs_Lock();
1258         obj = yaffsfs_GetHandleObject(fd);
1259
1260         if(obj)
1261         {
1262
1263                 if(wctime){
1264                         obj->win_ctime[0] = wctime[0];
1265                         obj->win_ctime[1] = wctime[1];
1266                 }
1267                 if(watime){
1268                         obj->win_atime[0] = watime[0];
1269                         obj->win_atime[1] = watime[1];
1270                 }
1271                 if(wmtime){
1272                         obj->win_mtime[0] = wmtime[0];
1273                         obj->win_mtime[1] = wmtime[1];
1274                 }
1275
1276                 obj->dirty = 1;
1277                 result = yaffs_FlushFile(obj,0,0);
1278                 retVal = 0;
1279         }
1280         else
1281         {
1282                 // bad handle
1283                 yaffsfs_SetError(-EBADF);
1284         }
1285
1286         yaffsfs_Unlock();
1287
1288         return retVal;
1289 }
1290
1291 #endif
1292
1293
1294 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1295 {
1296         int result = -1;
1297
1298         if(obj)
1299         {
1300                 obj = yaffs_GetEquivalentObject(obj);
1301         }
1302
1303         if(obj)
1304         {
1305                 obj->yst_mode = mode;
1306                 obj->dirty = 1;
1307                 result = yaffs_FlushFile(obj,0,0);
1308         }
1309
1310         return result == YAFFS_OK ? 0 : -1;
1311 }
1312
1313
1314 int yaffs_access(const YCHAR *path, int amode)
1315 {
1316         yaffs_Object *obj;
1317
1318         int retval = 0;
1319
1320         yaffsfs_Lock();
1321         obj = yaffsfs_FindObject(NULL,path,0);
1322
1323         if(obj)
1324         {
1325                 int access_ok = 1;
1326
1327                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1328                         access_ok = 0;
1329                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1330                         access_ok = 0;
1331                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1332                         access_ok = 0;
1333
1334                 if(!access_ok) {
1335                         yaffsfs_SetError(-EACCES);
1336                         retval = -1;
1337                 }
1338         }
1339         else
1340         {
1341                 // todo error not found
1342                 yaffsfs_SetError(-ENOENT);
1343                 retval = -1;
1344         }
1345
1346         yaffsfs_Unlock();
1347
1348         return retval;
1349
1350 }
1351
1352
1353 int yaffs_chmod(const YCHAR *path, mode_t mode)
1354 {
1355         yaffs_Object *obj;
1356
1357         int retVal = -1;
1358
1359         yaffsfs_Lock();
1360         obj = yaffsfs_FindObject(NULL,path,0);
1361
1362         if(obj)
1363         {
1364                 retVal = yaffsfs_DoChMod(obj,mode);
1365         }
1366         else
1367         {
1368                 // todo error not found
1369                 yaffsfs_SetError(-ENOENT);
1370         }
1371
1372         yaffsfs_Unlock();
1373
1374         return retVal;
1375
1376 }
1377
1378
1379 int yaffs_fchmod(int fd, mode_t mode)
1380 {
1381         yaffs_Object *obj;
1382
1383         int retVal = -1;
1384
1385         yaffsfs_Lock();
1386         obj = yaffsfs_GetHandleObject(fd);
1387
1388         if(obj)
1389         {
1390                 retVal = yaffsfs_DoChMod(obj,mode);
1391         }
1392         else
1393         {
1394                 // bad handle
1395                 yaffsfs_SetError(-EBADF);
1396         }
1397
1398         yaffsfs_Unlock();
1399
1400         return retVal;
1401 }
1402
1403
1404 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1405 {
1406         yaffs_Object *parent = NULL;
1407         yaffs_Object *dir = NULL;
1408         YCHAR *name;
1409         int retVal= -1;
1410
1411         yaffsfs_Lock();
1412         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1413         if(parent)
1414                 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1415         if(dir)
1416         {
1417                 retVal = 0;
1418         }
1419         else
1420         {
1421                 if(!parent){
1422                         yaffsfs_SetError(-ENOENT); // missing path
1423                 }
1424                 else if (yaffs_FindObjectByName(parent,name)){
1425                         yaffsfs_SetError(-EEXIST); // the name already exists
1426                 }
1427                 else
1428                         yaffsfs_SetError(-ENOSPC); // just assume no space
1429                 retVal = -1;
1430         }
1431
1432         yaffsfs_Unlock();
1433
1434         return retVal;
1435 }
1436
1437 int yaffs_mount(const YCHAR *path)
1438 {
1439         int retVal=-1;
1440         int result=YAFFS_FAIL;
1441         yaffs_Device *dev=NULL;
1442         YCHAR *dummy;
1443
1444         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1445
1446         yaffsfs_Lock();
1447         dev = yaffsfs_FindDevice(path,&dummy);
1448         if(dev)
1449         {
1450                 if(!dev->isMounted)
1451                 {
1452                         result = yaffs_GutsInitialise(dev);
1453                         if(result == YAFFS_FAIL)
1454                         {
1455                                 // todo error - mount failed
1456                                 yaffsfs_SetError(-ENOMEM);
1457                         }
1458                         retVal = result ? 0 : -1;
1459
1460                 }
1461                 else
1462                 {
1463                         //todo error - already mounted.
1464                         yaffsfs_SetError(-EBUSY);
1465                 }
1466         }
1467         else
1468         {
1469                 // todo error - no device
1470                 yaffsfs_SetError(-ENODEV);
1471         }
1472         yaffsfs_Unlock();
1473         return retVal;
1474
1475 }
1476
1477 int yaffs_sync(const YCHAR *path)
1478 {
1479         int retVal=-1;
1480         yaffs_Device *dev=NULL;
1481         YCHAR *dummy;
1482         
1483         yaffsfs_Lock();
1484         dev = yaffsfs_FindDevice(path,&dummy);
1485         if(dev)
1486         {
1487                 if(dev->isMounted)
1488                 {
1489                         
1490                         yaffs_FlushEntireDeviceCache(dev);
1491                         yaffs_CheckpointSave(dev);
1492                         
1493                         
1494                 }
1495                 else
1496                 {
1497                         //todo error - not mounted.
1498                         yaffsfs_SetError(-EINVAL);
1499                         
1500                 }
1501         }
1502         else
1503         {
1504                 // todo error - no device
1505                 yaffsfs_SetError(-ENODEV);
1506         }       
1507         yaffsfs_Unlock();
1508         return retVal;  
1509 }
1510
1511
1512 int yaffs_unmount(const YCHAR *path)
1513 {
1514         int retVal=-1;
1515         yaffs_Device *dev=NULL;
1516         YCHAR *dummy;
1517
1518         yaffsfs_Lock();
1519         dev = yaffsfs_FindDevice(path,&dummy);
1520         if(dev)
1521         {
1522                 if(dev->isMounted)
1523                 {
1524                         int i;
1525                         int inUse;
1526
1527                         yaffs_FlushEntireDeviceCache(dev);
1528                         yaffs_CheckpointSave(dev);
1529
1530                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1531                         {
1532                                 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1533                                 {
1534                                         inUse = 1; // the device is in use, can't unmount
1535                                 }
1536                         }
1537
1538                         if(!inUse)
1539                         {
1540                                 yaffs_Deinitialise(dev);
1541
1542                                 retVal = 0;
1543                         }
1544                         else
1545                         {
1546                                 // todo error can't unmount as files are open
1547                                 yaffsfs_SetError(-EBUSY);
1548                         }
1549
1550                 }
1551                 else
1552                 {
1553                         //todo error - not mounted.
1554                         yaffsfs_SetError(-EINVAL);
1555
1556                 }
1557         }
1558         else
1559         {
1560                 // todo error - no device
1561                 yaffsfs_SetError(-ENODEV);
1562         }
1563         yaffsfs_Unlock();
1564         return retVal;
1565
1566 }
1567
1568 loff_t yaffs_freespace(const YCHAR *path)
1569 {
1570         loff_t retVal=-1;
1571         yaffs_Device *dev=NULL;
1572         YCHAR *dummy;
1573
1574         yaffsfs_Lock();
1575         dev = yaffsfs_FindDevice(path,&dummy);
1576         if(dev  && dev->isMounted)
1577         {
1578                 retVal = yaffs_GetNumberOfFreeChunks(dev);
1579                 retVal *= dev->nDataBytesPerChunk;
1580
1581         }
1582         else
1583         {
1584                 yaffsfs_SetError(-EINVAL);
1585         }
1586
1587         yaffsfs_Unlock();
1588         return retVal;
1589 }
1590
1591 loff_t yaffs_totalspace(const YCHAR *path)
1592 {
1593         loff_t retVal=-1;
1594         yaffs_Device *dev=NULL;
1595         YCHAR *dummy;
1596
1597         yaffsfs_Lock();
1598         dev = yaffsfs_FindDevice(path,&dummy);
1599         if(dev  && dev->isMounted)
1600         {
1601                 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1602                 retVal *= dev->nChunksPerBlock;
1603                 retVal *= dev->nDataBytesPerChunk;
1604
1605         }
1606         else
1607         {
1608                 yaffsfs_SetError(-EINVAL);
1609         }
1610
1611         yaffsfs_Unlock();
1612         return retVal;
1613 }
1614
1615 int yaffs_inodecount(const YCHAR *path)
1616 {
1617         loff_t retVal= -1;
1618         yaffs_Device *dev=NULL;
1619         YCHAR *dummy;
1620
1621         yaffsfs_Lock();
1622         dev = yaffsfs_FindDevice(path,&dummy);
1623         if(dev  && dev->isMounted) {
1624            int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1625            if(nObjects > dev->nHardLinks)
1626                 retVal = nObjects - dev->nHardLinks;
1627         }
1628         
1629         if(retVal < 0){
1630                 yaffsfs_SetError(-EINVAL);
1631         }
1632         
1633         yaffsfs_Unlock();
1634         return retVal;  
1635 }
1636
1637
1638
1639 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1640 {
1641
1642         yaffsfs_DeviceConfiguration *cfg;
1643
1644         yaffsfs_configurationList = cfgList;
1645
1646         yaffsfs_InitHandles();
1647
1648         cfg = yaffsfs_configurationList;
1649
1650         while(cfg && cfg->prefix && cfg->dev)
1651         {
1652                 cfg->dev->isMounted = 0;
1653                 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1654                 cfg++;
1655         }
1656
1657
1658 }
1659
1660
1661 //
1662 // Directory search stuff.
1663
1664 //
1665 // Directory search context
1666 //
1667 // NB this is an opaque structure.
1668
1669
1670 typedef struct
1671 {
1672         __u32 magic;
1673         yaffs_dirent de;                /* directory entry being used by this dsc */
1674         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
1675         yaffs_Object *dirObj;           /* ptr to directory being searched */
1676         yaffs_Object *nextReturn;       /* obj to be returned by next readddir */
1677         int offset;
1678         struct ylist_head others;       
1679 } yaffsfs_DirectorySearchContext;
1680
1681
1682
1683 static struct ylist_head search_contexts;
1684
1685
1686 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1687 {
1688         if(dsc &&
1689            dsc->dirObj &&
1690            dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1691
1692            dsc->offset = 0;
1693
1694            if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1695                 dsc->nextReturn = NULL;
1696            } else {
1697                 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1698                                                 yaffs_Object,siblings);
1699            }
1700         } else {
1701                 /* Hey someone isn't playing nice! */
1702         }
1703 }
1704
1705 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1706 {
1707         if(dsc &&
1708            dsc->dirObj &&
1709            dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1710
1711            if( dsc->nextReturn == NULL ||
1712                ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1713                 dsc->nextReturn = NULL;
1714            } else {
1715                    struct ylist_head *next = dsc->nextReturn->siblings.next;
1716
1717                    if( next == &dsc->dirObj->variant.directoryVariant.children)
1718                         dsc->nextReturn = NULL; /* end of list */
1719                    else
1720                         dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1721            }
1722         } else {
1723                 /* Hey someone isn't playing nice! */
1724         }
1725 }
1726
1727 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1728 {
1729
1730         struct ylist_head *i;
1731         yaffsfs_DirectorySearchContext *dsc;
1732
1733         /* if search contexts not initilised then skip */
1734         if(!search_contexts.next)
1735                 return;
1736
1737         /* Iterate through the directory search contexts.
1738          * If any are the one being removed, then advance the dsc to
1739          * the next one to prevent a hanging ptr.
1740          */
1741          ylist_for_each(i, &search_contexts) {
1742                 if (i) {
1743                         dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1744                         if(dsc->nextReturn == obj)
1745                                 yaffsfs_DirAdvance(dsc);
1746                 }
1747         }
1748
1749 }
1750
1751 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1752 {
1753         yaffs_DIR *dir = NULL;
1754         yaffs_Object *obj = NULL;
1755         yaffsfs_DirectorySearchContext *dsc = NULL;
1756
1757         yaffsfs_Lock();
1758
1759         obj = yaffsfs_FindObject(NULL,dirname,0);
1760
1761         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1762         {
1763
1764                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1765                 dir = (yaffs_DIR *)dsc;
1766                 if(dsc)
1767                 {
1768                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1769                         dsc->magic = YAFFS_MAGIC;
1770                         dsc->dirObj = obj;
1771                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1772                         YINIT_LIST_HEAD(&dsc->others);
1773
1774                         if(!search_contexts.next)
1775                                 YINIT_LIST_HEAD(&search_contexts);
1776
1777                         ylist_add(&dsc->others,&search_contexts);       
1778                         yaffsfs_SetDirRewound(dsc);             }
1779
1780         }
1781
1782         yaffsfs_Unlock();
1783
1784         return dir;
1785 }
1786
1787 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1788 {
1789         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1790         struct yaffs_dirent *retVal = NULL;
1791
1792         yaffsfs_Lock();
1793
1794         if(dsc && dsc->magic == YAFFS_MAGIC){
1795                 yaffsfs_SetError(0);
1796                 if(dsc->nextReturn){
1797                         dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1798                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1799                         dsc->de.d_off = dsc->offset++;
1800                         yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1801                         if(yaffs_strlen(dsc->de.d_name) == 0)
1802                         {
1803                                 // this should not happen!
1804                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1805                         }
1806                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1807                         retVal = &dsc->de;
1808                         yaffsfs_DirAdvance(dsc);
1809                 } else
1810                         retVal = NULL;
1811         }
1812         else
1813         {
1814                 yaffsfs_SetError(-EBADF);
1815         }
1816
1817         yaffsfs_Unlock();
1818
1819         return retVal;
1820
1821 }
1822
1823
1824 void yaffs_rewinddir(yaffs_DIR *dirp)
1825 {
1826         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1827
1828         yaffsfs_Lock();
1829
1830         yaffsfs_SetDirRewound(dsc);
1831
1832         yaffsfs_Unlock();
1833 }
1834
1835
1836 int yaffs_closedir(yaffs_DIR *dirp)
1837 {
1838         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1839
1840         yaffsfs_Lock();
1841         dsc->magic = 0;
1842         ylist_del(&dsc->others); /* unhook from list */
1843         YFREE(dsc);
1844         yaffsfs_Unlock();
1845         return 0;
1846 }
1847
1848 // end of directory stuff
1849
1850
1851 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1852 {
1853         yaffs_Object *parent = NULL;
1854         yaffs_Object *obj;
1855         YCHAR *name;
1856         int retVal= -1;
1857         int mode = 0; // ignore for now
1858
1859         yaffsfs_Lock();
1860         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1861         if(parent){
1862                 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1863                 if(obj)
1864                 {
1865                         retVal = 0;
1866                 }
1867                 else
1868                 {
1869                         yaffsfs_SetError(-ENOSPC); // just assume no space for now
1870                         retVal = -1;
1871                 }
1872         } else {
1873                 yaffsfs_SetError(-EINVAL);
1874                 retVal = -1;
1875         }
1876
1877         yaffsfs_Unlock();
1878
1879         return retVal;
1880
1881 }
1882
1883 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1884 {
1885         yaffs_Object *obj = NULL;
1886         int retVal;
1887
1888
1889         yaffsfs_Lock();
1890
1891         obj = yaffsfs_FindObject(NULL,path,0);
1892
1893         if(!obj)
1894         {
1895                 yaffsfs_SetError(-ENOENT);
1896                 retVal = -1;
1897         }
1898         else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1899         {
1900                 yaffsfs_SetError(-EINVAL);
1901                 retVal = -1;
1902         }
1903         else
1904         {
1905                 YCHAR *alias = obj->variant.symLinkVariant.alias;
1906                 memset(buf,0,bufsiz);
1907                 yaffs_strncpy(buf,alias,bufsiz - 1);
1908                 retVal = 0;
1909         }
1910         yaffsfs_Unlock();
1911         return retVal;
1912 }
1913
1914 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1915 {
1916         // Creates a link called newpath to existing oldpath
1917         yaffs_Object *obj = NULL;
1918         yaffs_Object *target = NULL;
1919         int retVal = 0;
1920
1921
1922         yaffsfs_Lock();
1923
1924         obj = yaffsfs_FindObject(NULL,oldpath,0);
1925         target = yaffsfs_FindObject(NULL,newpath,0);
1926
1927         if(!obj)
1928         {
1929                 yaffsfs_SetError(-ENOENT);
1930                 retVal = -1;
1931         }
1932         else if(target)
1933         {
1934                 yaffsfs_SetError(-EEXIST);
1935                 retVal = -1;
1936         }
1937         else
1938         {
1939                 yaffs_Object *newdir = NULL;
1940                 yaffs_Object *link = NULL;
1941
1942                 YCHAR *newname;
1943
1944                 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1945
1946                 if(!newdir)
1947                 {
1948                         yaffsfs_SetError(-ENOTDIR);
1949                         retVal = -1;
1950                 }
1951                 else if(newdir->myDev != obj->myDev)
1952                 {
1953                         yaffsfs_SetError(-EXDEV);
1954                         retVal = -1;
1955                 }
1956                 if(newdir && yaffs_strlen(newname) > 0)
1957                 {
1958                         link = yaffs_Link(newdir,newname,obj);
1959                         if(link)
1960                                 retVal = 0;
1961                         else
1962                         {
1963                                 yaffsfs_SetError(-ENOSPC);
1964                                 retVal = -1;
1965                         }
1966
1967                 }
1968         }
1969         yaffsfs_Unlock();
1970
1971         return retVal;
1972 }
1973
1974 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev);
1975
1976 int yaffs_DumpDevStruct(const YCHAR *path)
1977 {
1978 #if 0
1979         YCHAR *rest;
1980
1981         yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1982
1983         if(obj)
1984         {
1985                 yaffs_Device *dev = obj->myDev;
1986
1987                 printf("\n"
1988                            "nPageWrites.......... %d\n"
1989                            "nPageReads........... %d\n"
1990                            "nBlockErasures....... %d\n"
1991                            "nGCCopies............ %d\n"
1992                            "garbageCollections... %d\n"
1993                            "passiveGarbageColl'ns %d\n"
1994                            "\n",
1995                                 dev->nPageWrites,
1996                                 dev->nPageReads,
1997                                 dev->nBlockErasures,
1998                                 dev->nGCCopies,
1999                                 dev->garbageCollections,
2000                                 dev->passiveGarbageCollections
2001                 );
2002
2003         }
2004
2005 #endif
2006         return 0;
2007 }
2008