uint64_t size;
} SDArchiverInternalToWrite;
-void free_FILE_helper(FILE **fd) {
- if (fd && *fd) {
- fclose(*fd);
- *fd = NULL;
- }
-}
-
-void free_malloced_memory(void **data) {
- if (data && *data) {
- free(*data);
- *data = NULL;
- }
-}
-
void free_internal_to_write(void *data) {
SDArchiverInternalToWrite *to_write = data;
free(to_write->buf);
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
if (ptrs_array && *ptrs_array) {
if ((*ptrs_array)[1]) {
- free_FILE_helper((FILE **)(*ptrs_array)[1]);
+ simple_archiver_helper_cleanup_FILE((FILE **)(*ptrs_array)[1]);
}
if ((*ptrs_array)[0]) {
unlink((char *)((*ptrs_array)[0]));
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
- __attribute__((cleanup(free_malloced_memory))) void *path =
+ __attribute__((cleanup(simple_archiver_helper_cleanup_malloced))) void *path =
malloc(strlen(filename) + 1);
strncpy(path, filename, strlen(filename) + 1);
return NULL;
}
- __attribute__((cleanup(free_malloced_memory))) void *dir_realpath =
+ __attribute__((
+ cleanup(simple_archiver_helper_cleanup_malloced))) void *dir_realpath =
realpath(path_dir, NULL);
if (!dir_realpath) {
return NULL;
}
// Recreate "path" since it may have been modified by dirname().
- free_malloced_memory(&path);
+ simple_archiver_helper_cleanup_malloced(&path);
path = malloc(strlen(filename) + 1);
strncpy(path, filename, strlen(filename) + 1);
break;
}
} while (1);
- __attribute__((cleanup(free_FILE_helper))) FILE *file_fd =
- fopen(file_info->filename, "rb");
+ __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
+ FILE *file_fd = fopen(file_info->filename, "rb");
if (!file_fd) {
// Unable to open file for compressing and archiving.
return 1;
}
- __attribute__((cleanup(free_FILE_helper))) FILE *tmp_fd =
- fopen(temp_filename, "wb");
+ __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
+ FILE *tmp_fd = fopen(temp_filename, "wb");
if (!tmp_fd) {
fprintf(stderr, "ERROR: Unable to create temp file for compressing!\n");
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
- __attribute__((cleanup(free_malloced_memory))) void *real_cwd =
+ __attribute__((
+ cleanup(simple_archiver_helper_cleanup_malloced))) void *real_cwd =
realpath(".", NULL);
if (real_cwd) {
fprintf(stderr, "Tried to create temp file(s) in \"%s\"!\n",
}
} else {
if (feof(file_fd)) {
- free_FILE_helper(&file_fd);
+ simple_archiver_helper_cleanup_FILE(&file_fd);
write_done = 1;
close(pipe_into_cmd[1]);
// fprintf(stderr, "write_done\n");
}
} else if (ret == 0) {
read_done = 1;
- free_FILE_helper(&tmp_fd);
+ simple_archiver_helper_cleanup_FILE(&tmp_fd);
close(pipe_outof_cmd[0]);
// fprintf(stderr, "read_done\n");
} else if (ret == -1) {
} while (1);
// Cleanup.
- free_FILE_helper(&tmp_fd);
+ simple_archiver_helper_cleanup_FILE(&tmp_fd);
#endif
} else {
uint16_t u16;
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
// Write file length.
- __attribute__((cleanup(free_FILE_helper))) FILE *fd =
+ __attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) FILE *fd =
fopen(file_info->filename, "rb");
if (!fd) {
// Error.
// Need to get abs_path for checking/setting a flag before storing flags.
// Get absolute path.
- __attribute__((cleanup(free_malloced_memory))) void *abs_path = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *abs_path = NULL;
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
abs_path = realpath(file_info->filename, NULL);
#endif
- __attribute__((cleanup(free_malloced_memory))) void *rel_path = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *rel_path = NULL;
if (abs_path) {
// Get relative path.
// First get absolute path of link.
- __attribute__((cleanup(free_malloced_memory))) void *link_abs_path =
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *link_abs_path =
filename_to_absolute_path(file_info->filename);
if (!link_abs_path) {
fprintf(stderr, "WARNING: Failed to get absolute path of link!\n");
return 0;
}
-void cleanup_nop_fn(__attribute__((unused)) void *unused) {}
-void cleanup_free_fn(void *data) { free(data); }
-
-void simple_archiver_internal_chdir_back2(char **original) {
- if (original && *original) {
-#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX || \
- SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
- SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN
- chdir(*original);
-#endif
- free(*original);
- *original = NULL;
- }
-}
-
int filenames_to_abs_map_fn(void *data, void *ud) {
SDArchiverFileInfo *file_info = data;
void **ptr_array = ud;
SDArchiverHashMap **abs_filenames = ptr_array[0];
const char *user_cwd = ptr_array[1];
- __attribute__((
- cleanup(simple_archiver_internal_chdir_back2))) char *original_cwd = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_chdir_back))) char *original_cwd = NULL;
if (user_cwd) {
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
return 1;
}
- simple_archiver_hash_map_insert(abs_filenames, fullpath, fullpath,
- strlen(fullpath) + 1, cleanup_nop_fn, NULL);
+ simple_archiver_hash_map_insert(
+ abs_filenames, fullpath, fullpath, strlen(fullpath) + 1,
+ simple_archiver_helper_datastructure_cleanup_nop, NULL);
// Try putting all parent dirs up to current working directory.
// First get absolute path to current working directory.
- __attribute__((cleanup(free_malloced_memory))) void *cwd_dirname = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *cwd_dirname = NULL;
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
// fprintf(stderr, "cwd_dirname: %s\n", (char*)cwd_dirname);
// Use copy of fullpath to avoid clobbering it.
- __attribute__((cleanup(free_malloced_memory))) void *fullpath_copy =
+ __attribute__((
+ cleanup(simple_archiver_helper_cleanup_malloced))) void *fullpath_copy =
malloc(strlen(fullpath) + 1);
strncpy(fullpath_copy, fullpath, strlen(fullpath) + 1);
strlen(fullpath_dirname) + 1);
simple_archiver_hash_map_insert(
abs_filenames, fullpath_dirname_copy, fullpath_dirname_copy,
- strlen(fullpath_dirname_copy) + 1, cleanup_nop_fn, NULL);
+ strlen(fullpath_dirname_copy) + 1,
+ simple_archiver_helper_datastructure_cleanup_nop, NULL);
}
prev = fullpath_dirname;
}
state->map = abs_filenames;
state->digits = simple_archiver_helper_num_digits(state->max);
fprintf(stderr, "Begin archiving...\n");
- __attribute__((
- cleanup(simple_archiver_internal_chdir_back2))) char *original_cwd = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_chdir_back))) char *original_cwd = NULL;
if (state->parsed->user_cwd) {
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
#endif
}
- __attribute__((cleanup(free_malloced_memory))) void *decompressor_cmd = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *decompressor_cmd = NULL;
if ((buf[0] & 1) != 0) {
fprintf(stderr, "De/compressor flag is set.\n");
buf[1023] = 0;
fprintf(stderr, "Compressor cmd: %s\n", buf);
} else {
- __attribute__((cleanup(free_malloced_memory))) void *heap_buf =
+ __attribute__((
+ cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1);
unsigned char *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
memcpy((char *)decompressor_cmd, buf, u16 + 1);
((char *)decompressor_cmd)[u16] = 0;
} else {
- __attribute__((cleanup(free_malloced_memory))) void *heap_buf =
+ __attribute__((
+ cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1);
unsigned char *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
char *key = malloc(len);
memcpy(key, *iter, len);
key[len - 1] = 0;
- simple_archiver_hash_map_insert(&hash_map, key, key, len, cleanup_nop_fn,
- cleanup_free_fn);
+ simple_archiver_hash_map_insert(
+ &hash_map, key, key, len,
+ simple_archiver_helper_datastructure_cleanup_nop, NULL);
// fprintf(stderr, "\"%s\" put in map\n", key);
}
}
return SDAS_INVALID_FILE;
}
simple_archiver_helper_16_bit_be(&u16);
- __attribute__((cleanup(free_malloced_memory))) void *out_f_name = NULL;
- __attribute__((cleanup(free_FILE_helper))) FILE *out_f = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *out_f_name = NULL;
+ __attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) FILE *out_f =
+ NULL;
if (u16 < 1024) {
if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE;
fprintf(stderr, " Filename: %s\n", buf);
if (do_extract) {
if ((state->parsed->flags & 0x8) == 0) {
- __attribute__((cleanup(free_FILE_helper))) FILE *test_fd =
- fopen((const char *)buf, "rb");
+ __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
+ FILE *test_fd = fopen((const char *)buf, "rb");
if (test_fd) {
skip = 1;
fprintf(stderr,
}
}
} else {
- __attribute__((cleanup(free_malloced_memory))) void *heap_buf =
+ __attribute__((
+ cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1);
unsigned char *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
fprintf(stderr, " Filename: %s\n", uc_heap_buf);
if (do_extract) {
if ((state->parsed->flags & 0x8) == 0) {
- __attribute__((cleanup(free_FILE_helper))) FILE *test_fd =
- fopen((const char *)buf, "rb");
+ __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
+ FILE *test_fd = fopen((const char *)buf, "rb");
if (test_fd) {
skip = 1;
fprintf(stderr,
// EOF.
read_pipe_done = 1;
close(pipe_outof_cmd[0]);
- free_FILE_helper(&out_f);
+ simple_archiver_helper_cleanup_FILE(&out_f);
} else {
// Invalid state (unreachable?), error.
fprintf(stderr,
fprintf(stderr, " Absolute path is %s\n",
(abs_preferred ? "preferred" : "NOT preferred"));
- __attribute__((cleanup(free_malloced_memory))) void *abs_path = NULL;
- __attribute__((cleanup(free_malloced_memory))) void *rel_path = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *abs_path = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_malloced))) void *rel_path = NULL;
if (fread(&u16, 2, 1, in_f) != 1) {
return SDAS_INVALID_FILE;
}
}
-void simple_archiver_internal_chdir_back(char **original) {
- if (original && *original) {
-#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX || \
- SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
- SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN
- chdir(*original);
-#endif
- free(*original);
- *original = NULL;
- }
-}
-
void simple_archiver_internal_free_file_info_fn(void *data) {
SDArchiverFileInfo *file_info = data;
if (file_info) {
return 0;
}
-void container_no_free_fn(__attribute__((unused)) void *data) { return; }
-
int list_remove_same_str_fn(void *data, void *ud) {
if (strcmp((char *)data, (char *)ud) == 0) {
return 1;
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
- __attribute__((
- cleanup(simple_archiver_internal_chdir_back))) char *original_cwd = NULL;
+ __attribute__((cleanup(
+ simple_archiver_helper_cleanup_chdir_back))) char *original_cwd = NULL;
if (parsed->user_cwd) {
original_cwd = realpath(".", NULL);
if (chdir(parsed->user_cwd)) {
}
simple_archiver_list_add(files_list, file_info,
simple_archiver_internal_free_file_info_fn);
- simple_archiver_hash_map_insert(&hash_map, &hash_map_sentinel, filename,
- len - 1, container_no_free_fn,
- container_no_free_fn);
+ simple_archiver_hash_map_insert(
+ &hash_map, &hash_map_sentinel, filename, len - 1,
+ simple_archiver_helper_datastructure_cleanup_nop,
+ simple_archiver_helper_datastructure_cleanup_nop);
} else {
free(filename);
}
// Is a directory.
__attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *dir_list = simple_archiver_list_init();
- simple_archiver_list_add(dir_list, file_path, container_no_free_fn);
+ simple_archiver_list_add(
+ dir_list, file_path,
+ simple_archiver_helper_datastructure_cleanup_nop);
char *next;
while (dir_list->count != 0) {
simple_archiver_list_get(dir_list, list_get_last_fn, &next);
simple_archiver_internal_free_file_info_fn);
simple_archiver_hash_map_insert(
&hash_map, &hash_map_sentinel, combined_path,
- combined_size - 1, container_no_free_fn,
- container_no_free_fn);
+ combined_size - 1,
+ simple_archiver_helper_datastructure_cleanup_nop,
+ simple_archiver_helper_datastructure_cleanup_nop);
} else {
free(combined_path);
}