1 |
|
/* |
2 |
|
* extfs_beos.cpp - MacOS file system for access native file system access, BeOS specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-1999 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2001 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
87 |
|
|
88 |
|
|
89 |
|
/* |
90 |
< |
* Get/set finder type/creator for file specified by full path |
90 |
> |
* Get/set finder info for file/directory specified by full path |
91 |
|
*/ |
92 |
|
|
93 |
|
struct mime2type { |
137 |
|
{NULL, 0, 0, false} // End marker |
138 |
|
}; |
139 |
|
|
140 |
< |
void get_finder_type(const char *path, uint32 &type, uint32 &creator) |
140 |
> |
void get_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir) |
141 |
|
{ |
142 |
< |
type = 0; |
143 |
< |
creator = 0; |
142 |
> |
// Set default finder info |
143 |
> |
Mac_memset(finfo, 0, SIZEOF_FInfo); |
144 |
> |
if (fxinfo) |
145 |
> |
Mac_memset(fxinfo, 0, SIZEOF_FXInfo); |
146 |
> |
WriteMacInt16(finfo + fdFlags, DEFAULT_FINDER_FLAGS); |
147 |
> |
WriteMacInt32(finfo + fdLocation, (uint32)-1); |
148 |
|
|
149 |
|
// Open file |
150 |
|
int fd = open(path, O_RDONLY); |
151 |
|
if (fd < 0) |
152 |
|
return; |
153 |
|
|
154 |
< |
// Read BeOS MIME type and close file |
151 |
< |
char mime[256]; |
152 |
< |
ssize_t actual = fs_read_attr(fd, "BEOS:TYPE", B_MIME_STRING_TYPE, 0, mime, 256); |
153 |
< |
mime[255] = 0; |
154 |
> |
if (!is_dir) { |
155 |
|
|
156 |
< |
if (actual > 0) { |
157 |
< |
|
158 |
< |
// Translate MIME type to MacOS type/creator |
159 |
< |
char mactype[4]; |
160 |
< |
if (sscanf(mime, "application/x-MacOS-%c%c%c%c", mactype, mactype+1, mactype+2, mactype+3) == 4) { |
161 |
< |
|
162 |
< |
// MacOS style type |
163 |
< |
memcpy(&type, mactype, 4); |
164 |
< |
|
165 |
< |
} else { |
166 |
< |
|
167 |
< |
// MIME string, look in table |
168 |
< |
for (int i=0; m2t_translation[i].mime; i++) { |
169 |
< |
if (!strcmp(mime, m2t_translation[i].mime)) { |
170 |
< |
type = m2t_translation[i].type; |
171 |
< |
creator = m2t_translation[i].creator; |
172 |
< |
break; |
156 |
> |
// Read BeOS MIME type |
157 |
> |
ssize_t actual = fs_read_attr(fd, "BEOS:TYPE", B_MIME_STRING_TYPE, 0, tmp_buf, 256); |
158 |
> |
tmp_buf[255] = 0; |
159 |
> |
|
160 |
> |
if (actual > 0) { |
161 |
> |
|
162 |
> |
// Translate MIME type to MacOS type/creator |
163 |
> |
uint8 mactype[4]; |
164 |
> |
if (sscanf((char *)tmp_buf, "application/x-MacOS-%c%c%c%c", mactype, mactype+1, mactype+2, mactype+3) == 4) { |
165 |
> |
|
166 |
> |
// MacOS style type |
167 |
> |
WriteMacInt32(finfo + fdType, (mactype[0] << 24) | (mactype[1] << 16) | (mactype[2] << 8) | mactype[3]); |
168 |
> |
|
169 |
> |
} else { |
170 |
> |
|
171 |
> |
// MIME string, look in table |
172 |
> |
for (int i=0; m2t_translation[i].mime; i++) { |
173 |
> |
if (!strcmp((char *)tmp_buf, m2t_translation[i].mime)) { |
174 |
> |
WriteMacInt32(finfo + fdType, m2t_translation[i].type); |
175 |
> |
WriteMacInt32(finfo + fdCreator, m2t_translation[i].creator); |
176 |
> |
break; |
177 |
> |
} |
178 |
|
} |
179 |
|
} |
180 |
|
} |
181 |
+ |
|
182 |
+ |
// Override file type with MACOS:CREATOR attribute |
183 |
+ |
if (fs_read_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, tmp_buf, 4) == 4) |
184 |
+ |
WriteMacInt32(finfo + fdCreator, (tmp_buf[0] << 24) | (tmp_buf[1] << 16) | (tmp_buf[2] << 8) | tmp_buf[3]); |
185 |
|
} |
186 |
|
|
187 |
< |
// Override file type with MACOS:CREATOR attribute |
188 |
< |
fs_read_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, &creator, 4); |
187 |
> |
// Read MACOS:HFS_FLAGS attribute |
188 |
> |
if (fs_read_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, tmp_buf, 2) == 2) |
189 |
> |
WriteMacInt16(finfo + fdFlags, (tmp_buf[0] << 8) | tmp_buf[1]); |
190 |
|
|
191 |
|
// Close file |
192 |
|
close(fd); |
193 |
|
} |
194 |
|
|
195 |
< |
void set_finder_type(const char *path, uint32 type, uint32 creator) |
195 |
> |
void set_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir) |
196 |
|
{ |
197 |
|
// Open file |
198 |
|
int fd = open(path, O_WRONLY); |
199 |
|
if (fd < 0) |
200 |
|
return; |
201 |
|
|
202 |
< |
// Set BEOS:TYPE attribute |
203 |
< |
if (type) { |
204 |
< |
bool written = false; |
205 |
< |
for (int i=0; m2t_translation[i].mime; i++) { |
206 |
< |
if (m2t_translation[i].type == type && m2t_translation[i].reversible) { |
207 |
< |
fs_write_attr(fd, "BEOS:TYPE", B_MIME_STRING_TYPE, 0, m2t_translation[i].mime, strlen(m2t_translation[i].mime) + 1); |
208 |
< |
written = true; |
209 |
< |
break; |
202 |
> |
if (!is_dir) { |
203 |
> |
|
204 |
> |
// Set BEOS:TYPE attribute |
205 |
> |
uint32 type = ReadMacInt32(finfo + fdType); |
206 |
> |
if (type) { |
207 |
> |
for (int i=0; m2t_translation[i].mime; i++) { |
208 |
> |
if (m2t_translation[i].type == type && m2t_translation[i].reversible) { |
209 |
> |
fs_write_attr(fd, "BEOS:TYPE", B_MIME_STRING_TYPE, 0, m2t_translation[i].mime, strlen(m2t_translation[i].mime) + 1); |
210 |
> |
break; |
211 |
> |
} |
212 |
|
} |
213 |
|
} |
214 |
< |
if (!written) { |
215 |
< |
char mime[256]; |
216 |
< |
sprintf(mime, "application/x-MacOS-%c%c%c%c", type >> 24, type >> 16, type >> 8, type); |
217 |
< |
fs_write_attr(fd, "BEOS:TYPE", B_MIME_STRING_TYPE, 0, mime, strlen(mime) + 1); |
214 |
> |
|
215 |
> |
// Set MACOS:CREATOR attribute |
216 |
> |
uint32 creator = ReadMacInt32(finfo + fdCreator); |
217 |
> |
if (creator) { |
218 |
> |
tmp_buf[0] = creator >> 24; |
219 |
> |
tmp_buf[1] = creator >> 16; |
220 |
> |
tmp_buf[2] = creator >> 8; |
221 |
> |
tmp_buf[3] = creator; |
222 |
> |
fs_write_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, tmp_buf, 4); |
223 |
|
} |
224 |
|
} |
225 |
|
|
208 |
– |
// Set MACOS:CREATOR attribute |
209 |
– |
if (creator) |
210 |
– |
fs_write_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, &creator, 4); |
211 |
– |
|
212 |
– |
// Close file |
213 |
– |
close(fd); |
214 |
– |
} |
215 |
– |
|
216 |
– |
|
217 |
– |
/* |
218 |
– |
* Get/set finder flags for file/dir specified by full path (MACOS:HFS_FLAGS attribute) |
219 |
– |
*/ |
220 |
– |
|
221 |
– |
void get_finder_flags(const char *path, uint16 &flags) |
222 |
– |
{ |
223 |
– |
flags = DEFAULT_FINDER_FLAGS; // Default |
224 |
– |
|
225 |
– |
// Open file |
226 |
– |
int fd = open(path, O_RDONLY); |
227 |
– |
if (fd < 0) |
228 |
– |
return; |
229 |
– |
|
230 |
– |
// Read MACOS:HFS_FLAGS attribute |
231 |
– |
fs_read_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, &flags, 2); |
232 |
– |
|
233 |
– |
// Close file |
234 |
– |
close(fd); |
235 |
– |
} |
236 |
– |
|
237 |
– |
void set_finder_flags(const char *path, uint16 flags) |
238 |
– |
{ |
239 |
– |
// Open file |
240 |
– |
int fd = open(path, O_WRONLY); |
241 |
– |
if (fd < 0) |
242 |
– |
return; |
243 |
– |
|
226 |
|
// Write MACOS:HFS_FLAGS attribute |
227 |
< |
if (flags != DEFAULT_FINDER_FLAGS) |
228 |
< |
fs_write_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, &flags, 2); |
229 |
< |
else |
227 |
> |
uint16 flags = ReadMacInt16(finfo + fdFlags); |
228 |
> |
if (flags != DEFAULT_FINDER_FLAGS) { |
229 |
> |
tmp_buf[0] = flags >> 8; |
230 |
> |
tmp_buf[1] = flags; |
231 |
> |
fs_write_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, tmp_buf, 2); |
232 |
> |
} else |
233 |
|
fs_remove_attr(fd, "MACOS:HFS_FLAGS"); |
234 |
|
|
235 |
|
// Close file |
268 |
|
// Open temporary file for resource fork |
269 |
|
char rname[L_tmpnam]; |
270 |
|
tmpnam(rname); |
271 |
< |
int rfd = open(rname, O_RDWR | O_CREAT | O_TRUNC, 0664); |
271 |
> |
int rfd = open(rname, O_RDWR | O_CREAT | O_TRUNC, 0666); |
272 |
|
if (rfd < 0) { |
273 |
|
close(fd); |
274 |
|
return -1; |
354 |
|
|
355 |
|
/* |
356 |
|
* Read "length" bytes from file to "buffer", |
357 |
< |
* returns number of bytes read (or 0) |
357 |
> |
* returns number of bytes read (or -1 on error) |
358 |
|
*/ |
359 |
|
|
360 |
|
static inline ssize_t sread(int fd, void *buf, size_t count) |
364 |
|
return res; |
365 |
|
} |
366 |
|
|
367 |
< |
size_t extfs_read(int fd, void *buffer, size_t length) |
367 |
> |
ssize_t extfs_read(int fd, void *buffer, size_t length) |
368 |
|
{ |
384 |
– |
errno = 0; |
385 |
– |
|
369 |
|
// Buffer in kernel space? |
387 |
– |
size_t actual = 0; |
370 |
|
if ((uint32)buffer < 0x80000000) { |
371 |
|
|
372 |
|
// Yes, transfer via buffer |
373 |
+ |
ssize_t actual = 0; |
374 |
|
while (length) { |
375 |
|
size_t transfer_size = (length > TMP_BUF_SIZE) ? TMP_BUF_SIZE : length; |
376 |
|
ssize_t res = sread(fd, tmp_buf, transfer_size); |
377 |
< |
if (res >= 0) { |
378 |
< |
memcpy(buffer, tmp_buf, res); |
379 |
< |
buffer = (void *)((uint8 *)buffer + res); |
380 |
< |
length -= res; |
381 |
< |
actual += res; |
382 |
< |
} |
377 |
> |
if (res < 0) |
378 |
> |
return res; |
379 |
> |
memcpy(buffer, tmp_buf, res); |
380 |
> |
buffer = (void *)((uint8 *)buffer + res); |
381 |
> |
length -= res; |
382 |
> |
actual += res; |
383 |
|
if (res != transfer_size) |
384 |
|
return actual; |
385 |
|
} |
386 |
+ |
return actual; |
387 |
|
|
388 |
|
} else { |
389 |
|
|
390 |
|
// No, transfer directly |
391 |
< |
actual = sread(fd, buffer, length); |
408 |
< |
if (actual < 0) |
409 |
< |
actual = 0; |
391 |
> |
return sread(fd, buffer, length); |
392 |
|
} |
411 |
– |
return actual; |
393 |
|
} |
394 |
|
|
395 |
|
|
396 |
|
/* |
397 |
|
* Write "length" bytes from "buffer" to file, |
398 |
< |
* returns number of bytes written (or 0) |
398 |
> |
* returns number of bytes written (or -1 on error) |
399 |
|
*/ |
400 |
|
|
401 |
|
static inline ssize_t swrite(int fd, void *buf, size_t count) |
405 |
|
return res; |
406 |
|
} |
407 |
|
|
408 |
< |
size_t extfs_write(int fd, void *buffer, size_t length) |
408 |
> |
ssize_t extfs_write(int fd, void *buffer, size_t length) |
409 |
|
{ |
429 |
– |
errno = 0; |
430 |
– |
|
410 |
|
// Buffer in kernel space? |
432 |
– |
size_t actual = 0; |
411 |
|
if ((uint32)buffer < 0x80000000) { |
412 |
|
|
413 |
|
// Yes, transfer via buffer |
414 |
+ |
ssize_t actual = 0; |
415 |
|
while (length) { |
416 |
|
size_t transfer_size = (length > TMP_BUF_SIZE) ? TMP_BUF_SIZE : length; |
417 |
|
memcpy(tmp_buf, buffer, transfer_size); |
418 |
|
ssize_t res = swrite(fd, tmp_buf, transfer_size); |
419 |
< |
if (res >= 0) { |
420 |
< |
buffer = (void *)((uint8 *)buffer + res); |
421 |
< |
length -= res; |
422 |
< |
actual += res; |
423 |
< |
} |
419 |
> |
if (res < 0) |
420 |
> |
return res; |
421 |
> |
buffer = (void *)((uint8 *)buffer + res); |
422 |
> |
length -= res; |
423 |
> |
actual += res; |
424 |
|
if (res != transfer_size) |
425 |
|
return actual; |
426 |
|
} |
427 |
+ |
return actual; |
428 |
|
|
429 |
|
} else { |
430 |
|
|
431 |
|
// No, transfer directly |
432 |
< |
actual = swrite(fd, buffer, length); |
453 |
< |
if (actual < 0) |
454 |
< |
actual = 0; |
432 |
> |
return swrite(fd, buffer, length); |
433 |
|
} |
434 |
< |
return actual; |
434 |
> |
} |
435 |
> |
|
436 |
> |
|
437 |
> |
/* |
438 |
> |
* Remove file/directory, returns false on error (and sets errno) |
439 |
> |
*/ |
440 |
> |
|
441 |
> |
bool extfs_remove(const char *path) |
442 |
> |
{ |
443 |
> |
if (remove(path) < 0) { |
444 |
> |
if (errno == EISDIR) |
445 |
> |
return rmdir(path) == 0; |
446 |
> |
else |
447 |
> |
return false; |
448 |
> |
} |
449 |
> |
return true; |
450 |
> |
} |
451 |
> |
|
452 |
> |
|
453 |
> |
/* |
454 |
> |
* Rename/move file/directory, returns false on error (and sets errno) |
455 |
> |
*/ |
456 |
> |
|
457 |
> |
bool extfs_rename(const char *old_path, const char *new_path) |
458 |
> |
{ |
459 |
> |
return rename(old_path, new_path) == 0; |
460 |
|
} |