amar_t * amar_new( int fd, mode_t mode, GError **error) { amar_t *archive = malloc(sizeof(amar_t)); assert(archive != NULL); /* make some sanity checks first */ g_assert(fd >= 0); g_assert(mode == O_RDONLY || mode == O_WRONLY); archive->fd = fd; archive->mode = mode; archive->maxfilenum = 0; archive->position = 0; archive->seekable = TRUE; /* assume seekable until lseek() fails */ archive->files = g_hash_table_new(g_int_hash, g_int_equal); archive->buf = NULL; if (mode == O_WRONLY) { archive->buf = g_malloc(WRITE_BUFFER_SIZE); archive->buf_size = WRITE_BUFFER_SIZE; } archive->buf_len = 0; if (mode == O_WRONLY) { /* preformat a header with our version number */ bzero(archive->hdr.magic, HEADER_SIZE); snprintf(archive->hdr.magic, HEADER_SIZE, HEADER_MAGIC " %d", HEADER_VERSION); /* and write it out to start the file */ if (!write_header(archive, error)) { amar_close(archive, NULL); /* flushing buffer won't fail */ return NULL; } } return archive; }
static void do_create(char *opt_file, int opt_verbose, int argc, char **argv) { FILE *output = stdout; amar_t *archive; amar_file_t *file; amar_attr_t *attribute; GError *error = NULL; int i, fd_out, fd_in; off_t filesize = 0; if (opt_file != NULL && strcmp(opt_file,"-") != 0) { fd_out = open(opt_file, O_CREAT|O_WRONLY|O_TRUNC, 0660); if (fd_out <= 0) { error("open of '%s' failed: %s\n", opt_file, strerror(errno)); } } else { fd_out = fileno(stdout); output = stderr; } archive = amar_new(fd_out, O_WRONLY, &error); if (!archive) error_exit("amar_new", error); i = 0; while (i<argc) { fd_in = open(argv[i], O_RDONLY); if (fd_in <= 0) { g_fprintf(stderr, "open of '%s' failed: %s\n", argv[i], strerror(errno)); i++; continue; } filesize = 0; file = amar_new_file(archive, argv[i], strlen(argv[i]), NULL, &error); if (error) error_exit("amar_new_file", error); attribute = amar_new_attr(file, AMAR_ATTR_GENERIC_DATA, &error); if (error) error_exit("amar_new_attr", error); filesize += amar_attr_add_data_fd(attribute, fd_in, 1, &error); if (error) error_exit("amar_attr_add_data_fd", error); if (!amar_attr_close(attribute, &error)) error_exit("amar_attr_close", error); if (!amar_file_close(file, &error)) error_exit("amar_file_close", error); if (opt_verbose == 1) { g_fprintf(output,"%s\n", argv[i]); } else if (opt_verbose > 1) { g_fprintf(output,"%llu %s\n", (unsigned long long)filesize, argv[i]); } close(fd_in); i++; } if (!amar_close(archive, &error)) error_exit("amar_close", error); close(fd_out); }