2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * fs.c - Generic access to the file system modules.
25 * Copyright (c) 1999-2000 Apple Computer, Inc.
32 typedef long (* FSLoadFile
)(CICell ih
, char *filePath
);
33 typedef long (* FSGetDirEntry
)(CICell ih
, char *dirPath
,
34 long *dirIndex
, char **name
,
35 long *flags
, long *time
);
46 FSGetDirEntry getDirEntry
;
49 typedef struct PartInfo PartInfo
, *PartInfoPtr
;
51 #define kNumPartInfos (16)
52 static PartInfo gParts
[kNumPartInfos
];
54 // Private function prototypes
55 long LookupPartition(char *devSpec
);
60 long LoadFile(char *fileSpec
)
65 long ret
, length
, partIndex
;
67 ret
= ConvertFileSpec(fileSpec
, devSpec
, &filePath
);
68 if ((ret
== -1) || (filePath
== NULL
)) return -1;
70 // Get the partition index for devSpec.
71 partIndex
= LookupPartition(devSpec
);
72 if (partIndex
== -1) return -1;
74 loadFile
= gParts
[partIndex
].loadFile
;
75 length
= loadFile(gParts
[partIndex
].partIH
, filePath
);
77 // if (length == 0) return -1;
82 long CopyFile(char *fileSpec
, char **addr
, long *length
)
87 len
= LoadFile(fileSpec
);
88 if (len
== -1) return -1;
90 file
= malloc(len
+ 2);
91 if (file
== NULL
) return -1;
93 strncpy(file
, (char *)kLoadAddr
, len
);
105 long GetFileInfo(char *dirSpec
, char *name
, long *flags
, long *time
)
111 ret
= GetDirEntry(dirSpec
, &index
, &curName
, flags
, time
);
112 if (ret
== -1) break;
114 if (!strcmp(name
, curName
)) break;
120 long GetDirEntry(char *dirSpec
, long *dirIndex
, char **name
,
121 long *flags
, long *time
)
125 FSGetDirEntry getDirEntry
;
128 ret
= ConvertFileSpec(dirSpec
, devSpec
, &dirPath
);
129 if ((ret
== -1) || (dirPath
== NULL
)) return -1;
131 // Get the partition index for devSpec.
132 partIndex
= LookupPartition(devSpec
);
133 if (partIndex
== -1) return -1;
135 getDirEntry
= gParts
[partIndex
].getDirEntry
;
136 ret
= getDirEntry(gParts
[partIndex
].partIH
, dirPath
,
137 dirIndex
, name
, flags
, time
);
142 long DumpDir(char *dirSpec
)
144 long ret
, flags
, time
, index
= 0;
147 printf("DumpDir on [%s]\n", dirSpec
);
150 ret
= GetDirEntry(dirSpec
, &index
, &name
, &flags
, &time
);
151 if (ret
== -1) break;
153 printf("%x %x [%s]\n", flags
, time
, name
);
162 long LookupPartition(char *devSpec
)
165 long partIndex
, partType
;
168 // See if the devSpec has already been opened.
169 for (partIndex
= 0; partIndex
< kNumPartInfos
; partIndex
++) {
170 if (!strcmp(gParts
[partIndex
].partName
, devSpec
)) break;
173 // If it has not been opened, do so now.
174 if (partIndex
== kNumPartInfos
) {
176 for (partIndex
= 0; partIndex
< kNumPartInfos
; partIndex
++) {
177 if (gParts
[partIndex
].partIH
== 0) break;
179 // No free slots, so return error.
180 if (partIndex
== kNumPartInfos
) return -1;
182 deviceType
= GetDeviceType(devSpec
);
183 switch (deviceType
) {
184 case kNetworkDeviceType
:
185 partIH
= NetInitPartition(devSpec
);
186 if (partIH
== 0) return -1;
190 case kBlockDeviceType
:
191 printf("Opening partition [%s]...\n", devSpec
);
192 partIH
= Open(devSpec
);
194 printf("Failed to open partition [%s].\n", devSpec
);
198 // Find out what kind of partition it is.
199 if (HFSInitPartition(partIH
) != -1) partType
= kPartHFS
;
200 else if (UFSInitPartition(partIH
) != -1) partType
= kPartUFS
;
201 else if (Ext2InitPartition(partIH
) != -1) partType
= kPartExt2
;
209 gParts
[partIndex
].partIH
= partIH
;
210 gParts
[partIndex
].partType
= partType
;
211 strcpy(gParts
[partIndex
].partName
, devSpec
);
215 gParts
[partIndex
].loadFile
= NetLoadFile
;
216 gParts
[partIndex
].getDirEntry
= NetGetDirEntry
;
220 gParts
[partIndex
].loadFile
= HFSLoadFile
;
221 gParts
[partIndex
].getDirEntry
= HFSGetDirEntry
;
225 gParts
[partIndex
].loadFile
= UFSLoadFile
;
226 gParts
[partIndex
].getDirEntry
= UFSGetDirEntry
;
230 gParts
[partIndex
].loadFile
= Ext2LoadFile
;
231 gParts
[partIndex
].getDirEntry
= Ext2GetDirEntry
;