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