/* Extract the data postioned at src_stream to either filesystem, stdout or * buffer depending on the value of 'function' which is defined in bbtargz.h * * prefix doesnt have to be just a directory, it may prefix the filename as well. * * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have * 'dpkg.' as their prefix * * For this reason if prefix does point to a dir then it must end with a * trailing '/' or else the last dir will be assumed to be the file prefix */ static char * extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry, const int function, const char *prefix, int *err) { FILE *dst_stream = NULL; char *full_name = NULL; char *full_link_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 */ if (prefix != NULL) { /* strip leading '/' in filename to extract as prefix may not be dir */ /* Cant use concat_path_file here as prefix might not be a directory */ char *path = file_entry->name; if (strncmp("./", path, 2) == 0) { path += 2; if (strlen(path) == 0) /* Do nothing, current dir already exists. */ return NULL; } full_name = xmalloc(strlen(prefix) + strlen(path) + 1); strcpy(full_name, prefix); strcat(full_name, path); if ( file_entry->link_name ){ full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1); strcpy(full_link_name, prefix); strcat(full_link_name, file_entry->link_name); } } else { full_name = xstrdup(file_entry->name); if ( file_entry->link_name ) full_link_name = xstrdup(file_entry->link_name); } if (function & extract_to_stream) { if (S_ISREG(file_entry->mode)) { *err = copy_file_chunk(src_stream, out_stream, file_entry->size); archive_offset += file_entry->size; } } else if (function & extract_one_to_buffer) { if (S_ISREG(file_entry->mode)) { buffer = (char *) xmalloc(file_entry->size + 1); fread(buffer, 1, file_entry->size, src_stream); buffer[file_entry->size] = '\0'; archive_offset += file_entry->size; goto cleanup; } } else if (function & extract_all_to_fs) { struct stat oldfile; int stat_res; stat_res = lstat (full_name, &oldfile); if (stat_res == 0) { /* The file already exists */ if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) { if (!S_ISDIR(oldfile.st_mode)) { unlink(full_name); /* Directories might not be empty etc */ } } 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); goto cleanup; } } if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */ char *buf, *parent; buf = xstrdup(full_name); 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"); } } free (buf); } switch(file_entry->mode & S_IFMT) { case S_IFREG: 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; } archive_offset += file_entry->size; *err = copy_file_chunk(src_stream, dst_stream, file_entry->size); fclose(dst_stream); } break; case S_IFDIR: if (stat_res != 0) { if (mkdir(full_name, file_entry->mode) < 0) { if ((function & extract_quiet) != extract_quiet) { *err = -1; perror_msg("Cannot make dir %s", full_name); } } } break; 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; } break; case S_IFSOCK: case S_IFBLK: case S_IFCHR: 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: *err = -1; perror_msg("Don't know how to handle %s", full_name); } /* Changing a symlink's properties normally changes the properties of the * file pointed to, so dont try and change the date or mode, lchown does * does the right thing, but isnt available in older versions of libc */ if (S_ISLNK(file_entry->mode)) { #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1) lchown(full_name, file_entry->uid, file_entry->gid); #endif } else { if (function & extract_preserve_date) { t.actime = file_entry->mtime; t.modtime = file_entry->mtime; utime(full_name, &t); } chown(full_name, file_entry->uid, file_entry->gid); chmod(full_name, file_entry->mode); } } else { /* If we arent extracting data we have to skip it, * if data size is 0 then then just do it anyway * (saves testing for it) */ seek_sub_file(src_stream, file_entry->size); } /* extract_list and extract_verbose_list can be used in conjunction * with one of the above four extraction functions, so do this seperately */ if (function & extract_verbose_list) { fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), file_entry->uid, file_entry->gid, (int) file_entry->size, time_string(file_entry->mtime)); } if ((function & extract_list) || (function & extract_verbose_list)){ /* fputs doesnt add a trailing \n, so use fprintf */ fprintf(out_stream, "%s\n", file_entry->name); } cleanup: free(full_name); if ( full_link_name ) free(full_link_name); return buffer; }
int copy_file(const char *source, const char *dest, int flags) { struct stat source_stat; struct stat dest_stat; int dest_exists = 0; int status = 0; if ((!(flags & FILEUTILS_PRESERVE_SYMLINKS) && lstat(source, &source_stat) < 0) || ((flags & FILEUTILS_PRESERVE_SYMLINKS) && stat(source, &source_stat) < 0)) { perror_msg("%s", source); return -1; } if (lstat(dest, &dest_stat) < 0) { if (errno != ENOENT) { perror_msg("unable to stat `%s'", dest); return -1; } } else { if (source_stat.st_dev == dest_stat.st_dev && source_stat.st_ino == dest_stat.st_ino) { error_msg("`%s' and `%s' are the same file", source, dest); return -1; } dest_exists = 1; } if (S_ISDIR(source_stat.st_mode)) { DIR *dp; struct dirent *d; mode_t saved_umask = 0; if (!(flags & FILEUTILS_RECUR)) { error_msg("%s: omitting directory", source); return -1; } /* Create DEST. */ if (dest_exists) { if (!S_ISDIR(dest_stat.st_mode)) { error_msg("`%s' is not a directory", dest); return -1; } } else { mode_t mode; saved_umask = umask(0); mode = source_stat.st_mode; if (!(flags & FILEUTILS_PRESERVE_STATUS)) mode = source_stat.st_mode & ~saved_umask; mode |= S_IRWXU; if (mkdir(dest, mode) < 0) { umask(saved_umask); perror_msg("cannot create directory `%s'", dest); return -1; } umask(saved_umask); } /* Recursively copy files in SOURCE. */ if ((dp = opendir(source)) == NULL) { perror_msg("unable to open directory `%s'", source); status = -1; goto end; } while ((d = readdir(dp)) != NULL) { char *new_source, *new_dest; if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) continue; new_source = concat_path_file(source, d->d_name); new_dest = concat_path_file(dest, d->d_name); if (copy_file(new_source, new_dest, flags) < 0) status = -1; free(new_source); free(new_dest); } /* closedir have only EBADF error, but "dp" not changes */ closedir(dp); if (!dest_exists && chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { perror_msg("unable to change permissions of `%s'", dest); status = -1; } } else if (S_ISREG(source_stat.st_mode)) { FILE *sfp, *dfp=NULL; #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS char *link_name; if (!(flags & FILEUTILS_PRESERVE_SYMLINKS) && is_in_ino_dev_hashtable(&source_stat, &link_name)) { if (link(link_name, dest) < 0) { perror_msg("unable to link `%s'", dest); return -1; } return 0; } #endif if ((sfp = wfopen(source, "r")) == NULL) { return -1; } if (dest_exists) { if (flags & FILEUTILS_INTERACTIVE) { fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); if (!ask_confirmation()) { fclose (sfp); return 0; } } if ((dfp = fopen(dest, "w")) == NULL) { if (!(flags & FILEUTILS_FORCE)) { perror_msg("unable to open `%s'", dest); fclose (sfp); return -1; } if (unlink(dest) < 0) { perror_msg("unable to remove `%s'", dest); fclose (sfp); return -1; } dest_exists = 0; } } if (!dest_exists) { int fd; if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || (dfp = fdopen(fd, "w")) == NULL) { if (fd >= 0) close(fd); perror_msg("unable to open `%s'", dest); fclose (sfp); return -1; } } if (copy_file_chunk(sfp, dfp, -1) < 0) status = -1; if (fclose(dfp) < 0) { perror_msg("unable to close `%s'", dest); status = -1; } if (fclose(sfp) < 0) { perror_msg("unable to close `%s'", source); status = -1; } } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) || S_ISLNK(source_stat.st_mode)) { if (dest_exists && ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) { perror_msg("unable to remove `%s'", dest); return -1; } } else { error_msg("internal error: unrecognized file type"); return -1; } if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || S_ISSOCK(source_stat.st_mode)) { if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { perror_msg("unable to create `%s'", dest); return -1; } } else if (S_ISFIFO(source_stat.st_mode)) { if (mkfifo(dest, source_stat.st_mode) < 0) { perror_msg("cannot create fifo `%s'", dest); return -1; } } else if (S_ISLNK(source_stat.st_mode)) { char *lpath; lpath = xreadlink(source); if (symlink(lpath, dest) < 0) { perror_msg("cannot create symlink `%s'", dest); return -1; } free(lpath); #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) if (flags & FILEUTILS_PRESERVE_STATUS) if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) perror_msg("unable to preserve ownership of `%s'", dest); #endif #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS add_to_ino_dev_hashtable(&source_stat, dest); #endif return 0; } #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS add_to_ino_dev_hashtable(&source_stat, dest); #endif end: if (flags & FILEUTILS_PRESERVE_STATUS) { struct utimbuf times; times.actime = source_stat.st_atime; times.modtime = source_stat.st_mtime; if (utime(dest, ×) < 0) perror_msg("unable to preserve times of `%s'", dest); if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { source_stat.st_mode &= ~(S_ISUID | S_ISGID); perror_msg("unable to preserve ownership of `%s'", dest); } if (chmod(dest, source_stat.st_mode) < 0) perror_msg("unable to preserve permissions of `%s'", dest); } return status; }