f2aabe257cd31eb0a83ee2401c7419cb48d3e9ca
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 /////////////////////////////////////////////////////////////////////////////////////////////
37 /////////////////////////////////////////////////////////////////////////////////////////////
39 ULONG FsType
= 0; // Type of filesystem on boot device, set by FsOpenVolume()
41 /////////////////////////////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////////////////////////////
45 VOID
FileSystemError(PCSTR ErrorString
)
47 DbgPrint((DPRINT_FILESYSTEM
, "%s\n", ErrorString
));
49 UiMessageBox(ErrorString
);
54 * BOOL FsOpenVolume(ULONG DriveNumber, ULONGLONG StartSector, ULONGLONG SectorCount, int Type);
56 * This function is called to open a disk volume for file access.
57 * It must be called before any of the file functions will work.
60 static BOOL
FsOpenVolume(ULONG DriveNumber
, ULONGLONG StartSector
, ULONGLONG SectorCount
, int Type
)
69 return FatOpenVolume(DriveNumber
, StartSector
, SectorCount
);
71 return Ext2OpenVolume(DriveNumber
, StartSector
);
73 return NtfsOpenVolume(DriveNumber
, StartSector
);
75 return IsoOpenVolume(DriveNumber
);
78 sprintf(ErrorText
, "Unsupported file system. Type: 0x%x", Type
);
79 FileSystemError(ErrorText
);
86 * BOOL FsOpenBootVolume()
88 * This function is called to open the boot disk volume for file access.
89 * It must be called before any of the file functions will work.
91 BOOL
FsOpenBootVolume()
94 ULONGLONG StartSector
;
95 ULONGLONG SectorCount
;
98 if (! MachDiskGetBootVolume(&DriveNumber
, &StartSector
, &SectorCount
, &Type
))
100 FileSystemError("Unable to locate boot partition\n");
104 return FsOpenVolume(DriveNumber
, StartSector
, SectorCount
, Type
);
107 BOOL
FsOpenSystemVolume(char *SystemPath
, char *RemainingPath
, PULONG Device
)
110 ULONGLONG StartSector
;
111 ULONGLONG SectorCount
;
114 if (! MachDiskGetSystemVolume(SystemPath
, RemainingPath
, Device
,
115 &DriveNumber
, &StartSector
, &SectorCount
,
118 FileSystemError("Unable to locate system partition\n");
122 return FsOpenVolume(DriveNumber
, StartSector
, SectorCount
, Type
);
126 PFILE
FsOpenFile(PCSTR FileName
)
128 PFILE FileHandle
= NULL
;
131 // Print status message
133 DbgPrint((DPRINT_FILESYSTEM
, "Opening file '%s'...\n", FileName
));
136 // Check and see if the first character is '\' or '/' and remove it if so
138 while ((*FileName
== '\\') || (*FileName
== '/'))
144 // Check file system type and pass off to appropriate handler
149 FileHandle
= FatOpenFile(FileName
);
152 FileHandle
= IsoOpenFile(FileName
);
155 FileHandle
= Ext2OpenFile(FileName
);
158 FileHandle
= NtfsOpenFile(FileName
);
161 FileSystemError("Error: Unknown filesystem.");
167 // Check return value
169 if (FileHandle
!= NULL
)
171 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() succeeded. FileHandle: 0x%x\n", FileHandle
));
175 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() failed.\n"));
177 #endif // defined DEBUG
182 VOID
FsCloseFile(PFILE FileHandle
)
188 * returns number of bytes read or EOF
190 BOOL
FsReadFile(PFILE FileHandle
, ULONG BytesToRead
, ULONG
* BytesRead
, PVOID Buffer
)
192 ULONGLONG BytesReadBig
;
196 // Set the number of bytes read equal to zero
198 if (BytesRead
!= NULL
)
207 return FatReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
211 return IsoReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
215 //return Ext2ReadFile(FileHandle, BytesToRead, BytesRead, Buffer);
216 Success
= Ext2ReadFile(FileHandle
, BytesToRead
, &BytesReadBig
, Buffer
);
217 *BytesRead
= (ULONG
)BytesReadBig
;
222 return NtfsReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
226 FileSystemError("Unknown file system.");
233 ULONG
FsGetFileSize(PFILE FileHandle
)
239 return FatGetFileSize(FileHandle
);
243 return IsoGetFileSize(FileHandle
);
247 return Ext2GetFileSize(FileHandle
);
251 return NtfsGetFileSize(FileHandle
);
254 FileSystemError("Unknown file system.");
261 VOID
FsSetFilePointer(PFILE FileHandle
, ULONG NewFilePointer
)
267 FatSetFilePointer(FileHandle
, NewFilePointer
);
272 IsoSetFilePointer(FileHandle
, NewFilePointer
);
277 Ext2SetFilePointer(FileHandle
, NewFilePointer
);
282 NtfsSetFilePointer(FileHandle
, NewFilePointer
);
286 FileSystemError("Unknown file system.");
291 ULONG
FsGetFilePointer(PFILE FileHandle
)
297 return FatGetFilePointer(FileHandle
);
302 return IsoGetFilePointer(FileHandle
);
307 return Ext2GetFilePointer(FileHandle
);
312 return NtfsGetFilePointer(FileHandle
);
316 FileSystemError("Unknown file system.");
323 BOOL
FsIsEndOfFile(PFILE FileHandle
)
325 if (FsGetFilePointer(FileHandle
) >= FsGetFileSize(FileHandle
))
336 * FsGetNumPathParts()
337 * This function parses a path in the form of dir1\dir2\file1.ext
338 * and returns the number of parts it has (i.e. 3 - dir1,dir2,file1.ext)
340 ULONG
FsGetNumPathParts(PCSTR Path
)
345 for (i
=0,num
=0; i
<strlen(Path
); i
++)
347 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
354 DbgPrint((DPRINT_FILESYSTEM
, "FatGetNumPathParts() Path = %s NumPathParts = %d\n", Path
, num
));
360 * FsGetFirstNameFromPath()
361 * This function parses a path in the form of dir1\dir2\file1.ext
362 * and puts the first name of the path (e.g. "dir1") in buffer
363 * compatible with the MSDOS directory structure
365 VOID
FsGetFirstNameFromPath(PCHAR Buffer
, PCSTR Path
)
369 // Copy all the characters up to the end of the
370 // string or until we hit a '\' character
371 // and put them in Buffer
372 for (i
=0; i
<strlen(Path
); i
++)
374 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
386 DbgPrint((DPRINT_FILESYSTEM
, "FatGetFirstNameFromPath() Path = %s FirstName = %s\n", Path
, Buffer
));