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