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 |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
*/ |
20 |
|
|
21 |
+ |
#include "sysdeps.h" |
22 |
+ |
|
23 |
|
#include <sys/types.h> |
24 |
|
#include <sys/stat.h> |
25 |
+ |
#include <string.h> |
26 |
|
#include <stdio.h> |
27 |
|
#include <stdlib.h> |
28 |
|
#include <unistd.h> |
29 |
+ |
#include <fcntl.h> |
30 |
|
#include <dirent.h> |
31 |
|
#include <errno.h> |
32 |
|
|
33 |
|
#include <fs_attr.h> |
34 |
+ |
#include <support/TypeConstants.h> |
35 |
+ |
#include <storage/Mime.h> |
36 |
|
|
31 |
– |
#include "sysdeps.h" |
37 |
|
#include "extfs.h" |
38 |
|
#include "extfs_defs.h" |
39 |
|
|
72 |
|
|
73 |
|
|
74 |
|
/* |
75 |
< |
* Get/set finder type/creator for file specified by full path |
75 |
> |
* Add component to path name |
76 |
> |
*/ |
77 |
> |
|
78 |
> |
void add_path_component(char *path, const char *component) |
79 |
> |
{ |
80 |
> |
int l = strlen(path); |
81 |
> |
if (l < MAX_PATH_LENGTH-1 && path[l-1] != '/') { |
82 |
> |
path[l] = '/'; |
83 |
> |
path[l+1] = 0; |
84 |
> |
} |
85 |
> |
strncat(path, component, MAX_PATH_LENGTH-1); |
86 |
> |
} |
87 |
> |
|
88 |
> |
|
89 |
> |
/* |
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) |
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 |
154 |
> |
// Read BeOS MIME type |
155 |
|
char mime[256]; |
156 |
|
ssize_t actual = fs_read_attr(fd, "BEOS:TYPE", B_MIME_STRING_TYPE, 0, mime, 256); |
157 |
|
mime[255] = 0; |
163 |
|
if (sscanf(mime, "application/x-MacOS-%c%c%c%c", mactype, mactype+1, mactype+2, mactype+3) == 4) { |
164 |
|
|
165 |
|
// MacOS style type |
166 |
< |
memcpy(&type, mactype, 4); |
166 |
> |
WriteMacInt32(finfo + fdType, mactype); |
167 |
|
|
168 |
|
} else { |
169 |
|
|
170 |
|
// MIME string, look in table |
171 |
|
for (int i=0; m2t_translation[i].mime; i++) { |
172 |
|
if (!strcmp(mime, m2t_translation[i].mime)) { |
173 |
< |
type = m2t_translation[i].type; |
174 |
< |
creator = m2t_translation[i].creator; |
173 |
> |
WriteMacInt32(finfo + fdType, m2t_translation[i].type); |
174 |
> |
WriteMacInt32(finfo + fdCreator, m2t_translation[i].creator); |
175 |
|
break; |
176 |
|
} |
177 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
// Override file type with MACOS:CREATOR attribute |
182 |
< |
fs_read_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, &creator, 4); |
182 |
> |
if (fs_read_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, &mime, 4) == 4) |
183 |
> |
WriteMacInt32(finfo + fdCreator, (mime[0] << 24) | (mime[1] << 16) | (mime[2] << 8) | mime[3]); |
184 |
> |
|
185 |
> |
// Read MACOS:HFS_FLAGS attribute |
186 |
> |
if (fs_read_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, &mime, 2) == 2) |
187 |
> |
WriteMacInt16(finfo + fdFlags, (mime[0] << 8) | mime[1]); |
188 |
|
|
189 |
|
// Close file |
190 |
|
close(fd); |
191 |
|
} |
192 |
|
|
193 |
< |
void set_finder_type(const char *path, uint32 type, uint32 creator) |
193 |
> |
void set_finfo(const char *path, uint32 finfo, uint32 fxinfo) |
194 |
|
{ |
195 |
+ |
char mime[256]; |
196 |
+ |
|
197 |
|
// Open file |
198 |
|
int fd = open(path, O_WRONLY); |
199 |
|
if (fd < 0) |
200 |
|
return; |
201 |
|
|
202 |
|
// Set BEOS:TYPE attribute |
203 |
+ |
uint32 type = ReadMacInt32(finfo + fdType); |
204 |
|
if (type) { |
205 |
|
bool written = false; |
206 |
|
for (int i=0; m2t_translation[i].mime; i++) { |
211 |
|
} |
212 |
|
} |
213 |
|
if (!written) { |
182 |
– |
char mime[256]; |
214 |
|
sprintf(mime, "application/x-MacOS-%c%c%c%c", type >> 24, type >> 16, type >> 8, type); |
215 |
|
fs_write_attr(fd, "BEOS:TYPE", B_MIME_STRING_TYPE, 0, mime, strlen(mime) + 1); |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
// Set MACOS:CREATOR attribute |
220 |
< |
if (creator) |
221 |
< |
fs_write_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, &creator, 4); |
222 |
< |
|
223 |
< |
// Close file |
224 |
< |
close(fd); |
225 |
< |
} |
226 |
< |
|
227 |
< |
|
197 |
< |
/* |
198 |
< |
* Get/set finder flags for file/dir specified by full path (MACOS:HFS_FLAGS attribute) |
199 |
< |
*/ |
200 |
< |
|
201 |
< |
void get_finder_flags(const char *path, uint16 &flags) |
202 |
< |
{ |
203 |
< |
flags = DEFAULT_FINDER_FLAGS; // Default |
204 |
< |
|
205 |
< |
// Open file |
206 |
< |
int fd = open(path, O_RDONLY); |
207 |
< |
if (fd < 0) |
208 |
< |
return; |
209 |
< |
|
210 |
< |
// Read MACOS:HFS_FLAGS attribute |
211 |
< |
fs_read_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, &flags, 2); |
212 |
< |
|
213 |
< |
// Close file |
214 |
< |
close(fd); |
215 |
< |
} |
216 |
< |
|
217 |
< |
void set_finder_flags(const char *path, uint16 flags) |
218 |
< |
{ |
219 |
< |
// Open file |
220 |
< |
int fd = open(path, O_WRONLY); |
221 |
< |
if (fd < 0) |
222 |
< |
return; |
220 |
> |
uint32 creator = ReadMacInt32(finfo + fdType); |
221 |
> |
if (creator) { |
222 |
> |
mime[0] = creator >> 24; |
223 |
> |
mime[1] = creator >> 16; |
224 |
> |
mime[2] = creator >> 8; |
225 |
> |
mime[3] = creator; |
226 |
> |
fs_write_attr(fd, "MACOS:CREATOR", B_UINT32_TYPE, 0, &mime, 4); |
227 |
> |
} |
228 |
|
|
229 |
|
// Write MACOS:HFS_FLAGS attribute |
230 |
< |
if (flags != DEFAULT_FINDER_FLAGS) |
231 |
< |
fs_write_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, &flags, 2); |
232 |
< |
else |
230 |
> |
uint16 flags = ReadMacInt16(finfo + fdFlags); |
231 |
> |
if (flags != DEFAULT_FINDER_FLAGS) { |
232 |
> |
mime[0] = flags >> 8; |
233 |
> |
mime[1] = flags; |
234 |
> |
fs_write_attr(fd, "MACOS:HFS_FLAGS", B_UINT16_TYPE, 0, &mime, 2); |
235 |
> |
} else |
236 |
|
fs_remove_attr(fd, "MACOS:HFS_FLAGS"); |
237 |
|
|
238 |
|
// Close file |
271 |
|
// Open temporary file for resource fork |
272 |
|
char rname[L_tmpnam]; |
273 |
|
tmpnam(rname); |
274 |
< |
int rfd = open(rname, O_RDWR | O_CREAT | O_TRUNC, 0664); |
274 |
> |
int rfd = open(rname, O_RDWR | O_CREAT | O_TRUNC, 0666); |
275 |
|
if (rfd < 0) { |
276 |
|
close(fd); |
277 |
|
return -1; |
357 |
|
|
358 |
|
/* |
359 |
|
* Read "length" bytes from file to "buffer", |
360 |
< |
* returns number of bytes read (or 0) |
360 |
> |
* returns number of bytes read (or -1 on error) |
361 |
|
*/ |
362 |
|
|
363 |
|
static inline ssize_t sread(int fd, void *buf, size_t count) |
367 |
|
return res; |
368 |
|
} |
369 |
|
|
370 |
< |
size_t extfs_read(int fd, void *buffer, size_t length) |
370 |
> |
ssize_t extfs_read(int fd, void *buffer, size_t length) |
371 |
|
{ |
364 |
– |
errno = 0; |
365 |
– |
|
372 |
|
// Buffer in kernel space? |
367 |
– |
size_t actual = 0; |
373 |
|
if ((uint32)buffer < 0x80000000) { |
374 |
|
|
375 |
|
// Yes, transfer via buffer |
376 |
+ |
ssize_t actual = 0; |
377 |
|
while (length) { |
378 |
|
size_t transfer_size = (length > TMP_BUF_SIZE) ? TMP_BUF_SIZE : length; |
379 |
|
ssize_t res = sread(fd, tmp_buf, transfer_size); |
380 |
< |
if (res >= 0) { |
381 |
< |
memcpy(buffer, tmp_buf, res); |
382 |
< |
buffer = (void *)((uint8 *)buffer + res); |
383 |
< |
length -= res; |
384 |
< |
actual += res; |
385 |
< |
} |
380 |
> |
if (res < 0) |
381 |
> |
return res; |
382 |
> |
memcpy(buffer, tmp_buf, res); |
383 |
> |
buffer = (void *)((uint8 *)buffer + res); |
384 |
> |
length -= res; |
385 |
> |
actual += res; |
386 |
|
if (res != transfer_size) |
387 |
|
return actual; |
388 |
|
} |
389 |
+ |
return actual; |
390 |
|
|
391 |
|
} else { |
392 |
|
|
393 |
|
// No, transfer directly |
394 |
< |
actual = sread(fd, buffer, length); |
388 |
< |
if (actual < 0) |
389 |
< |
actual = 0; |
394 |
> |
return sread(fd, buffer, length); |
395 |
|
} |
391 |
– |
return actual; |
396 |
|
} |
397 |
|
|
398 |
|
|
399 |
|
/* |
400 |
|
* Write "length" bytes from "buffer" to file, |
401 |
< |
* returns number of bytes written (or 0) |
401 |
> |
* returns number of bytes written (or -1 on error) |
402 |
|
*/ |
403 |
|
|
404 |
|
static inline ssize_t swrite(int fd, void *buf, size_t count) |
408 |
|
return res; |
409 |
|
} |
410 |
|
|
411 |
< |
size_t extfs_write(int fd, void *buffer, size_t length) |
411 |
> |
ssize_t extfs_write(int fd, void *buffer, size_t length) |
412 |
|
{ |
409 |
– |
errno = 0; |
410 |
– |
|
413 |
|
// Buffer in kernel space? |
412 |
– |
size_t actual = 0; |
414 |
|
if ((uint32)buffer < 0x80000000) { |
415 |
|
|
416 |
|
// Yes, transfer via buffer |
417 |
+ |
ssize_t actual = 0; |
418 |
|
while (length) { |
419 |
|
size_t transfer_size = (length > TMP_BUF_SIZE) ? TMP_BUF_SIZE : length; |
420 |
|
memcpy(tmp_buf, buffer, transfer_size); |
421 |
|
ssize_t res = swrite(fd, tmp_buf, transfer_size); |
422 |
< |
if (res >= 0) { |
423 |
< |
buffer = (void *)((uint8 *)buffer + res); |
424 |
< |
length -= res; |
425 |
< |
actual += res; |
426 |
< |
} |
422 |
> |
if (res < 0) |
423 |
> |
return res; |
424 |
> |
buffer = (void *)((uint8 *)buffer + res); |
425 |
> |
length -= res; |
426 |
> |
actual += res; |
427 |
|
if (res != transfer_size) |
428 |
|
return actual; |
429 |
|
} |
430 |
+ |
return actual; |
431 |
|
|
432 |
|
} else { |
433 |
|
|
434 |
|
// No, transfer directly |
435 |
< |
actual = swrite(fd, buffer, length); |
433 |
< |
if (actual < 0) |
434 |
< |
actual = 0; |
435 |
> |
return swrite(fd, buffer, length); |
436 |
|
} |
437 |
< |
return actual; |
437 |
> |
} |
438 |
> |
|
439 |
> |
|
440 |
> |
/* |
441 |
> |
* Remove file/directory, returns false on error (and sets errno) |
442 |
> |
*/ |
443 |
> |
|
444 |
> |
bool extfs_remove(const char *path) |
445 |
> |
{ |
446 |
> |
if (remove(path) < 0) { |
447 |
> |
if (errno == EISDIR) |
448 |
> |
return rmdir(path) == 0; |
449 |
> |
else |
450 |
> |
return false; |
451 |
> |
} |
452 |
> |
return true; |
453 |
> |
} |
454 |
> |
|
455 |
> |
|
456 |
> |
/* |
457 |
> |
* Rename/move file/directory, returns false on error (and sets errno) |
458 |
> |
*/ |
459 |
> |
|
460 |
> |
bool extfs_rename(const char *old_path, const char *new_path) |
461 |
> |
{ |
462 |
> |
return rename(old_path, new_path) == 0; |
463 |
|
} |