60 |
|
* Add component to path name |
61 |
|
*/ |
62 |
|
|
63 |
< |
void add_path_component(char *path, const char *component, int max_len) |
63 |
> |
void add_path_component(char *path, const char *component) |
64 |
|
{ |
65 |
|
int l = strlen(path); |
66 |
< |
if (l < max_len-1 && path[l-1] != '/') { |
66 |
> |
if (l < MAX_PATH_LENGTH-1 && path[l-1] != '/') { |
67 |
|
path[l] = '/'; |
68 |
|
path[l+1] = 0; |
69 |
|
} |
70 |
< |
strncat(path, component, max_len-1); |
70 |
> |
strncat(path, component, MAX_PATH_LENGTH-1); |
71 |
> |
} |
72 |
> |
|
73 |
> |
|
74 |
> |
/* |
75 |
> |
* Finder info and resource forks are kept in helper files |
76 |
> |
* |
77 |
> |
* Finder info: |
78 |
> |
* /path/.finf/file |
79 |
> |
* Resource fork: |
80 |
> |
* /path/.rsrc/file |
81 |
> |
*/ |
82 |
> |
|
83 |
> |
// Layout of Finder info helper files (all fields big-endian) |
84 |
> |
struct finf_struct { |
85 |
> |
uint32 type; |
86 |
> |
uint32 creator; |
87 |
> |
uint16 flags; |
88 |
> |
}; |
89 |
> |
|
90 |
> |
static void make_helper_path(const char *src, char *dest, const char *add, bool only_dir = false) |
91 |
> |
{ |
92 |
> |
dest[0] = 0; |
93 |
> |
|
94 |
> |
// Get pointer to last component of path |
95 |
> |
const char *last_part = strrchr(src, '/'); |
96 |
> |
if (last_part) |
97 |
> |
last_part++; |
98 |
> |
else |
99 |
> |
last_part = src; |
100 |
> |
|
101 |
> |
// Copy everything before |
102 |
> |
strncpy(dest, src, last_part-src); |
103 |
> |
dest[last_part-src] = 0; |
104 |
> |
|
105 |
> |
// Add additional component |
106 |
> |
strncat(dest, add, MAX_PATH_LENGTH-1); |
107 |
> |
|
108 |
> |
// Add last component |
109 |
> |
if (!only_dir) |
110 |
> |
strncat(dest, last_part, MAX_PATH_LENGTH-1); |
111 |
> |
} |
112 |
> |
|
113 |
> |
static int create_helper_dir(const char *path, const char *add) |
114 |
> |
{ |
115 |
> |
char helper_dir[MAX_PATH_LENGTH]; |
116 |
> |
make_helper_path(path, helper_dir, add, true); |
117 |
> |
return mkdir(helper_dir, 0755); |
118 |
> |
} |
119 |
> |
|
120 |
> |
static int open_helper(const char *path, const char *add, int flag) |
121 |
> |
{ |
122 |
> |
char helper_path[MAX_PATH_LENGTH]; |
123 |
> |
make_helper_path(path, helper_path, add); |
124 |
> |
|
125 |
> |
if ((flag & O_RDWR) || (flag && O_WRONLY)) |
126 |
> |
flag |= O_CREAT; |
127 |
> |
int fd = open(helper_path, flag, 0644); |
128 |
> |
if (fd < 0) { |
129 |
> |
if (errno == ENOENT && (flag & O_CREAT)) { |
130 |
> |
// One path component was missing, probably the helper |
131 |
> |
// directory. Try to create it and re-open the file. |
132 |
> |
int ret = create_helper_dir(path, add); |
133 |
> |
if (ret < 0) |
134 |
> |
return ret; |
135 |
> |
fd = open(helper_path, flag, 0644); |
136 |
> |
} |
137 |
> |
} |
138 |
> |
return fd; |
139 |
> |
} |
140 |
> |
|
141 |
> |
static int open_finf(const char *path, int flag) |
142 |
> |
{ |
143 |
> |
return open_helper(path, ".finf/", flag); |
144 |
> |
} |
145 |
> |
|
146 |
> |
static int open_rsrc(const char *path, int flag) |
147 |
> |
{ |
148 |
> |
return open_helper(path, ".rsrc/", flag); |
149 |
|
} |
150 |
|
|
151 |
|
|
219 |
|
type = 0; |
220 |
|
creator = 0; |
221 |
|
|
222 |
< |
// Translate file name extension to MacOS type/creator |
222 |
> |
// Open Finder info file |
223 |
> |
int fd = open_finf(path, O_RDONLY); |
224 |
> |
if (fd >= 0) { |
225 |
> |
|
226 |
> |
// Read file |
227 |
> |
finf_struct finf; |
228 |
> |
if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) { |
229 |
> |
|
230 |
> |
// Type/creator are in Finder info file, return them |
231 |
> |
type = ntohl(finf.type); |
232 |
> |
creator = ntohl(finf.creator); |
233 |
> |
close(fd); |
234 |
> |
return; |
235 |
> |
} |
236 |
> |
close(fd); |
237 |
> |
} |
238 |
> |
|
239 |
> |
// No Finder info file, translate file name extension to MacOS type/creator |
240 |
|
int path_len = strlen(path); |
241 |
|
for (int i=0; e2t_translation[i].ext; i++) { |
242 |
|
int ext_len = strlen(e2t_translation[i].ext); |
252 |
|
|
253 |
|
void set_finder_type(const char *path, uint32 type, uint32 creator) |
254 |
|
{ |
255 |
+ |
// Open Finder info file |
256 |
+ |
int fd = open_finf(path, O_RDWR); |
257 |
+ |
if (fd < 0) |
258 |
+ |
return; |
259 |
+ |
|
260 |
+ |
// Read file |
261 |
+ |
finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS}; |
262 |
+ |
read(fd, &finf, sizeof(finf_struct)); |
263 |
+ |
|
264 |
+ |
// Set Finder flags |
265 |
+ |
finf.type = htonl(type); |
266 |
+ |
finf.creator = htonl(creator); |
267 |
+ |
|
268 |
+ |
// Update file |
269 |
+ |
lseek(fd, 0, SEEK_SET); |
270 |
+ |
write(fd, &finf, sizeof(finf_struct)); |
271 |
+ |
close(fd); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
/* |
276 |
< |
* Get/set finder flags for file/dir specified by full path (MACOS:HFS_FLAGS attribute) |
276 |
> |
* Get/set finder flags for file/dir specified by full path |
277 |
|
*/ |
278 |
|
|
279 |
|
void get_finder_flags(const char *path, uint16 &flags) |
280 |
|
{ |
281 |
|
flags = DEFAULT_FINDER_FLAGS; // Default |
282 |
+ |
|
283 |
+ |
// Open Finder info file |
284 |
+ |
int fd = open_finf(path, O_RDONLY); |
285 |
+ |
if (fd < 0) |
286 |
+ |
return; |
287 |
+ |
|
288 |
+ |
// Read Finder flags |
289 |
+ |
finf_struct finf; |
290 |
+ |
if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) |
291 |
+ |
flags = ntohs(finf.flags); |
292 |
+ |
|
293 |
+ |
// Close file |
294 |
+ |
close(fd); |
295 |
|
} |
296 |
|
|
297 |
|
void set_finder_flags(const char *path, uint16 flags) |
298 |
|
{ |
299 |
+ |
// Open Finder info file |
300 |
+ |
int fd = open_finf(path, O_RDWR); |
301 |
+ |
if (fd < 0) |
302 |
+ |
return; |
303 |
+ |
|
304 |
+ |
// Read file |
305 |
+ |
finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS}; |
306 |
+ |
read(fd, &finf, sizeof(finf_struct)); |
307 |
+ |
|
308 |
+ |
// Set Finder flags |
309 |
+ |
finf.flags = htons(flags); |
310 |
+ |
|
311 |
+ |
// Update file |
312 |
+ |
lseek(fd, 0, SEEK_SET); |
313 |
+ |
write(fd, &finf, sizeof(finf_struct)); |
314 |
+ |
close(fd); |
315 |
|
} |
316 |
|
|
317 |
|
|
321 |
|
|
322 |
|
uint32 get_rfork_size(const char *path) |
323 |
|
{ |
324 |
< |
return 0; |
324 |
> |
// Open resource file |
325 |
> |
int fd = open_rsrc(path, O_RDONLY); |
326 |
> |
if (fd < 0) |
327 |
> |
return 0; |
328 |
> |
|
329 |
> |
// Get size |
330 |
> |
off_t size = lseek(fd, 0, SEEK_END); |
331 |
> |
|
332 |
> |
// Close file and return size |
333 |
> |
close(fd); |
334 |
> |
return size < 0 ? 0 : size; |
335 |
|
} |
336 |
|
|
337 |
|
int open_rfork(const char *path, int flag) |
338 |
|
{ |
339 |
< |
return -1; |
339 |
> |
return open_rsrc(path, flag); |
340 |
|
} |
341 |
|
|
342 |
|
void close_rfork(const char *path, int fd) |
343 |
|
{ |
344 |
+ |
close(fd); |
345 |
|
} |
346 |
|
|
347 |
|
|