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.
25 /////////////////////////////////////////////////////////////////////////////////////////////
27 /////////////////////////////////////////////////////////////////////////////////////////////
29 ULONG FsType
= 0; // Type of filesystem on boot device, set by FsOpenVolume()
31 /////////////////////////////////////////////////////////////////////////////////////////////
33 /////////////////////////////////////////////////////////////////////////////////////////////
35 VOID
FileSystemError(PCSTR ErrorString
)
37 DbgPrint((DPRINT_FILESYSTEM
, "%s\n", ErrorString
));
39 UiMessageBox(ErrorString
);
44 * BOOLEAN FsOpenVolume(ULONG DriveNumber, ULONGLONG StartSector, ULONGLONG SectorCount, int Type);
46 * This function is called to open a disk volume for file access.
47 * It must be called before any of the file functions will work.
50 static BOOLEAN
FsOpenVolume(ULONG DriveNumber
, ULONGLONG StartSector
, ULONGLONG SectorCount
, int Type
)
59 return FatOpenVolume(DriveNumber
, StartSector
, SectorCount
);
61 return Ext2OpenVolume(DriveNumber
, StartSector
);
63 return NtfsOpenVolume(DriveNumber
, StartSector
);
65 return IsoOpenVolume(DriveNumber
);
68 sprintf(ErrorText
, "Unsupported file system. Type: 0x%x", Type
);
69 FileSystemError(ErrorText
);
76 * BOOLEAN FsOpenBootVolume()
78 * This function is called to open the boot disk volume for file access.
79 * It must be called before any of the file functions will work.
81 BOOLEAN
FsOpenBootVolume()
84 ULONGLONG StartSector
;
85 ULONGLONG SectorCount
;
88 if (! MachDiskGetBootVolume(&DriveNumber
, &StartSector
, &SectorCount
, &Type
))
90 FileSystemError("Unable to locate boot partition\n");
94 return FsOpenVolume(DriveNumber
, StartSector
, SectorCount
, Type
);
97 BOOLEAN
FsOpenSystemVolume(char *SystemPath
, char *RemainingPath
, PULONG Device
)
100 ULONGLONG StartSector
;
101 ULONGLONG SectorCount
;
104 if (! MachDiskGetSystemVolume(SystemPath
, RemainingPath
, Device
,
105 &DriveNumber
, &StartSector
, &SectorCount
,
108 FileSystemError("Unable to locate system partition\n");
112 return FsOpenVolume(DriveNumber
, StartSector
, SectorCount
, Type
);
116 PFILE
FsOpenFile(PCSTR FileName
)
118 PFILE FileHandle
= NULL
;
121 // Print status message
123 DbgPrint((DPRINT_FILESYSTEM
, "Opening file '%s'...\n", FileName
));
126 // Check and see if the first character is '\' or '/' and remove it if so
128 while ((*FileName
== '\\') || (*FileName
== '/'))
134 // Check file system type and pass off to appropriate handler
139 FileHandle
= FatOpenFile(FileName
);
142 FileHandle
= IsoOpenFile(FileName
);
145 FileHandle
= Ext2OpenFile(FileName
);
148 FileHandle
= NtfsOpenFile(FileName
);
151 FileSystemError("Error: Unknown filesystem.");
157 // Check return value
159 if (FileHandle
!= NULL
)
161 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() succeeded. FileHandle: 0x%x\n", FileHandle
));
165 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() failed.\n"));
167 #endif // defined DEBUG
172 VOID
FsCloseFile(PFILE FileHandle
)
181 NtfsCloseFile(FileHandle
);
184 FileSystemError("Error: Unknown filesystem.");
191 * returns number of bytes read or EOF
193 BOOLEAN
FsReadFile(PFILE FileHandle
, ULONG BytesToRead
, ULONG
* BytesRead
, PVOID Buffer
)
195 ULONGLONG BytesReadBig
;
199 // Set the number of bytes read equal to zero
201 if (BytesRead
!= NULL
)
210 return FatReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
214 return IsoReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
218 //return Ext2ReadFile(FileHandle, BytesToRead, BytesRead, Buffer);
219 Success
= Ext2ReadFile(FileHandle
, BytesToRead
, &BytesReadBig
, Buffer
);
220 *BytesRead
= (ULONG
)BytesReadBig
;
225 return NtfsReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
229 FileSystemError("Unknown file system.");
236 ULONG
FsGetFileSize(PFILE FileHandle
)
242 return FatGetFileSize(FileHandle
);
246 return IsoGetFileSize(FileHandle
);
250 return Ext2GetFileSize(FileHandle
);
254 return NtfsGetFileSize(FileHandle
);
257 FileSystemError("Unknown file system.");
264 VOID
FsSetFilePointer(PFILE FileHandle
, ULONG NewFilePointer
)
270 FatSetFilePointer(FileHandle
, NewFilePointer
);
275 IsoSetFilePointer(FileHandle
, NewFilePointer
);
280 Ext2SetFilePointer(FileHandle
, NewFilePointer
);
285 NtfsSetFilePointer(FileHandle
, NewFilePointer
);
289 FileSystemError("Unknown file system.");
294 ULONG
FsGetFilePointer(PFILE FileHandle
)
300 return FatGetFilePointer(FileHandle
);
305 return IsoGetFilePointer(FileHandle
);
310 return Ext2GetFilePointer(FileHandle
);
315 return NtfsGetFilePointer(FileHandle
);
319 FileSystemError("Unknown file system.");
326 BOOLEAN
FsIsEndOfFile(PFILE FileHandle
)
328 if (FsGetFilePointer(FileHandle
) >= FsGetFileSize(FileHandle
))
339 * FsGetNumPathParts()
340 * This function parses a path in the form of dir1\dir2\file1.ext
341 * and returns the number of parts it has (i.e. 3 - dir1,dir2,file1.ext)
343 ULONG
FsGetNumPathParts(PCSTR Path
)
348 for (i
=0,num
=0; i
<strlen(Path
); i
++)
350 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
357 DbgPrint((DPRINT_FILESYSTEM
, "FatGetNumPathParts() Path = %s NumPathParts = %d\n", Path
, num
));
363 * FsGetFirstNameFromPath()
364 * This function parses a path in the form of dir1\dir2\file1.ext
365 * and puts the first name of the path (e.g. "dir1") in buffer
366 * compatible with the MSDOS directory structure
368 VOID
FsGetFirstNameFromPath(PCHAR Buffer
, PCSTR Path
)
372 // Copy all the characters up to the end of the
373 // string or until we hit a '\' character
374 // and put them in Buffer
375 for (i
=0; i
<strlen(Path
); i
++)
377 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
389 DbgPrint((DPRINT_FILESYSTEM
, "FatGetFirstNameFromPath() Path = %s FirstName = %s\n", Path
, Buffer
));