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.
13 #define AROS_ALMOST_COMPATIBLE
15 #include <aros/macros.h>
16 #include <exec/types.h>
18 #include <dos/notify.h>
19 #include <proto/exec.h>
22 #include "fat_protos.h"
24 #define DEBUG DEBUG_OPS
27 #define FREE_CLUSTER_CHAIN(sb,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; \
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
,
47 UBYTE
*name
= *pname
, *base
, ch
, *p
;
48 ULONG namelen
= *pnamelen
, baselen
;
51 /* Skip device name (if any) */
52 for (ch
= *(p
= name
); ch
!= ':' && ch
!= '\0'; ch
= *(++p
));
55 namelen
-= (p
- name
) + 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 */
66 if (base
[baselen
- 1] != '/')
72 if (base
[baselen
- 1] == '/')
77 name
= &base
[baselen
];
80 bug("[fat] base is '");
81 RawPutChars(base
, baselen
); bug("', name is '");
82 RawPutChars(name
, namelen
);
88 if ((err
= GetDirEntryByPath(dh
, base
, baselen
, &de
)) != 0)
90 D(bug("[fat] base not found\n"));
94 if ((err
= InitDirHandle(dh
->ioh
.sb
, FIRST_FILE_CLUSTER(&de
), dh
,
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 */
110 return LockFileByName(dirlock
, name
, namelen
, access
, filelock
,
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 */
119 return LockRoot(access
, filelock
, glob
);
122 void OpUnlockFile(struct ExtFileLock
*lock
, struct Globals
*glob
)
125 FreeLock(lock
, glob
);
128 LONG
OpCopyLock(struct ExtFileLock
*lock
, struct ExtFileLock
**copy
,
129 struct Globals
*glob
)
132 return CopyLock(lock
, copy
, glob
);
134 return LockRoot(SHARED_LOCK
, copy
, glob
);
137 LONG
OpLockParent(struct ExtFileLock
*lock
, struct ExtFileLock
**parent
,
138 struct Globals
*glob
)
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
)
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
);
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
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
;
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
)
190 LockFile(parent_cluster
, dh
.cur_index
, SHARED_LOCK
, parent
,
196 ReleaseDirHandle(&dh
);
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
)
209 struct ExtFileLock
*lock
;
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 */
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) */
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
);
252 err
= LockFileByName(dirlock
, name
, namelen
,
253 action
== ACTION_FINDINPUT
? SHARED_LOCK
: EXCLUSIVE_LOCK
, &lock
,
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"));
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
;
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
);
303 D(bug("[fat] file truncated, returning the lock\n"));
305 /* file is empty, go */
311 /* any error other than "not found" should be taken as-is */
312 if (err
!= ERROR_OBJECT_NOT_FOUND
)
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
);
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)
333 /* get down to the correct subdir */
334 if ((err
= MoveToSubdir(&dh
, &name
, &namelen
)) != 0)
336 ReleaseDirHandle(&dh
);
340 /* if the dir is write protected, can't do anything. root dir is never
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 */
355 CreateDirEntry(&dh
, name
, namelen
, ATTR_ARCHIVE
, 0, &de
)) != 0)
357 ReleaseDirHandle(&dh
);
361 /* lock the new file */
362 err
= LockFile(de
.cluster
, de
.index
, EXCLUSIVE_LOCK
, filelock
, glob
);
365 ReleaseDirHandle(&dh
);
369 (*filelock
)->do_notify
= TRUE
;
370 D(bug("[fat] returning lock on new file\n"));
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
)
382 struct ExtFileLock
*lock
;
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);
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
,
400 D(bug("[fat] couldn't obtain exclusive lock on named file\n"));
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
,
419 FreeLock(lock
, glob
);
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 */
428 while ((err
= GetDirEntry(&dh
, de
.index
+ 1, &de
)) == 0)
430 /* skip unused entries */
431 if (de
.e
.entry
.name
[0] == 0xe5)
434 /* end of directory, it's empty */
435 if (de
.e
.entry
.name
[0] == 0x00)
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
,
453 FreeLock(lock
, glob
);
457 /* if the dir is write protected, can't do anything. root dir is never
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
);
478 ReleaseDirHandle(&dh
);
480 /* now free the clusters the file was using */
481 FREE_CLUSTER_CHAIN(lock
->ioh
.sb
, lock
->ioh
.first_cluster
);
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
);
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
;
508 /* get the source dir handle */
509 if ((err
= InitDirHandle(glob
->sb
,
510 sdirlock
!= NULL
? sdirlock
->ioh
.first_cluster
: 0, &sdh
,
514 /* get down to the correct subdir */
515 if ((err
= MoveToSubdir(&sdh
, &sname
, &snamelen
)) != 0)
517 ReleaseDirHandle(&sdh
);
522 if ((err
= GetDirEntryByName(&sdh
, sname
, snamelen
, &sde
)) != 0)
524 ReleaseDirHandle(&sdh
);
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
,
533 ReleaseDirHandle(&sdh
);
537 /* get down to the correct subdir */
538 if ((err
= MoveToSubdir(&ddh
, &dname
, &dnamelen
)) != 0)
540 ReleaseDirHandle(&ddh
);
541 ReleaseDirHandle(&sdh
);
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
);
577 /* at this point we have the source entry in sde, and we know the dest
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
,
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
);
628 SendNotifyByDirEntry(sdh
.ioh
.sb
, &dde
);
630 ReleaseDirHandle(&ddh
);
631 ReleaseDirHandle(&sdh
);
636 LONG
OpCreateDir(struct ExtFileLock
*dirlock
, UBYTE
*name
, ULONG namelen
,
637 struct ExtFileLock
**newdirlock
, struct Globals
*glob
)
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)
656 /* get down to the correct subdir */
657 if ((err
= MoveToSubdir(&dh
, &name
, &namelen
)) != 0)
659 ReleaseDirHandle(&dh
);
663 /* Make sure 'name' is just the FilePart() */
664 for (i
= namelen
- 1; i
> 0; i
--)
666 if (name
[i
] == '/' || name
[i
] == ':')
674 /* if the dir is write protected, can't do anything. root dir is never
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
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
);
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
);
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
)
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
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
);
756 ReleaseDirHandle(&dh
);
759 SendNotifyByLock((*newdirlock
)->ioh
.sb
, (*newdirlock
)->gl
);
764 LONG
OpRead(struct ExtFileLock
*lock
, UBYTE
*data
, ULONG want
,
765 ULONG
*read
, struct Globals
*glob
)
769 D(bug("[fat] request to read %ld bytes from file pos %ld\n", want
,
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)
785 D(bug("[fat] read %ld bytes, new file pos is %ld\n", *read
,
792 LONG
OpWrite(struct ExtFileLock
*lock
, UBYTE
*data
, ULONG want
,
793 ULONG
*written
, struct Globals
*glob
)
796 BOOL update_entry
= FALSE
;
800 D(bug("[fat] request to write %ld bytes to file pos %ld\n", want
,
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
;
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)
828 if ((err
= WriteFileChunk(&(lock
->ioh
), lock
->pos
, want
, data
,
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 */
835 D(bug("[fat] nothing successfully written (!),"
836 " nothing else to do\n"));
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
;
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
))
859 D(bug("[fat] wrote %ld bytes, new file pos is %ld, size is %ld\n",
860 *written
, lock
->pos
, lock
->gl
->size
));
864 D(bug("[fat] updating dir entry, first cluster is %ld,"
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
;
880 ReleaseDirHandle(&dh
);
887 LONG
OpSetFileSize(struct ExtFileLock
*lock
, LONG offset
, LONG whence
,
888 LONG
*newsize
, struct Globals
*glob
)
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)
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
;
920 return ERROR_SEEK_ERROR
;
922 if (lock
->gl
->size
== size
)
924 D(bug("[fat] new size matches old size, nothing to do\n"));
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
,
938 if ((err
= GetDirEntry(&dh
, lock
->gl
->dir_entry
, &de
)) != 0)
940 ReleaseDirHandle(&dh
);
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
);
961 D(bug("[fat] file is empty\n"));
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 */
988 /* do the actual count */
989 while ((last
= GET_NEXT_CLUSTER(glob
->sb
, cl
))
990 < glob
->sb
->eoc_mark
- 7)
995 /* if we get as many clusters as we want, kill everything after
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"));
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 */
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
);
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 */
1036 /* otherwise, hook it up to the current one */
1038 SET_NEXT_CLUSTER(glob
->sb
, 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
,
1062 LONG
OpSetProtect(struct ExtFileLock
*dirlock
, UBYTE
*name
, ULONG namelen
,
1063 ULONG prot
, struct Globals
*glob
)
1066 struct DirHandle dh
;
1069 /* get the dir handle */
1070 if ((err
= InitDirHandle(glob
->sb
,
1071 dirlock
!= NULL
? dirlock
->ioh
.first_cluster
: 0, &dh
, FALSE
)) != 0)
1074 /* get down to the correct subdir */
1075 if ((err
= MoveToSubdir(&dh
, &name
, &namelen
)) != 0)
1077 ReleaseDirHandle(&dh
);
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
;
1090 if ((err
= GetDirEntryByName(&dh
, name
, namelen
, &de
)) != 0)
1092 ReleaseDirHandle(&dh
);
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
);
1128 LONG
OpSetDate(struct ExtFileLock
*dirlock
, UBYTE
*name
, ULONG namelen
,
1129 struct DateStamp
*ds
, struct Globals
*glob
)
1132 struct DirHandle dh
;
1135 /* get the dir handle */
1136 if ((err
= InitDirHandle(glob
->sb
,
1137 dirlock
!= NULL
? dirlock
->ioh
.first_cluster
: 0, &dh
, FALSE
)) != 0)
1140 /* get down to the correct subdir */
1141 if ((err
= MoveToSubdir(&dh
, &name
, &namelen
)) != 0)
1143 ReleaseDirHandle(&dh
);
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
;
1156 if ((err
= GetDirEntryByName(&dh
, name
, namelen
, &de
)) != 0)
1158 ReleaseDirHandle(&dh
);
1162 /* set and update the date */
1163 ConvertDOSDate(ds
, &de
.e
.entry
.write_date
, &de
.e
.entry
.write_time
,
1165 de
.e
.entry
.last_access_date
= de
.e
.entry
.write_date
;
1166 UpdateDirEntry(&de
);
1168 SendNotifyByDirEntry(glob
->sb
, &de
);
1170 ReleaseDirHandle(&dh
);
1175 LONG
OpAddNotify(struct NotifyRequest
*nr
, struct Globals
*glob
)
1178 struct DirHandle dh
;
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
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
;
1196 if ((err
= InitDirHandle(glob
->sb
, 0, &dh
, FALSE
)) != 0)
1199 /* look for the entry */
1201 GetDirEntryByPath(&dh
, nr
->nr_FullName
, strlen(nr
->nr_FullName
),
1203 if (err
!= 0 && err
!= ERROR_OBJECT_NOT_FOUND
)
1206 /* if it was found, then it might be open. try to find the global
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
)
1222 D(bug("[fat] found global lock 0x%0x\n", gl
));
1233 D(bug("[fat] file doesn't exist\n"));
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 */
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
));
1261 LONG
OpRemoveNotify(struct NotifyRequest
*nr
, struct Globals
*glob
)
1264 struct NotifyNode
*nn
, *nn2
;
1266 D(bug("[fat] trying to remove notification for '%s'\n",
1269 /* search inserted volume for the request */
1270 if (glob
->sb
!= NULL
)
1272 ForeachNodeSafe(&glob
->sb
->info
->notifies
, nn
, nn2
)
1276 D(bug("[fat] found notify request in list, removing it\n"));
1278 FreeVecPooled(glob
->sb
->info
->mem_pool
, nn
);
1284 /* search offline volumes for the request */
1285 ForeachNode(&glob
->sblist
, sb
)
1287 ForeachNodeSafe(&sb
->info
->notifies
, nn
, nn2
)
1291 D(bug("[fat] found notify request in list, removing it\n"));
1293 FreeVecPooled(sb
->info
->mem_pool
, nn
);
1294 AttemptDestroyVolume(sb
);
1300 D(bug("[fat] not found, doing nothing\n"));