12 |
#include <stdio.h> |
#include <stdio.h> |
13 |
#include <stdlib.h> |
#include <stdlib.h> |
14 |
#include <string.h> |
#include <string.h> |
15 |
|
#include <strings.h> |
16 |
#include <sys/stat.h> |
#include <sys/stat.h> |
17 |
#include <sys/types.h> |
#include <sys/types.h> |
18 |
#include <unistd.h> |
#include <unistd.h> |
25 |
# include <limits.h> |
# include <limits.h> |
26 |
#endif |
#endif |
27 |
|
|
28 |
|
#define UAR_ROOT_DIR_NAME 0x01 |
29 |
|
|
30 |
const unsigned char UAR_MAGIC[] = { 0x99, 'U', 'A', 'R' }; |
const unsigned char UAR_MAGIC[] = { 0x99, 'U', 'A', 'R' }; |
31 |
|
|
32 |
struct uar_header |
struct uar_header |
38 |
uint64_t size; |
uint64_t size; |
39 |
} __attribute__ ((packed)); |
} __attribute__ ((packed)); |
40 |
|
|
|
enum uar_file_type |
|
|
{ |
|
|
UF_FILE, |
|
|
UF_DIR, |
|
|
UF_LINK, |
|
|
}; |
|
|
|
|
41 |
struct uar_file |
struct uar_file |
42 |
{ |
{ |
43 |
enum uar_file_type type; |
enum uar_file_type type; |
44 |
|
int __pad1; |
45 |
char *name; |
char *name; |
46 |
uint64_t namelen; |
uint64_t namelen; |
47 |
uint32_t offset; |
uint64_t offset; |
48 |
union |
union |
49 |
{ |
{ |
50 |
uint64_t size; |
uint64_t size; |
55 |
} linkinfo; |
} linkinfo; |
56 |
} data; |
} data; |
57 |
mode_t mode; |
mode_t mode; |
58 |
|
int __pad2; |
59 |
}; |
}; |
60 |
|
|
61 |
struct uar_archive |
struct uar_archive |
107 |
} |
} |
108 |
|
|
109 |
struct uar_archive * |
struct uar_archive * |
110 |
|
uar_create (void) |
111 |
|
{ |
112 |
|
struct uar_archive *uar = malloc (sizeof (struct uar_archive)); |
113 |
|
|
114 |
|
if (uar == NULL) |
115 |
|
return NULL; |
116 |
|
|
117 |
|
uar->is_stream = false; |
118 |
|
uar->buffer = NULL; |
119 |
|
uar->ecode = UAR_SUCCESS; |
120 |
|
uar->header.size = 0; |
121 |
|
memcpy (uar->header.magic, UAR_MAGIC, 4); |
122 |
|
uar->header.version = 1; |
123 |
|
uar->header.flags = 0; |
124 |
|
uar->header.nfiles = 0; |
125 |
|
uar->files = NULL; |
126 |
|
|
127 |
|
return uar; |
128 |
|
} |
129 |
|
|
130 |
|
struct uar_archive * |
131 |
uar_open (const char *filename) |
uar_open (const char *filename) |
132 |
{ |
{ |
133 |
struct uar_archive *uar = NULL; |
struct uar_archive *uar = NULL; |
135 |
int cerrno; |
int cerrno; |
136 |
|
|
137 |
errno = 0; |
errno = 0; |
138 |
uar = malloc (sizeof (struct uar_archive)); |
uar = uar_create (); |
139 |
|
|
140 |
if (uar == NULL) |
if (uar == NULL) |
141 |
return NULL; |
return NULL; |
208 |
goto uar_open_ret; |
goto uar_open_ret; |
209 |
} |
} |
210 |
|
|
211 |
file->name = malloc (file->namelen); |
file->name = malloc (file->namelen + 1); |
212 |
|
|
213 |
if (file->name == NULL) |
if (file->name == NULL) |
214 |
{ |
{ |
222 |
goto uar_open_ret; |
goto uar_open_ret; |
223 |
} |
} |
224 |
|
|
225 |
|
file->name[file->namelen] = 0; |
226 |
uar->files[i] = file; |
uar->files[i] = file; |
227 |
} |
} |
228 |
|
|
248 |
} |
} |
249 |
|
|
250 |
void |
void |
251 |
uar_close (struct uar_archive *uar) |
uar_file_destroy (struct uar_file *file) |
252 |
{ |
{ |
253 |
if (uar == NULL) |
if (file == NULL) |
254 |
return; |
return; |
255 |
|
|
256 |
free (uar->buffer); |
free (file->name); |
257 |
free (uar->files); |
free (file); |
|
free (uar); |
|
258 |
} |
} |
259 |
|
|
260 |
struct uar_archive * |
void |
261 |
uar_create () |
uar_close (struct uar_archive *uar) |
262 |
{ |
{ |
|
struct uar_archive *uar = malloc (sizeof (struct uar_archive)); |
|
|
|
|
263 |
if (uar == NULL) |
if (uar == NULL) |
264 |
return NULL; |
return; |
265 |
|
|
266 |
uar->is_stream = false; |
free (uar->buffer); |
|
uar->buffer = NULL; |
|
|
uar->header.size = sizeof (struct uar_header); |
|
|
memcpy (uar->header.magic, UAR_MAGIC, 4); |
|
|
uar->header.version = 1; |
|
|
uar->header.flags = 0; |
|
|
uar->header.nfiles = 0; |
|
267 |
|
|
268 |
return uar; |
for (uint64_t i = 0; i < uar->header.nfiles; i++) |
269 |
|
{ |
270 |
|
struct uar_file *file = uar->files[i]; |
271 |
|
uar_file_destroy (file); |
272 |
|
} |
273 |
|
|
274 |
|
free (uar->files); |
275 |
|
free (uar); |
276 |
} |
} |
277 |
|
|
278 |
bool |
bool |
303 |
uint32_t offset) |
uint32_t offset) |
304 |
{ |
{ |
305 |
struct uar_file *file; |
struct uar_file *file; |
306 |
|
int cerrno; |
307 |
assert (namelen < PATH_MAX); |
assert (namelen < PATH_MAX); |
308 |
|
|
309 |
file = malloc (sizeof (struct uar_file)); |
file = malloc (sizeof (struct uar_file)); |
311 |
if (file == NULL) |
if (file == NULL) |
312 |
return NULL; |
return NULL; |
313 |
|
|
314 |
|
bzero (file, sizeof (struct uar_file)); |
315 |
|
|
316 |
file->type = UF_FILE; |
file->type = UF_FILE; |
317 |
file->mode = 0644; |
file->mode = 0644; |
318 |
file->name = malloc (namelen + 1); |
file->name = malloc (namelen + 1); |
319 |
|
|
320 |
if (file->name == NULL) |
if (file->name == NULL) |
321 |
{ |
{ |
322 |
|
cerrno = errno; |
323 |
free (file); |
free (file); |
324 |
|
errno = cerrno; |
325 |
return NULL; |
return NULL; |
326 |
} |
} |
327 |
|
|
334 |
return file; |
return file; |
335 |
} |
} |
336 |
|
|
|
void |
|
|
uar_file_destroy (struct uar_file *file) |
|
|
{ |
|
|
if (file == NULL) |
|
|
return; |
|
|
|
|
|
free (file->name); |
|
|
free (file); |
|
|
} |
|
|
|
|
337 |
struct uar_file * |
struct uar_file * |
338 |
uar_add_file (struct uar_archive *restrict uar, const char *name, |
uar_add_file (struct uar_archive *restrict uar, const char *name, |
339 |
const char *path) |
const char *path) |
360 |
} |
} |
361 |
|
|
362 |
fseek (stream, 0, SEEK_END); |
fseek (stream, 0, SEEK_END); |
363 |
uint64_t size = ftell (stream); |
long size = ftell (stream); |
364 |
|
|
365 |
|
if (size < 0) |
366 |
|
{ |
367 |
|
uar_set_error (uar, UAR_IO_ERROR); |
368 |
|
fclose (stream); |
369 |
|
return NULL; |
370 |
|
} |
371 |
|
|
372 |
fseek (stream, 0, SEEK_SET); |
fseek (stream, 0, SEEK_SET); |
373 |
|
|
374 |
struct uar_file *file |
struct uar_file *file |
399 |
return NULL; |
return NULL; |
400 |
} |
} |
401 |
|
|
402 |
if (fread (uar->buffer + file->offset, size, 1, stream) != 1) |
if (size != 0 && fread (uar->buffer + file->offset, size, 1, stream) != 1) |
403 |
{ |
{ |
404 |
uar_set_error (uar, UAR_IO_ERROR); |
uar_set_error (uar, UAR_IO_ERROR); |
405 |
fclose (stream); |
fclose (stream); |
426 |
} |
} |
427 |
|
|
428 |
struct uar_file * |
struct uar_file * |
429 |
uar_add_dir (struct uar_archive *uar, const char *name, const char *path) |
uar_add_dir (struct uar_archive *uar, const char *dname, const char *path, |
430 |
|
bool (*callback) (struct uar_file *file, const char *fullname, |
431 |
|
const char *fullpath)) |
432 |
{ |
{ |
433 |
assert (uar != NULL && "uar is NULL"); |
assert (uar != NULL && "uar is NULL"); |
434 |
assert (name != NULL && "name is NULL"); |
assert (dname != NULL && "dname is NULL"); |
435 |
assert (path != NULL && "path is NULL"); |
assert (path != NULL && "path is NULL"); |
436 |
assert (!uar->is_stream && "uar in non-stream mode is not supported yet"); |
assert (!uar->is_stream && "uar in non-stream mode is not supported yet"); |
437 |
|
|
438 |
uint64_t namelen = strlen (name); |
char *name = (char *) dname; |
439 |
|
bool free_name = false; |
440 |
|
int cerrno; |
441 |
|
uint64_t namelen; |
442 |
|
|
443 |
|
if (strcmp (name, ".") == 0) |
444 |
|
{ |
445 |
|
name = malloc (2); |
446 |
|
name[0] = UAR_ROOT_DIR_NAME; |
447 |
|
name[1] = 0; |
448 |
|
free_name = true; |
449 |
|
namelen = 1; |
450 |
|
} |
451 |
|
else |
452 |
|
namelen = strlen (name); |
453 |
|
|
454 |
if (namelen >= PATH_MAX) |
if (namelen >= PATH_MAX) |
455 |
{ |
{ |
470 |
= uar_file_create (name, namelen, 0, uar->header.size); |
= uar_file_create (name, namelen, 0, uar->header.size); |
471 |
uint64_t dir_size = 0; |
uint64_t dir_size = 0; |
472 |
|
|
473 |
|
dir_file->type = UF_DIR; |
474 |
|
|
475 |
|
if (callback != NULL && !callback (dir_file, name, path)) |
476 |
|
{ |
477 |
|
uar_set_error (uar, UAR_SUCCESS); |
478 |
|
uar_file_destroy (dir_file); |
479 |
|
return NULL; |
480 |
|
} |
481 |
|
|
482 |
if (!uar_add_file_entry (uar, dir_file)) |
if (!uar_add_file_entry (uar, dir_file)) |
483 |
{ |
{ |
484 |
uar_file_destroy (dir_file); |
uar_file_destroy (dir_file); |
513 |
|
|
514 |
if (stat (fullpath, &stinfo) != 0) |
if (stat (fullpath, &stinfo) != 0) |
515 |
{ |
{ |
|
int current_errno = errno; |
|
516 |
uar_set_error (uar, UAR_IO_ERROR); |
uar_set_error (uar, UAR_IO_ERROR); |
517 |
uar_file_destroy (dir_file); |
goto uar_add_dir_error; |
|
closedir (dir); |
|
|
free (fullpath); |
|
|
free (fullname); |
|
|
errno = current_errno; |
|
|
return NULL; |
|
518 |
} |
} |
519 |
|
|
520 |
if (S_ISREG (stinfo.st_mode)) |
if (S_ISREG (stinfo.st_mode)) |
524 |
|
|
525 |
if (file == NULL) |
if (file == NULL) |
526 |
{ |
{ |
527 |
int current_errno = errno; |
goto uar_add_dir_error; |
528 |
uar_file_destroy (dir_file); |
} |
529 |
closedir (dir); |
|
530 |
free (fullpath); |
if (callback != NULL |
531 |
free (fullname); |
&& !callback (file, fullname, fullpath)) |
532 |
errno = current_errno; |
{ |
533 |
return NULL; |
uar_set_error (uar, UAR_SUCCESS); |
534 |
|
goto uar_add_dir_error; |
535 |
} |
} |
536 |
|
|
537 |
file->mode = stinfo.st_mode & 07777; |
file->mode = stinfo.st_mode & 07777; |
540 |
else if (S_ISDIR (stinfo.st_mode)) |
else if (S_ISDIR (stinfo.st_mode)) |
541 |
{ |
{ |
542 |
struct uar_file *direntry |
struct uar_file *direntry |
543 |
= uar_add_dir (uar, fullname, fullpath); |
= uar_add_dir (uar, fullname, fullpath, callback); |
544 |
|
|
545 |
if (direntry == NULL) |
if (direntry == NULL) |
546 |
{ |
{ |
547 |
int current_errno = errno; |
goto uar_add_dir_error; |
|
uar_file_destroy (dir_file); |
|
|
closedir (dir); |
|
|
free (fullpath); |
|
|
free (fullname); |
|
|
errno = current_errno; |
|
|
return NULL; |
|
548 |
} |
} |
549 |
|
|
550 |
direntry->mode = stinfo.st_mode & 07777; |
direntry->mode = stinfo.st_mode & 07777; |
554 |
assert (false && "Not supported"); |
assert (false && "Not supported"); |
555 |
|
|
556 |
free (fullpath); |
free (fullpath); |
557 |
|
free (fullname); |
558 |
|
|
559 |
|
continue; |
560 |
|
|
561 |
|
uar_add_dir_error: |
562 |
|
cerrno = errno; |
563 |
|
uar_file_destroy (dir_file); |
564 |
|
free (fullpath); |
565 |
|
free (fullname); |
566 |
|
errno = cerrno; |
567 |
|
goto uar_add_dir_end; |
568 |
} |
} |
569 |
|
|
570 |
|
dir_file->data.size = dir_size; |
571 |
|
|
572 |
|
uar_add_dir_end: |
573 |
|
cerrno = errno; |
574 |
closedir (dir); |
closedir (dir); |
575 |
|
|
576 |
dir_file->type = UF_DIR; |
if (free_name) |
577 |
dir_file->data.size = dir_size; |
free (name); |
578 |
|
|
579 |
|
errno = cerrno; |
580 |
return dir_file; |
return dir_file; |
581 |
} |
} |
582 |
|
|
633 |
: file->type == UF_DIR ? "directory" |
: file->type == UF_DIR ? "directory" |
634 |
: "link", |
: "link", |
635 |
i); |
i); |
636 |
printf (" name: \033[1m%s%s\033[0m\n", file->name, |
printf (" name: \033[1m%s%s%s\033[0m\n", |
637 |
|
file->name[0] == UAR_ROOT_DIR_NAME ? "/" : "", |
638 |
|
file->name[0] == UAR_ROOT_DIR_NAME ? file->name + 2 |
639 |
|
: file->name, |
640 |
file->type == UF_DIR ? "/" |
file->type == UF_DIR ? "/" |
641 |
: file->type == UF_LINK ? "@" |
: file->type == UF_LINK ? "@" |
642 |
: ""); |
: ""); |
643 |
printf (" offset: %u\n", file->offset); |
printf (" offset: %lu\n", file->offset); |
644 |
printf (" mode: %04o\n", file->mode); |
printf (" mode: %04o\n", file->mode); |
645 |
|
|
646 |
switch (file->type) |
switch (file->type) |
697 |
} |
} |
698 |
} |
} |
699 |
|
|
700 |
if (fwrite (uar->buffer, uar->header.size, 1, stream) != 1) |
if (fwrite (uar->buffer, 1, uar->header.size, stream) != uar->header.size) |
701 |
{ |
{ |
702 |
uar_set_error (uar, UAR_IO_ERROR); |
uar_set_error (uar, UAR_IO_ERROR); |
703 |
fclose (stream); |
fclose (stream); |
711 |
const char * |
const char * |
712 |
uar_get_file_name (const struct uar_file *file) |
uar_get_file_name (const struct uar_file *file) |
713 |
{ |
{ |
714 |
return file->name; |
return file->name[0] == UAR_ROOT_DIR_NAME |
715 |
|
? file->namelen == 1 ? "/" : file->name + 1 |
716 |
|
: file->name; |
717 |
|
} |
718 |
|
|
719 |
|
enum uar_file_type |
720 |
|
uar_get_entry_type (const struct uar_file *file) |
721 |
|
{ |
722 |
|
return file->type; |
723 |
} |
} |
724 |
|
|
725 |
bool |
bool |
739 |
if (callback != NULL && !callback (file)) |
if (callback != NULL && !callback (file)) |
740 |
return false; |
return false; |
741 |
|
|
742 |
|
char *name = file->name; |
743 |
|
|
744 |
|
if (name[0] == UAR_ROOT_DIR_NAME) |
745 |
|
name += 2; |
746 |
|
|
747 |
switch (file->type) |
switch (file->type) |
748 |
{ |
{ |
749 |
case UF_FILE: |
case UF_FILE: |
750 |
{ |
{ |
751 |
FILE *stream = fopen (file->name, "wb"); |
FILE *stream = fopen (name, "wb"); |
752 |
|
|
753 |
if (stream == NULL) |
if (stream == NULL) |
754 |
{ |
{ |
770 |
break; |
break; |
771 |
|
|
772 |
case UF_DIR: |
case UF_DIR: |
773 |
if (mkdir (file->name, file->mode) != 0) |
if (file->namelen == 1 |
774 |
|
&& file->name[0] == UAR_ROOT_DIR_NAME) |
775 |
|
continue; |
776 |
|
|
777 |
|
if (mkdir (name, file->mode) != 0) |
778 |
{ |
{ |
779 |
uar_set_error (uar, UAR_SYSTEM_ERROR); |
uar_set_error (uar, UAR_SYSTEM_ERROR); |
780 |
return false; |
return false; |