static char *
extract_archive(FILE *src_stream, FILE *out_stream,
const file_header_t *file_entry, const int function,
- const char *prefix)
+ const char *prefix,
+ int *err)
{
FILE *dst_stream = NULL;
char *full_name = NULL;
char *buffer = NULL;
struct utimbuf t;
+ *err = 0;
/* prefix doesnt have to be a proper path it may prepend
* the filename as well */
char *path = file_entry->name;
if (strncmp("./", path, 2) == 0) {
path += 2;
- if (strlen(path) == 0) {
- return(NULL);
- }
+ if (strlen(path) == 0)
+ /* Do nothing, current dir already exists. */
+ return NULL;
}
full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
strcpy(full_name, prefix);
if (function & extract_to_stream) {
if (S_ISREG(file_entry->mode)) {
- copy_file_chunk(src_stream, out_stream, file_entry->size);
+ *err = copy_file_chunk(src_stream, out_stream, file_entry->size);
archive_offset += file_entry->size;
}
}
}
} else {
if ((function & extract_quiet) != extract_quiet) {
+ *err = -1;
error_msg("%s not created: newer or same age file exists", file_entry->name);
}
seek_sub_file(src_stream, file_entry->size);
parent = dirname(buf);
if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
if ((function & extract_quiet) != extract_quiet) {
+ *err = -1;
error_msg("couldn't create leading directories");
}
}
if (file_entry->link_name) { /* Found a cpio hard link */
if (link(full_link_name, full_name) != 0) {
if ((function & extract_quiet) != extract_quiet) {
+ *err = -1;
perror_msg("Cannot link from %s to '%s'",
file_entry->name, file_entry->link_name);
}
}
} else {
if ((dst_stream = wfopen(full_name, "w")) == NULL) {
+ *err = -1;
seek_sub_file(src_stream, file_entry->size);
goto cleanup;
}
if (stat_res != 0) {
if (mkdir(full_name, file_entry->mode) < 0) {
if ((function & extract_quiet) != extract_quiet) {
- perror_msg("extract_archive: %s", full_name);
+ *err = -1;
+ perror_msg("%s: %s", __FUNCTION__, full_name);
}
}
}
case S_IFLNK:
if (symlink(file_entry->link_name, full_name) < 0) {
if ((function & extract_quiet) != extract_quiet) {
+ *err = -1;
perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
}
goto cleanup;
case S_IFIFO:
if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
if ((function & extract_quiet) != extract_quiet) {
+ *err = -1;
perror_msg("Cannot create node %s", file_entry->name);
}
goto cleanup;
}
break;
default:
- perror_msg("Don't know how to handle %s", full_name);
+ *err = -1;
+ perror_msg("Don't know how to handle %s", full_name);
}
if ( full_link_name )
free(full_link_name);
- return(buffer); /* Maybe we should say if failed */
+ return buffer;
}
static char *
void (*free_headers)(file_header_t *),
const int extract_function,
const char *prefix,
- const char **extract_names)
+ const char **extract_names,
+ int *err)
{
file_header_t *file_entry;
int extract_flag;
int i;
char *buffer = NULL;
+ *err = 0;
+
archive_offset = 0;
while ((file_entry = get_headers(src_stream)) != NULL) {
extract_flag = TRUE;
- /* fprintf(stderr, __FUNCTION__ " getting headers\n"); */
+
if (extract_names != NULL) {
int found_flag = FALSE;
for(i = 0; extract_names[i] != 0; i++) {
extract_flag = FALSE;
}
}
-
}
if (extract_flag == TRUE) {
- /* fprintf(stderr, __FUNCTION__ " extract?\n"); */
- buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
- /* fprintf(stderr, __FUNCTION__ " extracted\n"); */
+ buffer = extract_archive(src_stream, out_stream,
+ file_entry, extract_function,
+ prefix, err);
+ if (*err) {
+ free_headers(file_entry);
+ break;
+ }
} else {
/* seek past the data entry */
seek_sub_file(src_stream, file_entry->size);
}
free_headers(file_entry);
}
- /*fprintf(stderr, __FUNCTION__ " goin home\n");*/
-
- return(buffer);
+
+ return buffer;
}
static file_header_t *
char *
deb_extract(const char *package_filename, FILE *out_stream,
- const int extract_function, const char *prefix, const char *filename)
+ const int extract_function, const char *prefix,
+ const char *filename, int *err)
{
FILE *deb_stream = NULL;
file_header_t *ar_header = NULL;
char *ared_file = NULL;
char ar_magic[8];
+ *err = 0;
+
if (filename != NULL) {
file_list = xmalloc(sizeof(char *) * 2);
file_list[0] = filename;
ared_file = "data.tar.gz";
} else {
fprintf(stderr, "no file specified to extract -- extract_function=%x\n", extract_function);
+ *err = -1;
goto cleanup;
}
/* open the debian package to be worked on */
deb_stream = wfopen(package_filename, "r");
if (deb_stream == NULL) {
+ *err = -1;
goto cleanup;
}
/* set the buffer size */
/* check ar magic */
fread(ar_magic, 1, 8, deb_stream);
- /*fprintf(stderr, "deb_extract ar_magic=%08x\n", *(long *)ar_magic);*/
+
if (strncmp(ar_magic,"!<arch>",7) == 0) {
archive_offset = 8;
/* open a stream of decompressed data */
uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
if (uncompressed_stream == NULL) {
+ printf("%s: %d\n", __FUNCTION__, __LINE__);
+ *err = -1;
goto cleanup;
}
archive_offset = 0;
- output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, free_header_tar, extract_function, prefix, file_list);
+ output_buffer = unarchive(uncompressed_stream,
+ out_stream, get_header_tar,
+ free_header_tar,
+ extract_function, prefix,
+ file_list, err);
fclose(uncompressed_stream);
gz_close(gunzip_pid);
free_header_ar(ar_header);
fseek(deb_stream, 0, SEEK_SET);
unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
if (unzipped_opkg_stream == NULL) {
+ *err = -1;
goto cleanup;
}
- /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
/* walk through outer tar file to find ared_file */
while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
int name_offset = 0;
/* open a stream of decompressed data */
uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
if (uncompressed_stream == NULL) {
+ *err = -1;
goto cleanup;
}
archive_offset = 0;
- /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
+
output_buffer = unarchive(uncompressed_stream,
out_stream,
get_header_tar,
free_header_tar,
extract_function,
prefix,
- file_list);
- /*fprintf(stderr, __FUNCTION__ ":%d: unarchive complete\n", __LINE__);*/
+ file_list,
+ err);
+
free_header_tar(tar_header);
fclose(uncompressed_stream);
gz_close(gunzip_pid);
}
fclose(unzipped_opkg_stream);
gz_close(unzipped_opkg_pid);
- /*fprintf(stderr, __FUNCTION__ ":%d: done\n", __LINE__);*/
+
goto cleanup;
} else {
- error_msg_and_die("invalid magic");
+ *err = -1;
+ error_msg("%s: invalid magic", package_filename);
}
cleanup:
/* assuage libb functions */
const char *applet_name = "opkg";
-int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
+int
+pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
{
- deb_extract(pkg->local_filename, stream,
- extract_control_tar_gz
- | extract_to_stream,
- NULL, "./control");
+ int err;
+ deb_extract(pkg->local_filename, stream,
+ extract_control_tar_gz
+ | extract_to_stream,
+ NULL, "./control", &err);
- return 0;
+ return err;
}
-int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
+int
+pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, const char *dir,
+ const char *prefix)
{
- return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
-}
-
-int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
- const char *dir,
- const char *prefix)
-{
- char *dir_with_prefix;
- char *buffer = NULL;
+ int err;
+ char *dir_with_prefix;
- sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix);
+ sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix);
- buffer = deb_extract(pkg->local_filename, stderr,
- extract_control_tar_gz
- | extract_all_to_fs| extract_preserve_date
- | extract_unconditional,
- dir_with_prefix, NULL);
+ deb_extract(pkg->local_filename, stderr,
+ extract_control_tar_gz
+ | extract_all_to_fs| extract_preserve_date
+ | extract_unconditional,
+ dir_with_prefix, NULL, &err);
- free(dir_with_prefix);
-
- /* BUG: How do we know if deb_extract worked or not? This is a
- defect in the current deb_extract from what I can tell. */
+ free(dir_with_prefix);
+ return err;
+}
- if (buffer) {
- free(buffer);
- }
- return 0;
+int
+pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
+{
+ return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
}
-int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
+
+int
+pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
{
- char *buffer = NULL;
- buffer = deb_extract(pkg->local_filename, stderr,
+ int err;
+
+ deb_extract(pkg->local_filename, stderr,
extract_data_tar_gz
- | extract_all_to_fs| extract_preserve_date
+ | extract_all_to_fs| extract_preserve_date
| extract_unconditional,
- dir, NULL);
-
- /* BUG: How do we know if deb_extract worked or not? This is a
- defect in the current deb_extract from what I can tell. */
+ dir, NULL, &err);
- if (buffer) {
- free(buffer);
- }
- return 0;
+ return err;
}
-int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
+int
+pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *stream)
{
- char *buffer = NULL;
+ int err;
+
/* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the
data file names with a '.' as the first character. I've taught
opkg how to cope with the presence or absence of the '.', but
If we wanted to, we could workaround the deb_extract behavior
right here, by writing to a tmpfile, then munging things as we
wrote to the actual stream. */
- buffer = deb_extract(pkg->local_filename, file,
- extract_quiet | extract_data_tar_gz | extract_list,
- NULL, NULL);
-
- /* BUG: How do we know if deb_extract worked or not? This is a
- defect in the current deb_extract from what I can tell. */
- if (buffer) {
- free(buffer);
- }
- return 0;
+
+ deb_extract(pkg->local_filename, stream,
+ extract_quiet | extract_data_tar_gz | extract_list,
+ NULL, NULL, &err);
+
+ return err;
}