Use the FAT_MAX_SHORT_NAME macro instead of hard-coded 11.
[AROS.git] / rom / filesys / fat / ops.c
blobb4b710650ae3d4b51f925ca15c331c21d2e9c60c
1 /*
2 * fat-handler - FAT12/16/32 filesystem handler
4 * Copyright © 2006 Marek Szyprowski
5 * Copyright © 2007-2015 The AROS Development Team
7 * This program is free software; you can redistribute it and/or modify it
8 * under the same terms as AROS itself.
10 * $Id$
13 #define AROS_ALMOST_COMPATIBLE
15 #include <aros/macros.h>
16 #include <exec/types.h>
17 #include <dos/dos.h>
18 #include <dos/notify.h>
19 #include <proto/exec.h>
21 #include "fat_fs.h"
22 #include "fat_protos.h"
24 #define DEBUG DEBUG_OPS
25 #include "debug.h"
27 #define FREE_CLUSTER_CHAIN(sb,cl) \
28 do { \
29 ULONG cluster = cl; \
30 while (cluster >= 0 && cluster < sb->eoc_mark - 7) { \
31 ULONG next_cluster = GET_NEXT_CLUSTER(sb, cluster); \
32 FreeCluster(sb, cluster); \
33 cluster = next_cluster; \
34 } \
35 } while(0)
38 * this takes a full path and moves to the directory that would contain the
39 * last file in the path. e.g. calling with (dh, "foo/bar/baz", 11) will move to
40 * directory "foo/bar" under the dir specified by dh. dh will become a handle
41 * to the new dir. after the return name will be "baz" and namelen will be 3
43 static LONG MoveToSubdir(struct DirHandle *dh, UBYTE **pname,
44 ULONG *pnamelen)
46 LONG err;
47 UBYTE *name = *pname, *base, ch, *p;
48 ULONG namelen = *pnamelen, baselen;
49 struct DirEntry de;
51 /* Skip device name (if any) */
52 for (ch = *(p = name); ch != ':' && ch != '\0'; ch = *(++p));
53 if (ch == ':')
55 namelen -= (p - name) + 1;
56 name = p + 1;
59 /* we break the given name into two pieces - the name of the containing
60 * dir, and the name of the new dir to go within it. if the base ends up
61 * empty, then we just use the dirlock */
62 baselen = namelen;
63 base = name;
64 while (baselen > 0)
66 if (base[baselen - 1] != '/')
67 break;
68 baselen--;
70 while (baselen > 0)
72 if (base[baselen - 1] == '/')
73 break;
74 baselen--;
76 namelen -= baselen;
77 name = &base[baselen];
80 bug("[fat] base is '");
81 RawPutChars(base, baselen); bug("', name is '");
82 RawPutChars(name, namelen);
83 bug("'\n");
86 if (baselen > 0)
88 if ((err = GetDirEntryByPath(dh, base, baselen, &de)) != 0)
90 D(bug("[fat] base not found\n"));
91 return err;
94 if ((err = InitDirHandle(dh->ioh.sb, FIRST_FILE_CLUSTER(&de), dh,
95 TRUE)) != 0)
96 return err;
99 *pname = name;
100 *pnamelen = namelen;
102 return 0;
105 LONG OpLockFile(struct ExtFileLock *dirlock, UBYTE *name, ULONG namelen,
106 LONG access, struct ExtFileLock **filelock, struct Globals *glob)
108 /* if they passed in a name, go searching for it */
109 if (namelen != 0)
110 return LockFileByName(dirlock, name, namelen, access, filelock,
111 glob);
113 /* otherwise the empty filename, just make a copy */
114 else if (dirlock != NULL)
115 return CopyLock(dirlock, filelock, glob);
117 /* null dir lock means they want the root */
118 else
119 return LockRoot(access, filelock, glob);
122 void OpUnlockFile(struct ExtFileLock *lock, struct Globals *glob)
124 if (lock != NULL)
125 FreeLock(lock, glob);
128 LONG OpCopyLock(struct ExtFileLock *lock, struct ExtFileLock **copy,
129 struct Globals *glob)
131 if (lock != NULL)
132 return CopyLock(lock, copy, glob);
133 else
134 return LockRoot(SHARED_LOCK, copy, glob);
137 LONG OpLockParent(struct ExtFileLock *lock, struct ExtFileLock **parent,
138 struct Globals *glob)
140 LONG err;
141 struct DirHandle dh;
142 struct DirEntry de;
143 ULONG parent_cluster;
145 /* the root has no parent, but as a special case we have to return success
146 * with the zero lock */
147 if (lock == NULL || lock->gl == &glob->sb->info->root_lock)
149 *parent = NULL;
150 return 0;
153 /* if we're in the root directory, then the root is our parent */
154 if (lock->gl->dir_cluster == glob->sb->rootdir_cluster)
155 return LockRoot(SHARED_LOCK, parent, glob);
157 /* get the parent dir */
158 InitDirHandle(glob->sb, lock->gl->dir_cluster, &dh, FALSE);
159 if ((err = GetDirEntryByPath(&dh, "/", 1, &de)) != 0)
161 ReleaseDirHandle(&dh);
162 return err;
165 /* and its cluster */
166 if ((parent_cluster = FIRST_FILE_CLUSTER(&de)) == 0)
167 parent_cluster = glob->sb->rootdir_cluster;
169 /* then we go through the parent dir, looking for a link back to us. we do
170 * this so that we have an entry with the proper name for copying by
171 * LockFile() */
172 InitDirHandle(glob->sb, parent_cluster, &dh, TRUE);
173 while ((err = GetDirEntry(&dh, dh.cur_index + 1, &de)) == 0)
175 /* don't go past the end */
176 if (de.e.entry.name[0] == 0x00)
178 err = ERROR_OBJECT_NOT_FOUND;
179 break;
182 /* we found it if it's not empty, and it's not the volume id or a long
183 * name, and it is a directory, and it does point to us */
184 if (de.e.entry.name[0] != 0xe5 &&
185 !(de.e.entry.attr & ATTR_VOLUME_ID) &&
186 de.e.entry.attr & ATTR_DIRECTORY &&
187 FIRST_FILE_CLUSTER(&de) == lock->gl->dir_cluster)
189 err =
190 LockFile(parent_cluster, dh.cur_index, SHARED_LOCK, parent,
191 glob);
192 break;
196 ReleaseDirHandle(&dh);
197 return err;
201 * obtains a lock on the named file under the given dir. this is the service
202 * routine for DOS Open() (ie FINDINPUT/FINDOUTPUT/FINDUPDATE) and as such may
203 * only return a lock on a file, never on a dir.
205 LONG OpOpenFile(struct ExtFileLock *dirlock, UBYTE *name, ULONG namelen,
206 LONG action, struct ExtFileLock **filelock, struct Globals *glob)
208 LONG err;
209 struct ExtFileLock *lock;
210 struct DirHandle dh;
211 struct DirEntry de;
214 bug("[fat] opening file '");
215 RawPutChars(name, namelen);
216 bug("' in dir at cluster %ld, action %s\n",
217 dirlock != NULL ? dirlock->ioh.first_cluster : 0,
218 action == ACTION_FINDINPUT ? "FINDINPUT" :
219 action == ACTION_FINDOUTPUT ? "FINDOUTPUT" :
220 action == ACTION_FINDUPDATE ? "FINDUPDATE" : "[unknown]");
223 /* Explicitly mark the dirhandle as uninitialised */
224 dh.ioh.sb = NULL;
226 /* no filename means they're trying to open whatever dirlock is (which
227 * despite the name may not actually be a dir). since there's already an
228 * extant lock, it's never going to be possible to get an exclusive lock,
229 * so this will only work for FINDINPUT (read-only) */
230 if (namelen == 0)
232 D(bug("[fat] trying to copy passed dir lock\n"));
234 if (action != ACTION_FINDINPUT)
236 D(bug("[fat] can't copy lock for write (exclusive)\n"));
237 return ERROR_OBJECT_IN_USE;
240 /* dirs can't be opened */
241 if (dirlock == NULL || dirlock->gl->attr & ATTR_DIRECTORY)
243 D(bug("[fat] dir lock is a directory, which can't be opened\n"));
244 return ERROR_OBJECT_WRONG_TYPE;
247 /* it's a file, just copy the lock */
248 return CopyLock(dirlock, filelock, glob);
251 /* lock the file */
252 err = LockFileByName(dirlock, name, namelen,
253 action == ACTION_FINDINPUT ? SHARED_LOCK : EXCLUSIVE_LOCK, &lock,
254 glob);
256 /* found it */
257 if (err == 0)
259 D(bug("[fat] found existing file\n"));
261 /* can't open directories */
262 if (lock->gl->attr & ATTR_DIRECTORY)
264 D(bug("[fat] it's a directory, can't open it\n"));
265 FreeLock(lock, glob);
266 return ERROR_OBJECT_WRONG_TYPE;
269 /* INPUT/UPDATE use the file as/is */
270 if (action != ACTION_FINDOUTPUT)
272 D(bug("[fat] returning the lock\n"));
273 *filelock = lock;
274 return 0;
277 /* whereas OUTPUT truncates it */
278 D(bug("[fat] handling FINDOUTPUT, so truncating the file\n"));
280 if (lock->gl->attr & ATTR_READ_ONLY)
282 D(bug("[fat] file is write protected, doing nothing\n"));
283 FreeLock(lock, glob);
284 return ERROR_WRITE_PROTECTED;
287 /* update the dir entry to make the file empty */
288 InitDirHandle(lock->ioh.sb, lock->gl->dir_cluster, &dh, FALSE);
289 GetDirEntry(&dh, lock->gl->dir_entry, &de);
290 de.e.entry.first_cluster_lo = de.e.entry.first_cluster_hi = 0;
291 de.e.entry.file_size = 0;
292 de.e.entry.attr |= ATTR_ARCHIVE;
293 UpdateDirEntry(&de);
295 D(bug("[fat] set first cluster and size to 0 in directory entry\n"));
297 /* free the clusters */
298 FREE_CLUSTER_CHAIN(lock->ioh.sb, lock->ioh.first_cluster);
299 lock->gl->first_cluster = lock->ioh.first_cluster = 0xffffffff;
300 RESET_HANDLE(&lock->ioh);
301 lock->gl->size = 0;
303 D(bug("[fat] file truncated, returning the lock\n"));
305 /* file is empty, go */
306 *filelock = lock;
308 return 0;
311 /* any error other than "not found" should be taken as-is */
312 if (err != ERROR_OBJECT_NOT_FOUND)
313 return err;
315 /* not found. for INPUT we bail out */
316 if (action == ACTION_FINDINPUT)
318 D(bug("[fat] file not found, and not creating it\n"));
319 return ERROR_OBJECT_NOT_FOUND;
323 bug("[fat] trying to create '");
324 RawPutChars(name, namelen);
325 bug("'\n");
328 /* otherwise it's time to create the file. get a handle on the passed dir */
329 if ((err = InitDirHandle(glob->sb,
330 dirlock != NULL ? dirlock->ioh.first_cluster : 0, &dh, TRUE)) != 0)
331 return err;
333 /* get down to the correct subdir */
334 if ((err = MoveToSubdir(&dh, &name, &namelen)) != 0)
336 ReleaseDirHandle(&dh);
337 return err;
340 /* if the dir is write protected, can't do anything. root dir is never
341 * write protected */
342 if (dh.ioh.first_cluster != dh.ioh.sb->rootdir_cluster)
344 GetDirEntry(&dh, 0, &de);
345 if (de.e.entry.attr & ATTR_READ_ONLY)
347 D(bug("[fat] containing dir is write protected, doing nothing\n"));
348 ReleaseDirHandle(&dh);
349 return ERROR_WRITE_PROTECTED;
353 /* create the entry */
354 if ((err =
355 CreateDirEntry(&dh, name, namelen, ATTR_ARCHIVE, 0, &de)) != 0)
357 ReleaseDirHandle(&dh);
358 return err;
361 /* lock the new file */
362 err = LockFile(de.cluster, de.index, EXCLUSIVE_LOCK, filelock, glob);
364 /* done */
365 ReleaseDirHandle(&dh);
367 if (err == 0)
369 (*filelock)->do_notify = TRUE;
370 D(bug("[fat] returning lock on new file\n"));
373 return err;
376 /* find the named file in the directory referenced by dirlock, and delete it.
377 * if the file is a directory, it will only be deleted if it's empty */
378 LONG OpDeleteFile(struct ExtFileLock *dirlock, UBYTE *name, ULONG namelen,
379 struct Globals *glob)
381 LONG err;
382 struct ExtFileLock *lock;
383 struct DirHandle dh;
384 struct DirEntry de;
387 bug("[fat] deleting file '");
388 RawPutChars(name, namelen);
389 bug("' in directory at cluster % ld\n",
390 dirlock != NULL ? dirlock->ioh.first_cluster : 0);
393 dh.ioh.sb = NULL;
395 /* obtain a lock on the file. we need an exclusive lock as we don't want
396 * to delete the file if it's in use */
397 if ((err = LockFileByName(dirlock, name, namelen, EXCLUSIVE_LOCK, &lock,
398 glob)) != 0)
400 D(bug("[fat] couldn't obtain exclusive lock on named file\n"));
401 return err;
404 if (lock->gl->attr & ATTR_READ_ONLY)
406 D(bug("[fat] file is write protected, doing nothing\n"));
407 FreeLock(lock, glob);
408 return ERROR_DELETE_PROTECTED;
411 /* if it's a directory, we have to make sure it's empty */
412 if (lock->gl->attr & ATTR_DIRECTORY)
414 D(bug("[fat] file is a directory, making sure it's empty\n"));
416 if ((err = InitDirHandle(lock->ioh.sb, lock->ioh.first_cluster, &dh,
417 FALSE)) != 0)
419 FreeLock(lock, glob);
420 return err;
423 /* loop over the entries, starting from entry 2 (the first real
424 * entry). skipping unused ones, we look for the end-of-directory
425 * marker. if we find it, the directory is empty. if we find a real
426 * name, it's in use */
427 de.index = 1;
428 while ((err = GetDirEntry(&dh, de.index + 1, &de)) == 0)
430 /* skip unused entries */
431 if (de.e.entry.name[0] == 0xe5)
432 continue;
434 /* end of directory, it's empty */
435 if (de.e.entry.name[0] == 0x00)
436 break;
438 /* otherwise the directory is still in use */
439 D(bug("[fat] directory still has files in it, won't delete it\n"));
441 ReleaseDirHandle(&dh);
442 FreeLock(lock, glob);
443 return ERROR_DIRECTORY_NOT_EMPTY;
446 ReleaseDirHandle(&dh);
449 /* open the containing directory */
450 if ((err =InitDirHandle(lock->ioh.sb, lock->gl->dir_cluster, &dh,
451 TRUE)) != 0)
453 FreeLock(lock, glob);
454 return err;
457 /* if the dir is write protected, can't do anything. root dir is never
458 * write protected */
459 if (dh.ioh.first_cluster != dh.ioh.sb->rootdir_cluster)
461 GetDirEntry(&dh, 0, &de);
462 if (de.e.entry.attr & ATTR_READ_ONLY)
464 D(bug("[fat] containing dir is write protected, doing nothing\n"));
465 ReleaseDirHandle(&dh);
466 FreeLock(lock, glob);
467 return ERROR_WRITE_PROTECTED;
471 /* get the entry for the file */
472 GetDirEntry(&dh, lock->gl->dir_entry, &de);
474 /* kill it */
475 DeleteDirEntry(&de);
477 /* it's all good */
478 ReleaseDirHandle(&dh);
480 /* now free the clusters the file was using */
481 FREE_CLUSTER_CHAIN(lock->ioh.sb, lock->ioh.first_cluster);
483 /* notify */
484 SendNotifyByLock(lock->ioh.sb, lock->gl);
486 /* this lock is now completely meaningless */
487 FreeLock(lock, glob);
490 bug("[fat] deleted '");
491 RawPutChars(name, namelen);
492 bug("'\n");
495 return 0;
498 LONG OpRenameFile(struct ExtFileLock *sdirlock, UBYTE *sname,
499 ULONG snamelen, struct ExtFileLock *ddirlock, UBYTE *dname,
500 ULONG dnamelen, struct Globals *glob)
502 struct DirHandle sdh, ddh;
503 struct DirEntry sde, dde;
504 struct GlobalLock *gl;
505 LONG err;
506 ULONG len;
508 /* get the source dir handle */
509 if ((err = InitDirHandle(glob->sb,
510 sdirlock != NULL ? sdirlock->ioh.first_cluster : 0, &sdh,
511 FALSE)) != 0)
512 return err;
514 /* get down to the correct subdir */
515 if ((err = MoveToSubdir(&sdh, &sname, &snamelen)) != 0)
517 ReleaseDirHandle(&sdh);
518 return err;
521 /* get the entry */
522 if ((err = GetDirEntryByName(&sdh, sname, snamelen, &sde)) != 0)
524 ReleaseDirHandle(&sdh);
525 return err;
528 /* now get a handle on the passed dest dir */
529 if ((err = InitDirHandle(glob->sb,
530 ddirlock != NULL ? ddirlock->ioh.first_cluster : 0, &ddh,
531 FALSE)) != 0)
533 ReleaseDirHandle(&sdh);
534 return err;
537 /* get down to the correct subdir */
538 if ((err = MoveToSubdir(&ddh, &dname, &dnamelen)) != 0)
540 ReleaseDirHandle(&ddh);
541 ReleaseDirHandle(&sdh);
542 return err;
545 /* check the source and dest dirs. if either is read-only, do nothing */
546 GetDirEntry(&sdh, 0, &dde);
547 if (dde.e.entry.attr & ATTR_READ_ONLY)
549 D(bug("[fat] source dir is read only, doing nothing\n"));
550 ReleaseDirHandle(&ddh);
551 ReleaseDirHandle(&sdh);
552 return ERROR_WRITE_PROTECTED;
554 GetDirEntry(&ddh, 0, &dde);
555 if (dde.e.entry.attr & ATTR_READ_ONLY)
557 D(bug("[fat] dest dir is read only, doing nothing\n"));
558 ReleaseDirHandle(&ddh);
559 ReleaseDirHandle(&sdh);
560 return ERROR_WRITE_PROTECTED;
563 /* now see if the wanted name is in this dir. if it exists, do nothing */
564 if ((err = GetDirEntryByName(&ddh, dname, dnamelen, &dde)) == 0)
566 ReleaseDirHandle(&ddh);
567 ReleaseDirHandle(&sdh);
568 return ERROR_OBJECT_EXISTS;
570 else if (err != ERROR_OBJECT_NOT_FOUND)
572 ReleaseDirHandle(&ddh);
573 ReleaseDirHandle(&sdh);
574 return err;
577 /* at this point we have the source entry in sde, and we know the dest
578 * doesn't exist */
580 /* XXX if sdh and ddh are the same dir and there's room in the existing
581 * entries for the new name, just overwrite the name */
583 /* make a new entry in the target dir */
584 if ((err = CreateDirEntry(&ddh, dname, dnamelen,
585 sde.e.entry.attr | ATTR_ARCHIVE,
586 (sde.e.entry.first_cluster_hi << 16) | sde.e.entry.first_cluster_lo,
587 &dde)) != 0)
589 ReleaseDirHandle(&ddh);
590 ReleaseDirHandle(&sdh);
593 /* copy in the leftover attributes */
594 dde.e.entry.create_date = sde.e.entry.create_date;
595 dde.e.entry.create_time = sde.e.entry.create_time;
596 dde.e.entry.write_date = sde.e.entry.write_date;
597 dde.e.entry.write_time = sde.e.entry.write_time;
598 dde.e.entry.last_access_date = sde.e.entry.last_access_date;
599 dde.e.entry.create_time_tenth = sde.e.entry.create_time_tenth;
600 dde.e.entry.file_size = sde.e.entry.file_size;
602 UpdateDirEntry(&dde);
604 /* update the global lock (if present) with the new dir cluster/entry */
605 ForeachNode(&sdh.ioh.sb->info->locks, gl)
607 if (gl->dir_cluster == sde.cluster && gl->dir_entry == sde.index)
609 D(bug("[fat] found lock with old dir entry (%ld/%ld),"
610 " changing to (%ld/%ld)\n",
611 sde.cluster, sde.index, dde.cluster, dde.index));
613 gl->dir_cluster = dde.cluster;
614 gl->dir_entry = dde.index;
616 /* update the filename too */
617 GetDirEntryShortName(&dde, &(gl->name[1]), &len);
618 gl->name[0] = (UBYTE) len;
619 GetDirEntryLongName(&dde, &(gl->name[1]), &len);
620 gl->name[0] = (UBYTE) len;
624 /* delete the original */
625 DeleteDirEntry(&sde);
627 /* notify */
628 SendNotifyByDirEntry(sdh.ioh.sb, &dde);
630 ReleaseDirHandle(&ddh);
631 ReleaseDirHandle(&sdh);
633 return 0;
636 LONG OpCreateDir(struct ExtFileLock *dirlock, UBYTE *name, ULONG namelen,
637 struct ExtFileLock **newdirlock, struct Globals *glob)
639 LONG err, i;
640 ULONG cluster;
641 struct DirHandle dh, sdh;
642 struct DirEntry de, sde;
645 bug("[fat] creating directory '");
646 RawPutChars(name, namelen);
647 bug("' in directory at cluster %ld\n",
648 dirlock != NULL ? dirlock->ioh.first_cluster : 0);
651 /* get a handle on the passed dir */
652 if ((err = InitDirHandle(glob->sb,
653 dirlock != NULL ? dirlock->ioh.first_cluster : 0, &dh, FALSE)) != 0)
654 return err;
656 /* get down to the correct subdir */
657 if ((err = MoveToSubdir(&dh, &name, &namelen)) != 0)
659 ReleaseDirHandle(&dh);
660 return err;
663 /* Make sure 'name' is just the FilePart() */
664 for (i = namelen - 1; i > 0; i--)
666 if (name[i] == '/' || name[i] == ':')
668 namelen -= (i + 1);
669 name += (i + 1);
670 break;
674 /* if the dir is write protected, can't do anything. root dir is never
675 * write protected */
676 if (dh.ioh.first_cluster != dh.ioh.sb->rootdir_cluster)
678 GetDirEntry(&dh, 0, &de);
679 if (de.e.entry.attr & ATTR_READ_ONLY)
681 D(bug("[fat] containing dir is write protected, doing nothing\n"));
682 ReleaseDirHandle(&dh);
683 return ERROR_WRITE_PROTECTED;
687 /* now see if the wanted name is in this dir. if it exists, then we do
688 * nothing */
689 if ((err = GetDirEntryByName(&dh, name, namelen, &de)) == 0)
691 D(bug("[fat] name exists, can't do anything\n"));
692 ReleaseDirHandle(&dh);
693 return ERROR_OBJECT_EXISTS;
696 /* find a free cluster to store the dir in */
697 if ((err = FindFreeCluster(dh.ioh.sb, &cluster)) != 0)
699 ReleaseDirHandle(&dh);
700 return err;
703 /* allocate it */
704 AllocCluster(dh.ioh.sb, cluster);
706 D(bug("[fat] allocated cluster %ld for directory\n", cluster));
708 /* create the entry, pointing to the new cluster */
709 if ((err = CreateDirEntry(&dh, name, namelen,
710 ATTR_DIRECTORY | ATTR_ARCHIVE, cluster, &de)) != 0)
712 /* deallocate the cluster */
713 FreeCluster(dh.ioh.sb, cluster);
715 ReleaseDirHandle(&dh);
716 return err;
719 /* now get a handle on the new directory */
720 InitDirHandle(dh.ioh.sb, cluster, &sdh, FALSE);
722 /* create the dot entry. it's a direct copy of the just-created entry, but
723 * with a different name */
724 GetDirEntry(&sdh, 0, &sde);
725 CopyMem(&de.e.entry, &sde.e.entry, sizeof(struct FATDirEntry));
726 CopyMem(". ", &sde.e.entry.name, FAT_MAX_SHORT_NAME);
727 UpdateDirEntry(&sde);
729 /* create the dot-dot entry. again, a copy, with the cluster pointer setup
730 * to point to the parent */
731 GetDirEntry(&sdh, 1, &sde);
732 CopyMem(&de.e.entry, &sde.e.entry, sizeof(struct FATDirEntry));
733 CopyMem(".. ", &sde.e.entry.name, FAT_MAX_SHORT_NAME);
734 cluster = dh.ioh.first_cluster;
735 if (cluster == dh.ioh.sb->rootdir_cluster)
736 cluster = 0;
737 sde.e.entry.first_cluster_lo = cluster & 0xffff;
738 sde.e.entry.first_cluster_hi = cluster >> 16;
739 UpdateDirEntry(&sde);
741 /* clear all remaining entries (the first of which marks the end of the
742 * directory) */
743 for (i = 2; GetDirEntry(&sdh, i, &sde) == 0; i++)
745 memset(&sde.e.entry, 0, sizeof(struct FATDirEntry));
746 UpdateDirEntry(&sde);
749 /* new dir created */
750 ReleaseDirHandle(&sdh);
752 /* now obtain a lock on the new dir */
753 err = LockFile(de.cluster, de.index, SHARED_LOCK, newdirlock, glob);
755 /* done */
756 ReleaseDirHandle(&dh);
758 /* notify */
759 SendNotifyByLock((*newdirlock)->ioh.sb, (*newdirlock)->gl);
761 return err;
764 LONG OpRead(struct ExtFileLock *lock, UBYTE *data, ULONG want,
765 ULONG *read, struct Globals *glob)
767 LONG err;
769 D(bug("[fat] request to read %ld bytes from file pos %ld\n", want,
770 lock->pos));
772 if (want == 0)
773 return 0;
775 if (want + lock->pos > lock->gl->size)
777 want = lock->gl->size - lock->pos;
778 D(bug("[fat] full read would take us past end-of-file,"
779 " adjusted want to %ld bytes\n", want));
782 if ((err = ReadFileChunk(&(lock->ioh), lock->pos, want, data, read)) == 0)
784 lock->pos += *read;
785 D(bug("[fat] read %ld bytes, new file pos is %ld\n", *read,
786 lock->pos));
789 return err;
792 LONG OpWrite(struct ExtFileLock *lock, UBYTE *data, ULONG want,
793 ULONG *written, struct Globals *glob)
795 LONG err;
796 BOOL update_entry = FALSE;
797 struct DirHandle dh;
798 struct DirEntry de;
800 D(bug("[fat] request to write %ld bytes to file pos %ld\n", want,
801 lock->pos));
803 /* need an exclusive lock */
804 if (lock->gl->access != EXCLUSIVE_LOCK)
806 D(bug("[fat] can't modify global attributes via a shared lock\n"));
807 return ERROR_OBJECT_IN_USE;
810 /* don't modify the file if it's protected */
811 if (lock->gl->attr & ATTR_READ_ONLY)
813 D(bug("[fat] file is write protected\n"));
814 return ERROR_WRITE_PROTECTED;
817 if (want == 0)
819 *written = 0;
820 return 0;
823 /* if this is the first write, make a note as we'll have to store the
824 * first cluster in the directory entry later */
825 if (lock->ioh.first_cluster == 0xffffffff)
826 update_entry = TRUE;
828 if ((err = WriteFileChunk(&(lock->ioh), lock->pos, want, data,
829 written)) == 0)
831 /* if nothing was written but success was returned (can that even
832 * happen?) then we don't want to mess with the dir entry */
833 if (*written == 0)
835 D(bug("[fat] nothing successfully written (!),"
836 " nothing else to do\n"));
837 return 0;
840 /* something changed, we need to tell people about it */
841 lock->do_notify = TRUE;
843 /* move to the end of the area written */
844 lock->pos += *written;
846 /* update the dir entry if the size changed */
847 if (lock->pos > lock->gl->size)
849 lock->gl->size = lock->pos;
850 update_entry = TRUE;
853 /* force an update if the file hasn't already got an archive bit. this
854 * will happen if this was the first write to an existing file that
855 * didn't cause it to grow */
856 else if (!(lock->gl->attr & ATTR_ARCHIVE))
857 update_entry = TRUE;
859 D(bug("[fat] wrote %ld bytes, new file pos is %ld, size is %ld\n",
860 *written, lock->pos, lock->gl->size));
862 if (update_entry)
864 D(bug("[fat] updating dir entry, first cluster is %ld,"
865 " size is %ld\n",
866 lock->ioh.first_cluster, lock->gl->size));
868 lock->gl->first_cluster = lock->ioh.first_cluster;
870 InitDirHandle(lock->ioh.sb, lock->gl->dir_cluster, &dh, FALSE);
871 GetDirEntry(&dh, lock->gl->dir_entry, &de);
873 de.e.entry.file_size = lock->gl->size;
874 de.e.entry.first_cluster_lo = lock->gl->first_cluster & 0xffff;
875 de.e.entry.first_cluster_hi = lock->gl->first_cluster >> 16;
877 de.e.entry.attr |= ATTR_ARCHIVE;
878 UpdateDirEntry(&de);
880 ReleaseDirHandle(&dh);
884 return err;
887 LONG OpSetFileSize(struct ExtFileLock *lock, LONG offset, LONG whence,
888 LONG *newsize, struct Globals *glob)
890 LONG err;
891 LONG size;
892 struct DirHandle dh;
893 struct DirEntry de;
894 ULONG want, count;
895 ULONG cl, next, first, last;
897 /* need an exclusive lock to do what is effectively a write */
898 if (lock->gl->access != EXCLUSIVE_LOCK)
900 D(bug("[fat] can't modify global attributes via a shared lock\n"));
901 return ERROR_OBJECT_IN_USE;
904 /* don't modify the file if it's protected */
905 if (lock->gl->attr & ATTR_READ_ONLY)
907 D(bug("[fat] file is write protected\n"));
908 return ERROR_WRITE_PROTECTED;
911 /* calculate the new length based on the current position */
912 if (whence == OFFSET_BEGINNING && offset >= 0)
913 size = offset;
914 else if (whence == OFFSET_CURRENT && lock->pos + offset >= 0)
915 size = lock->pos + offset;
916 else if (whence == OFFSET_END && offset <= 0
917 && lock->gl->size + offset >= 0)
918 size = lock->gl->size + offset;
919 else
920 return ERROR_SEEK_ERROR;
922 if (lock->gl->size == size)
924 D(bug("[fat] new size matches old size, nothing to do\n"));
925 *newsize = size;
926 return 0;
929 D(bug("[fat] old size was %ld bytes, new size is %ld bytes\n",
930 lock->gl->size, size));
932 /* get the dir that this file is in */
933 if ((err = InitDirHandle(glob->sb, lock->gl->dir_cluster, &dh,
934 FALSE)) != 0)
935 return err;
937 /* and the entry */
938 if ((err = GetDirEntry(&dh, lock->gl->dir_entry, &de)) != 0)
940 ReleaseDirHandle(&dh);
941 return err;
944 /* calculate how many clusters we need */
945 want = (size >> glob->sb->clustersize_bits)
946 + ((size & (glob->sb->clustersize - 1)) ? 1 : 0);
948 D(bug("[fat] want %ld clusters for file\n", want));
950 /* we're getting three things here - the first cluster of the existing
951 * file, the last cluster of the existing file (which might be the same),
952 * and the number of clusters currently allocated to it (it's not safe to
953 * infer it from the current size as a broken fat implementation may have
954 * allocated it more than it needs). we handle file shrinking/truncation
955 * here as it falls out naturally from following the current cluster chain
958 cl = FIRST_FILE_CLUSTER(&de);
959 if (cl == 0)
961 D(bug("[fat] file is empty\n"));
963 first = 0;
964 count = 0;
967 else if (want == 0)
969 /* if we're fully truncating the file, then the below loop will
970 * actually not truncate the file at all (count will get incremented
971 * past want first time around the loop). it's a pain to incorporate a
972 * full truncate into the loop, not counting the change to the first
973 * cluster, so it's easier to just take care of it all here */
974 D(bug("[fat] want nothing, so truncating the entire file\n"));
976 FREE_CLUSTER_CHAIN(glob->sb, cl);
978 /* now it has nothing */
979 first = 0;
980 count = 0;
983 else
985 first = cl;
986 count = 0;
988 /* do the actual count */
989 while ((last = GET_NEXT_CLUSTER(glob->sb, cl))
990 < glob->sb->eoc_mark - 7)
992 count++;
993 cl = last;
995 /* if we get as many clusters as we want, kill everything after
996 * it */
997 if (count == want)
999 FREE_CLUSTER_CHAIN(glob->sb, GET_NEXT_CLUSTER(glob->sb, cl));
1000 SET_NEXT_CLUSTER(glob->sb, cl, glob->sb->eoc_mark);
1002 D(bug("[fat] truncated file\n"));
1004 break;
1008 D(bug("[fat] file has %ld clusters\n", count));
1011 /* now we know how big the current file is. if we don't have enough,
1012 * allocate more until we do */
1013 if (count < want)
1015 D(bug("[fat] growing file\n"));
1017 while (count < want)
1019 if ((err = FindFreeCluster(glob->sb, &next)) != 0)
1021 /* XXX probably no free clusters left. we should clean up the
1022 * extras we allocated before returning. it won't hurt
1023 * anything to leave them but it is dead space */
1024 ReleaseDirHandle(&dh);
1025 return err;
1028 /* mark the cluster used */
1029 AllocCluster(glob->sb, next);
1031 /* if the file had no clusters, then this is the first and we
1032 * need to note it for later storage in the direntry */
1033 if (cl == 0)
1034 first = next;
1036 /* otherwise, hook it up to the current one */
1037 else
1038 SET_NEXT_CLUSTER(glob->sb, cl, next);
1040 /* one more */
1041 count++;
1042 cl = next;
1046 /* clusters are fixed, now update the directory entry */
1047 de.e.entry.first_cluster_lo = first & 0xffff;
1048 de.e.entry.first_cluster_hi = first >> 16;
1049 de.e.entry.file_size = size;
1050 de.e.entry.attr |= ATTR_ARCHIVE;
1051 UpdateDirEntry(&de);
1053 D(bug("[fat] set file size to %ld, first cluster is %ld\n", size,
1054 first));
1056 /* done! */
1057 *newsize = size;
1059 return 0;
1062 LONG OpSetProtect(struct ExtFileLock *dirlock, UBYTE *name, ULONG namelen,
1063 ULONG prot, struct Globals *glob)
1065 LONG err;
1066 struct DirHandle dh;
1067 struct DirEntry de;
1069 /* get the dir handle */
1070 if ((err = InitDirHandle(glob->sb,
1071 dirlock != NULL ? dirlock->ioh.first_cluster : 0, &dh, FALSE)) != 0)
1072 return err;
1074 /* get down to the correct subdir */
1075 if ((err = MoveToSubdir(&dh, &name, &namelen)) != 0)
1077 ReleaseDirHandle(&dh);
1078 return err;
1081 /* can't change permissions on the root */
1082 if (dh.ioh.first_cluster == dh.ioh.sb->rootdir_cluster && namelen == 0)
1084 D(bug("[fat] can't set protection on root dir\n"));
1085 ReleaseDirHandle(&dh);
1086 return ERROR_INVALID_LOCK;
1089 /* get the entry */
1090 if ((err = GetDirEntryByName(&dh, name, namelen, &de)) != 0)
1092 ReleaseDirHandle(&dh);
1093 return err;
1096 /* set the attributes */
1097 de.e.entry.attr &= ~(ATTR_ARCHIVE | ATTR_READ_ONLY);
1098 de.e.entry.attr |= (prot & FIBF_ARCHIVE ? ATTR_ARCHIVE : 0);
1100 /* only set read-only if neither writable nor deletable */
1101 if ((prot & (FIBF_WRITE | FIBF_DELETE)) == (FIBF_WRITE | FIBF_DELETE))
1102 de.e.entry.attr |= ATTR_READ_ONLY;
1103 UpdateDirEntry(&de);
1105 D(bug("[fat] new protection is 0x%08x\n", de.e.entry.attr));
1107 SendNotifyByDirEntry(glob->sb, &de);
1109 /* if it's a directory, we also need to update the protections for the
1110 * directory's . entry */
1111 if (de.e.entry.attr & ATTR_DIRECTORY)
1113 ULONG attr = de.e.entry.attr;
1115 D(bug("[fat] setting protections for directory '.' entry\n"));
1117 InitDirHandle(glob->sb, FIRST_FILE_CLUSTER(&de), &dh, TRUE);
1118 GetDirEntry(&dh, 0, &de);
1119 de.e.entry.attr = attr;
1120 UpdateDirEntry(&de);
1123 ReleaseDirHandle(&dh);
1125 return 0;
1128 LONG OpSetDate(struct ExtFileLock *dirlock, UBYTE *name, ULONG namelen,
1129 struct DateStamp *ds, struct Globals *glob)
1131 LONG err;
1132 struct DirHandle dh;
1133 struct DirEntry de;
1135 /* get the dir handle */
1136 if ((err = InitDirHandle(glob->sb,
1137 dirlock != NULL ? dirlock->ioh.first_cluster : 0, &dh, FALSE)) != 0)
1138 return err;
1140 /* get down to the correct subdir */
1141 if ((err = MoveToSubdir(&dh, &name, &namelen)) != 0)
1143 ReleaseDirHandle(&dh);
1144 return err;
1147 /* can't set date on the root */
1148 if (dh.ioh.first_cluster == dh.ioh.sb->rootdir_cluster && namelen == 0)
1150 D(bug("[fat] can't set date on root dir\n"));
1151 ReleaseDirHandle(&dh);
1152 return ERROR_INVALID_LOCK;
1155 /* get the entry */
1156 if ((err = GetDirEntryByName(&dh, name, namelen, &de)) != 0)
1158 ReleaseDirHandle(&dh);
1159 return err;
1162 /* set and update the date */
1163 ConvertDOSDate(ds, &de.e.entry.write_date, &de.e.entry.write_time,
1164 glob);
1165 de.e.entry.last_access_date = de.e.entry.write_date;
1166 UpdateDirEntry(&de);
1168 SendNotifyByDirEntry(glob->sb, &de);
1170 ReleaseDirHandle(&dh);
1172 return 0;
1175 LONG OpAddNotify(struct NotifyRequest *nr, struct Globals *glob)
1177 LONG err;
1178 struct DirHandle dh;
1179 struct DirEntry de;
1180 struct GlobalLock *gl = NULL, *tmp;
1181 struct NotifyNode *nn;
1182 BOOL exists = FALSE;
1184 D(bug("[fat] trying to add notification for '%s'\n", nr->nr_FullName));
1186 /* if the request is for the volume root, then we just link to the root
1187 * lock */
1188 if (nr->nr_FullName[strlen(nr->nr_FullName) - 1] == ':')
1190 D(bug("[fat] adding notify for root dir\n"));
1191 gl = &glob->sb->info->root_lock;
1194 else
1196 if ((err = InitDirHandle(glob->sb, 0, &dh, FALSE)) != 0)
1197 return err;
1199 /* look for the entry */
1200 err =
1201 GetDirEntryByPath(&dh, nr->nr_FullName, strlen(nr->nr_FullName),
1202 &de);
1203 if (err != 0 && err != ERROR_OBJECT_NOT_FOUND)
1204 return err;
1206 /* if it was found, then it might be open. try to find the global
1207 * lock */
1208 if (err == 0)
1210 exists = TRUE;
1212 D(bug("[fat] file exists (%ld/%ld), looking for global lock\n",
1213 de.cluster, de.index));
1215 ForeachNode(&glob->sb->info->locks, tmp)
1217 if (tmp->dir_cluster == de.cluster
1218 && tmp->dir_entry == de.index)
1220 gl = tmp;
1222 D(bug("[fat] found global lock 0x%0x\n", gl));
1224 break;
1229 else
1231 exists = FALSE;
1233 D(bug("[fat] file doesn't exist\n"));
1237 if (gl == NULL)
1238 D(bug("[fat] file not currently locked\n"));
1240 /* allocate space for the notify node */
1241 if ((nn = AllocVecPooled(glob->sb->info->mem_pool,
1242 sizeof(struct NotifyNode))) == NULL)
1243 return ERROR_NO_FREE_STORE;
1245 /* plug the bits in */
1246 nn->gl = gl;
1247 nn->nr = nr;
1249 /* add to the list */
1250 ADDTAIL(&glob->sb->info->notifies, nn);
1252 /* tell them that the file exists if they wanted to know */
1253 if (exists && nr->nr_Flags & NRF_NOTIFY_INITIAL)
1254 SendNotify(nr, glob);
1256 D(bug("[fat] now reporting activity on '%s'\n", nr->nr_FullName));
1258 return 0;
1261 LONG OpRemoveNotify(struct NotifyRequest *nr, struct Globals *glob)
1263 struct FSSuper *sb;
1264 struct NotifyNode *nn, *nn2;
1266 D(bug("[fat] trying to remove notification for '%s'\n",
1267 nr->nr_FullName));
1269 /* search inserted volume for the request */
1270 if (glob->sb != NULL)
1272 ForeachNodeSafe(&glob->sb->info->notifies, nn, nn2)
1274 if (nn->nr == nr)
1276 D(bug("[fat] found notify request in list, removing it\n"));
1277 REMOVE(nn);
1278 FreeVecPooled(glob->sb->info->mem_pool, nn);
1279 return 0;
1284 /* search offline volumes for the request */
1285 ForeachNode(&glob->sblist, sb)
1287 ForeachNodeSafe(&sb->info->notifies, nn, nn2)
1289 if (nn->nr == nr)
1291 D(bug("[fat] found notify request in list, removing it\n"));
1292 REMOVE(nn);
1293 FreeVecPooled(sb->info->mem_pool, nn);
1294 AttemptDestroyVolume(sb);
1295 return 0;
1300 D(bug("[fat] not found, doing nothing\n"));
1302 return 0;