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