Skip to content

Commit 5d361b6

Browse files
committed
[FREELDR] fs.c: Fix handling of file handles, (de)referencing, and direct-device access (reactos#7414)
- The original code was just incrementing the reference counts (RefCounts) of the device objects or the device/file handles, without decrementing them when closing the handles. This is now fixed. Notice the following: * When opening a file on a device (disk), the device's (and its handle's) RefCount is incremented, and the file handle's RefCount is incremented as well. * When closing a file, the file handle's RefCount is decremented (and the file closed if the RefCount reaches zero), and the file's parent device handle is also closed, recursively. This has the effect of decrementing the parent device handle's RefCount, and the device's own RefCount is decremented as well. IMPORTANT NOTE: The usefulness of handle-level RefCount is still under question, and might be (consistently) removed in the future. - Fix opening a device (disk) in direct access, when this device is already opened. Indeed, we previously allowed direct access only if the device was opened as such for the very first time (its RefCount = 0 originally); no filesystem mounting was attempted as well. Then for any later open-operations on this device (while keeping an already-opened handle to it), filesystem access was assumed. Thus, this problem would show up in two ways: * Either the device is first opened for direct access, this succeeded and no filesystem was mounted. Then, for any other open-operations, the filesystem was NOT mounted, and opening files on it would fail. Direct accesses would succeed but would create an unnecessary second file handle. * Or, the device is first opened for file-access: a filesystem was mounted and file opening would succeed. Any other file opening operation would succeed as well (if the file exists). But, a direct access open-operation would fail, because now any open-operations on the device would be assumed to be a file opening. This is now correctly fixed. If direct-open is requested, just do it. If this is a file opening, we open the device, then try to mount a filesystem on it (if not already done), then we try to open the file. If file opening fails, derereference the device. - Pass the file path to the filesystem-specific Open() functions without truncating the leading path separator, if any. This has to be handled by the filesystem routines themselves.
1 parent 7dcfda8 commit 5d361b6

File tree

7 files changed

+236
-56
lines changed

7 files changed

+236
-56
lines changed

boot/freeldr/freeldr/include/fs.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ typedef struct tagDEVVTBL
3535
#define INVALID_FILE_ID ((ULONG)-1)
3636

3737
ARC_STATUS ArcOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId);
38-
ARC_STATUS ArcClose(ULONG FileId);
38+
39+
ARC_STATUS
40+
ArcClose(
41+
_In_ ULONG FileId);
42+
3943
ARC_STATUS ArcRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count);
4044
ARC_STATUS ArcSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode);
4145
ARC_STATUS ArcGetFileInformation(ULONG FileId, FILEINFORMATION* Information);
@@ -58,7 +62,7 @@ FsRegisterDevice(
5862
_In_ const DEVVTBL* FuncTable);
5963

6064
PCWSTR FsGetServiceName(ULONG FileId);
61-
VOID FsSetDeviceSpecific(ULONG FileId, VOID* Specific);
62-
VOID* FsGetDeviceSpecific(ULONG FileId);
65+
VOID FsSetDeviceSpecific(ULONG FileId, PVOID Specific);
66+
PVOID FsGetDeviceSpecific(ULONG FileId);
6367
ULONG FsGetDeviceId(ULONG FileId);
6468
VOID FsInit(VOID);

boot/freeldr/freeldr/lib/fs/ext2.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ BOOLEAN Ext2LookupFile(PEXT2_VOLUME_INFO Volume, PCSTR FileName, PEXT2_FILE_INFO
216216

217217
RtlZeroMemory(Ext2FileInfo, sizeof(EXT2_FILE_INFO));
218218

219+
/* Skip leading path separator, if any */
220+
if (*FileName == '\\' || *FileName == '/')
221+
++FileName;
219222
//
220223
// Figure out how many sub-directories we are nested in
221224
//

boot/freeldr/freeldr/lib/fs/fat.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,9 @@ ARC_STATUS FatLookupFile(PFAT_VOLUME_INFO Volume, PCSTR FileName, PFAT_FILE_INFO
785785

786786
RtlZeroMemory(FatFileInfoPointer, sizeof(FAT_FILE_INFO));
787787

788+
/* Skip leading path separator, if any */
789+
if (*FileName == '\\' || *FileName == '/')
790+
++FileName;
788791
//
789792
// Figure out how many sub-directories we are nested in
790793
//

0 commit comments

Comments
 (0)