void sig_cleanup(int which_sig) { char errbuf[80]; /* * restore modes and times for any dirs we may have created * or any dirs we may have read. Set vflag and vfpart so the user * will clearly see the message on a line by itself. */ vflag = vfpart = 1; /* paxwarn() uses stdio; fake it as well as we can */ if (which_sig == SIGXCPU) strlcpy(errbuf, "CPU time limit reached, cleaning up.\n", sizeof errbuf); else strlcpy(errbuf, "Signal caught, cleaning up.\n", sizeof errbuf); (void) write(STDERR_FILENO, errbuf, strlen(errbuf)); ar_close(); /* XXX signal race */ proc_dir(); /* XXX signal race */ if (tflag) atdir_end(); /* XXX signal race */ _exit(1); }
__dead static void sig_cleanup(int which_sig) { /* * restore modes and times for any dirs we may have created * or any dirs we may have read. Set vflag and vfpart so the user * will clearly see the message on a line by itself. */ vflag = vfpart = 1; #ifdef SIGXCPU if (which_sig == SIGXCPU) tty_warn(1, "CPU time limit reached, cleaning up."); else #endif tty_warn(1, "Signal caught, cleaning up."); /* delete any open temporary file */ if (xtmp_name) (void)unlink(xtmp_name); ar_close(); proc_dir(); if (tflag) atdir_end(); (void)raise_default_signal(which_sig); exit(1); }
void sig_cleanup(int which_sig) { /* * Restore modes and times for any dirs we may have created * or any dirs we may have read. Set vflag and vfpart so the * user will clearly see the message on a line by itself. */ vflag = vfpart = 1; /* paxwarn() uses stdio; fake it as well as we can */ if (which_sig == SIGXCPU) dprintf(STDERR_FILENO, "\nCPU time limit reached, cleaning up.\n"); else if (!which_sig) dprintf(STDERR_FILENO, "\nCowardly giving up, trying to clean up.\n"); else dprintf(STDERR_FILENO, "\nSignal caught, cleaning up.\n"); ar_close(1); sltab_process(1); proc_dir(1); if (tflag) atdir_end(); _exit(1); }
void sig_cleanup(int which_sig) { char errbuf[80]; /* * restore modes and times for any dirs we may have created * or any dirs we may have read. */ /* paxwarn() uses stdio; fake it as well as we can */ if (which_sig == SIGXCPU) strlcpy(errbuf, "\nCPU time limit reached, cleaning up.\n", sizeof errbuf); else strlcpy(errbuf, "\nSignal caught, cleaning up.\n", sizeof errbuf); (void) write(STDERR_FILENO, errbuf, strlen(errbuf)); ar_close(1); sltab_process(1); proc_dir(1); if (tflag) atdir_end(); _exit(1); }
bool MemDumper::dump(QFile *_file) const { #ifdef Q_WS_WIN return false; #endif Q_CHECK_PTR(_file); Q_ASSERT(_file -> isOpen()); QRegExp mem_addr_regex("^([0-9a-fA-F]+)-([0-9a-fA-F]+)\\sr"); QDir system_proc_dir("/proc"); QDir proc_dir(system_proc_dir.absoluteFilePath(QString::number(m_pid))); QString maps_file_str(proc_dir.absoluteFilePath("maps")); QString mem_file_str(proc_dir.absoluteFilePath("mem")); QFile maps_file(maps_file_str); QFile mem_file(mem_file_str); if(!maps_file.open(QIODevice::ReadOnly) || !mem_file.open(QIODevice::ReadOnly)) { qCritical("ERROR: Open file error: %s", maps_file.errorString().toLocal8Bit().data()); return false; } QTextStream maps_str(&maps_file); for(QString line = maps_str.readLine(); !line.isNull(); line = maps_str.readLine()) { qint32 pos = mem_addr_regex.indexIn(line); if(!pos) { qint64 start = mem_addr_regex.capturedTexts()[1].toLongLong(NULL, 16); qint64 end = mem_addr_regex.capturedTexts()[2].toLongLong(NULL, 16); qint64 size = end - start; for(qint64 read = 0; read < size;) { start += read; mem_file.seek(start); QByteArray buf; buf = mem_file.read(size - read); if(buf.size() < size - read && mem_file.error() != QFile::NoError) { qCritical("ERROR: Open file error: %s", mem_file.errorString().toLocal8Bit().data()); return false; } _file -> write(buf); read += buf.size(); } } } return true; }
void sig_cleanup(int which_sig) { /* * restore modes and times for any dirs we may have created * or any dirs we may have read. Set vflag and vfpart so the user * will clearly see the message on a line by itself. */ vflag = vfpart = 1; if (which_sig == SIGXCPU) paxwarn(0, "Cpu time limit reached, cleaning up."); else paxwarn(0, "Signal caught, cleaning up."); ar_close(); proc_dir(); if (tflag) atdir_end(); exit(1); }
void sig_cleanup(int which_sig) { /* * restore modes and times for any dirs we may have created * or any dirs we may have read. */ /* paxwarn() uses stdio; fake it as well as we can */ if (which_sig == SIGXCPU) dprintf(STDERR_FILENO, "\nCPU time limit reached, cleaning up.\n"); else dprintf(STDERR_FILENO, "\nSignal caught, cleaning up.\n"); ar_close(1); sltab_process(1); proc_dir(1); if (tflag) atdir_end(); _exit(1); }
/** * Analyze the volume starting at byte offset 'start' and look * for a file system. When found, the files will be analyzed. * * @param img Disk image to be analyzed. * @param start Byte offset of volume starting location. * * @return 1 on error and 0 on success */ static uint8_t proc_fs(TSK_IMG_INFO * img_info, TSK_OFF_T start) { TSK_FS_INFO *fs_info; TSK_STACK *stack; /* Try it as a file system */ if ((fs_info = tsk_fs_open_img(img_info, start, TSK_FS_TYPE_DETECT)) == NULL) { fprintf(stderr, "Error opening file system in partition at offset %" PRIuOFF "\n", start); tsk_error_print(stderr); /* We could do some carving on the volume data at this point */ return 1; } // create a stack to prevent infinite loops stack = tsk_stack_create(); // Process the directories if (proc_dir(fs_info, stack, fs_info->root_inum, "")) { fprintf(stderr, "Error processing file system in partition at offset %" PRIuOFF "\n", start); tsk_fs_close(fs_info); return 1; } tsk_stack_free(stack); /* We could do some analysis of unallocated blocks at this point... */ tsk_fs_close(fs_info); return 0; }
static void sig_cleanup(int which_sig) { /* * The definition of this array doubles as compile-time assert * on the size of long, off_t, and whether LONG_OFF_T is used, * or not, correctly; target size is 80, error size -1. */ char errbuf[((sizeof(long) >= 4) && (sizeof(ot_type) >= 4) && (sizeof(ot_type) == sizeof(off_t))) ? 80 : -1]; /* * restore modes and times for any dirs we may have created * or any dirs we may have read. Set vflag and vfpart so the user * will clearly see the message on a line by itself. */ vflag = vfpart = 1; /* paxwarn() uses stdio; fake it as well as we can */ if (which_sig == SIGXCPU) strlcpy(errbuf, "CPU time limit reached, cleaning up.\n", sizeof errbuf); else strlcpy(errbuf, "Signal caught, cleaning up.\n", sizeof errbuf); if (!write(STDERR_FILENO, errbuf, strlen(errbuf))) { /* dummy, to keep fortified gcc quiet */ errbuf[0] = '\0'; } ar_close(); /* XXX signal race */ proc_dir(); /* XXX signal race */ if (tflag) atdir_end(); /* XXX signal race */ _exit(1); }
void extract() #endif { register ARCHD *arcn; register int res; off_t cnt; ARCHD archd; struct stat sb; int fd; arcn = &archd; /* * figure out archive type; pass any format specific options to the * archive option processing routine; call the format init routine; * start up the directory modification time and access mode database */ if ((get_arc() < 0) || ((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0) || (dir_start() < 0)) return; /* * When we are doing interactive rename, we store the mapping of names * so we can fix up hard links files later in the archive. */ if (iflag && (name_start() < 0)) return; /* * step through each entry on the archive until the format read routine * says it is done */ while (next_head(arcn) == 0) { /* * check for pattern, and user specified options match. When * all the patterns are matched we are done */ if ((res = pat_match(arcn)) < 0) break; if ((res > 0) || (sel_chk(arcn) != 0)) { /* * file is not selected. skip past any file data and * padding and go back for the next archive member */ (void)rd_skip(arcn->skip + arcn->pad); continue; } /* * with -u or -D only extract when the archive member is newer * than the file with the same name in the file system (nos * test of being the same type is required). * NOTE: this test is done BEFORE name modifications as * specified by pax. this operation can be confusing to the * user who might expect the test to be done on an existing * file AFTER the name mod. In honesty the pax spec is probably * flawed in this respect. */ if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) { if (uflag && Dflag) { if ((arcn->sb.st_mtime <= sb.st_mtime) && (arcn->sb.st_ctime <= sb.st_ctime)) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (Dflag) { if (arcn->sb.st_ctime <= sb.st_ctime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (arcn->sb.st_mtime <= sb.st_mtime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } /* * this archive member is now been selected. modify the name. */ if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0)) break; if (res > 0) { /* * a bad name mod, skip and purge name from link table */ purg_lnk(arcn); (void)rd_skip(arcn->skip + arcn->pad); continue; } /* * Non standard -Y and -Z flag. When the exisiting file is * same age or newer skip */ if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) { if (Yflag && Zflag) { if ((arcn->sb.st_mtime <= sb.st_mtime) && (arcn->sb.st_ctime <= sb.st_ctime)) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (Yflag) { if (arcn->sb.st_ctime <= sb.st_ctime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (arcn->sb.st_mtime <= sb.st_mtime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } if (vflag) { (void)fputs(arcn->name, stderr); vfpart = 1; } /* * all ok, extract this member based on type */ if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) { /* * process archive members that are not regular files. * throw out padding and any data that might follow the * header (as determined by the format). */ if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) res = lnk_creat(arcn); else res = node_creat(arcn); (void)rd_skip(arcn->skip + arcn->pad); if (res < 0) purg_lnk(arcn); if (vflag && vfpart) { (void)putc('\n', stderr); vfpart = 0; } continue; } /* * we have a file with data here. If we can not create it, skip * over the data and purge the name from hard link table */ if ((fd = file_creat(arcn)) < 0) { (void)rd_skip(arcn->skip + arcn->pad); purg_lnk(arcn); continue; } /* * extract the file from the archive and skip over padding and * any unprocessed data */ res = (*frmt->rd_data)(arcn, fd, &cnt); file_close(arcn, fd); if (vflag && vfpart) { (void)putc('\n', stderr); vfpart = 0; } if (!res) (void)rd_skip(cnt + arcn->pad); } /* * all done, restore directory modes and times as required; make sure * all patterns supplied by the user were matched; block off signals * to avoid chance for multiple entry into the cleanup code. */ (void)(*frmt->end_rd)(); (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); ar_close(); proc_dir(); pat_chk(); }
/** * Open a directory and cycle through its contents. Read each file and recurse * into each directory. * * @param fs_info File system to process * @param stack Stack to prevent infinite recursion loops * @param dir_inum Metadata address of directory to open * @param path Path of directory being opened * @returns 1 on error */ static uint8_t proc_dir(TSK_FS_INFO * fs_info, TSK_STACK * stack, TSK_INUM_T dir_inum, const char *path) { TSK_FS_DIR *fs_dir; size_t i; char *path2 = NULL; char *buf = NULL; // open the directory if ((fs_dir = tsk_fs_dir_open_meta(fs_info, dir_inum)) == NULL) { fprintf(stderr, "Error opening directory: %" PRIuINUM "\n", dir_inum); tsk_error_print(stderr); return 1; } /* These should be dynamic lengths, but this is just a sample program. * Allocate heap space instead of stack to prevent overflow for deep * directories. */ if ((path2 = (char *) malloc(4096)) == NULL) { return 1; } if ((buf = (char *) malloc(2048)) == NULL) { free(path2); return 1; } // cycle through each entry for (i = 0; i < tsk_fs_dir_getsize(fs_dir); i++) { TSK_FS_FILE *fs_file; TSK_OFF_T off = 0; size_t len = 0; // get the entry if ((fs_file = tsk_fs_dir_get(fs_dir, i)) == NULL) { fprintf(stderr, "Error getting directory entry %" PRIuSIZE " in directory %" PRIuINUM "\n", i, dir_inum); tsk_error_print(stderr); free(path2); free(buf); return 1; } /* Ignore NTFS System files */ if ((TSK_FS_TYPE_ISNTFS(fs_file->fs_info->ftype)) && (fs_file->name->name[0] == '$')) { tsk_fs_file_close(fs_file); continue; } //printf("Processing %s/%s\n", path, fs_file->name->name); // make sure it's got metadata and not only a name if (fs_file->meta) { ssize_t cnt; /* Note that we could also cycle through all of the attributes in the * file by using one of the tsk_fs_attr_get() functions and reading it * with tsk_fs_attr_read(). See the File Systems section of the Library * User's Guide for more details: * http://www.sleuthkit.org/sleuthkit/docs/api-docs/ */ // read file contents if (fs_file->meta->type == TSK_FS_META_TYPE_REG) { int myflags = 0; for (off = 0; off < fs_file->meta->size; off += len) { if (fs_file->meta->size - off < 2048) len = (size_t) (fs_file->meta->size - off); else len = 2048; cnt = tsk_fs_file_read(fs_file, off, buf, len, (TSK_FS_FILE_READ_FLAG_ENUM) myflags); if (cnt == -1) { // could check tsk_errno here for a recovery error (TSK_ERR_FS_RECOVER) fprintf(stderr, "Error reading %s file: %s\n", ((fs_file->name-> flags & TSK_FS_NAME_FLAG_UNALLOC) || (fs_file->meta-> flags & TSK_FS_META_FLAG_UNALLOC)) ? "unallocated" : "allocated", fs_file->name->name); tsk_error_print(stderr); break; } else if (cnt != (ssize_t) len) { fprintf(stderr, "Warning: %" PRIuSIZE " of %" PRIuSIZE " bytes read from %s file %s\n", cnt, len, ((fs_file->name-> flags & TSK_FS_NAME_FLAG_UNALLOC) || (fs_file->meta-> flags & TSK_FS_META_FLAG_UNALLOC)) ? "unallocated" : "allocated", fs_file->name->name); } // do something with the data... } } // recurse into another directory (unless it is a '.' or '..') else if (fs_file->meta->type == TSK_FS_META_TYPE_DIR) { if (TSK_FS_ISDOT(fs_file->name->name) == 0) { // only go in if it is not on our stack if (tsk_stack_find(stack, fs_file->meta->addr) == 0) { // add the address to the top of the stack tsk_stack_push(stack, fs_file->meta->addr); snprintf(path2, 4096, "%s/%s", path, fs_file->name->name); if (proc_dir(fs_info, stack, fs_file->meta->addr, path2)) { tsk_fs_file_close(fs_file); tsk_fs_dir_close(fs_dir); free(path2); free(buf); return 1; } // pop the address tsk_stack_pop(stack); } } } } tsk_fs_file_close(fs_file); } tsk_fs_dir_close(fs_dir); free(path2); free(buf); return 0; }
int copy(void) { ARCHD *arcn; int res; int fddest; char *dest_pt; int dlen; int drem; int fdsrc = -1; struct stat sb; char dirbuf[PAXPATHLEN+1]; arcn = &archd; /* * set up the destination dir path and make sure it is a directory. We * make sure we have a trailing / on the destination */ dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf)); if (dlen >= sizeof(dirbuf) || (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) { tty_warn(1, "directory name is too long %s", dirptr); return 1; } dest_pt = dirbuf + dlen; if (*(dest_pt-1) != '/') { *dest_pt++ = '/'; ++dlen; } *dest_pt = '\0'; drem = PAXPATHLEN - dlen; if (stat(dirptr, &sb) < 0) { syswarn(1, errno, "Cannot access destination directory %s", dirptr); return 1; } if (!S_ISDIR(sb.st_mode)) { tty_warn(1, "Destination is not a directory %s", dirptr); return 1; } /* * start up the hard link table; file traversal routines and the * modification time and access mode database */ if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0)) return 1; /* * When we are doing interactive rename, we store the mapping of names * so we can fix up hard links files later in the archive. */ if (iflag && (name_start() < 0)) return 1; /* * set up to cp file trees */ cp_start(); /* * while there are files to archive, process them */ while (next_file(arcn) == 0) { fdsrc = -1; /* * check if this file meets user specified options */ if (sel_chk(arcn) != 0) continue; /* * if there is already a file in the destination directory with * the same name and it is newer, skip the one stored on the * archive. * NOTE: this test is done BEFORE name modifications as * specified by pax. this can be confusing to the user who * might expect the test to be done on an existing file AFTER * the name mod. In honesty the pax spec is probably flawed in * this respect */ if (uflag || Dflag) { /* * create the destination name */ if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'), drem + 1) > drem) { tty_warn(1, "Destination pathname too long %s", arcn->name); continue; } /* * if existing file is same age or newer skip */ res = lstat(dirbuf, &sb); *dest_pt = '\0'; if (res == 0) { if (uflag && Dflag) { if ((arcn->sb.st_mtime<=sb.st_mtime) && (arcn->sb.st_ctime<=sb.st_ctime)) continue; } else if (Dflag) { if (arcn->sb.st_ctime <= sb.st_ctime) continue; } else if (arcn->sb.st_mtime <= sb.st_mtime) continue; } } /* * this file is considered selected. See if this is a hard link * to a previous file; modify the name as requested by the * user; set the final destination. */ ftree_sel(arcn); if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn, RENM)) < 0)) break; if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) { /* * skip file, purge from link table */ purg_lnk(arcn); continue; } /* * Non standard -Y and -Z flag. When the exisiting file is * same age or newer skip */ if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) { if (Yflag && Zflag) { if ((arcn->sb.st_mtime <= sb.st_mtime) && (arcn->sb.st_ctime <= sb.st_ctime)) continue; } else if (Yflag) { if (arcn->sb.st_ctime <= sb.st_ctime) continue; } else if (arcn->sb.st_mtime <= sb.st_mtime) continue; } if (vflag) { (void)safe_print(arcn->name, listf); vfpart = 1; } ++flcnt; /* * try to create a hard link to the src file if requested * but make sure we are not trying to overwrite ourselves. */ if (lflag) res = cross_lnk(arcn); else res = chk_same(arcn); if (res <= 0) { if (vflag && vfpart) { (void)putc('\n', listf); vfpart = 0; } continue; } /* * have to create a new file */ if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) { /* * create a link or special file */ if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) { int payload; res = lnk_creat(arcn, &payload); } else { res = node_creat(arcn); } if (res < 0) purg_lnk(arcn); if (vflag && vfpart) { (void)putc('\n', listf); vfpart = 0; } continue; } /* * have to copy a regular file to the destination directory. * first open source file and then create the destination file */ if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) { syswarn(1, errno, "Unable to open %s to read", arcn->org_name); purg_lnk(arcn); continue; } if ((fddest = file_creat(arcn, 0)) < 0) { rdfile_close(arcn, &fdsrc); purg_lnk(arcn); continue; } /* * copy source file data to the destination file */ cp_file(arcn, fdsrc, fddest); file_close(arcn, fddest); rdfile_close(arcn, &fdsrc); if (vflag && vfpart) { (void)putc('\n', listf); vfpart = 0; } } /* * restore directory modes and times as required; make sure all * patterns were selected block off signals to avoid chance for * multiple entry into the cleanup code. */ (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); ar_close(); proc_dir(); ftree_chk(); return 0; }
static int wr_archive(ARCHD *arcn, int is_app) { int res; int hlk; int wr_one; off_t cnt; int (*wrf)(ARCHD *); int fd = -1; time_t now; /* * if this format supports hard link storage, start up the database * that detects them. */ if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0)) return 1; /* * start up the file traversal code and format specific write */ if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0)) return 1; wrf = frmt->wr; now = time((time_t *)NULL); /* * When we are doing interactive rename, we store the mapping of names * so we can fix up hard links files later in the archive. */ if (iflag && (name_start() < 0)) return 1; /* * if this is not append, and there are no files, we do no write a trailer */ wr_one = is_app; /* * while there are files to archive, process them one at at time */ while (next_file(arcn) == 0) { /* * check if this file meets user specified options match. */ if (sel_chk(arcn) != 0) continue; /* * Here we handle the exclusion -X gnu style patterns which * are implemented like a pattern list. We don't modify the * name as this will be done below again, and we don't want * to double modify it. */ if ((res = mod_name(arcn, 0)) < 0) break; if (res == 1) continue; fd = -1; if (uflag) { /* * only archive if this file is newer than a file with * the same name that is already stored on the archive */ if ((res = chk_ftime(arcn)) < 0) break; if (res > 0) continue; } /* * this file is considered selected now. see if this is a hard * link to a file already stored */ ftree_sel(arcn); if (hlk && (chk_lnk(arcn) < 0)) break; if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) || (arcn->type == PAX_CTG)) { /* * we will have to read this file. by opening it now we * can avoid writing a header to the archive for a file * we were later unable to read (we also purge it from * the link table). */ if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) { syswarn(1, errno, "Unable to open %s to read", arcn->org_name); purg_lnk(arcn); continue; } } /* * Now modify the name as requested by the user */ if ((res = mod_name(arcn, RENM)) < 0) { /* * name modification says to skip this file, close the * file and purge link table entry */ rdfile_close(arcn, &fd); purg_lnk(arcn); break; } if (arcn->name[0] == '/' && !check_Aflag()) { memmove(arcn->name, arcn->name + 1, strlen(arcn->name)); } if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) { /* * unable to obtain the crc we need, close the file, * purge link table entry */ rdfile_close(arcn, &fd); purg_lnk(arcn); continue; } if (vflag) { if (vflag > 1) ls_list(arcn, now, listf); else { (void)safe_print(arcn->name, listf); vfpart = 1; } } ++flcnt; /* * looks safe to store the file, have the format specific * routine write routine store the file header on the archive */ if ((res = (*wrf)(arcn)) < 0) { rdfile_close(arcn, &fd); break; } wr_one = 1; if (res > 0) { /* * format write says no file data needs to be stored * so we are done messing with this file */ if (vflag && vfpart) { (void)putc('\n', listf); vfpart = 0; } rdfile_close(arcn, &fd); continue; } /* * Add file data to the archive, quit on write error. if we * cannot write the entire file contents to the archive we * must pad the archive to replace the missing file data * (otherwise during an extract the file header for the file * which FOLLOWS this one will not be where we expect it to * be). */ res = (*frmt->wr_data)(arcn, fd, &cnt); rdfile_close(arcn, &fd); if (vflag && vfpart) { (void)putc('\n', listf); vfpart = 0; } if (res < 0) break; /* * pad as required, cnt is number of bytes not written */ if (((cnt > 0) && (wr_skip(cnt) < 0)) || ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0))) break; } /* * tell format to write trailer; pad to block boundary; reset directory * mode/access times, and check if all patterns supplied by the user * were matched. block off signals to avoid chance for multiple entry * into the cleanup code */ if (wr_one) { (*frmt->end_wr)(); wr_fin(); } (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); ar_close(); if (tflag) proc_dir(); ftree_chk(); return 0; }
int extract(void) { ARCHD *arcn; int res; off_t cnt; struct stat sb; int fd; time_t now; arcn = &archd; /* * figure out archive type; pass any format specific options to the * archive option processing routine; call the format init routine; * start up the directory modification time and access mode database */ if ((get_arc() < 0) || ((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0) || (dir_start() < 0)) return 1; now = time((time_t *)NULL); #if !HAVE_NBTOOL_CONFIG_H if (do_chroot) (void)fdochroot(cwdfd); #endif /* * When we are doing interactive rename, we store the mapping of names * so we can fix up hard links files later in the archive. */ if (iflag && (name_start() < 0)) return 1; /* * step through each entry on the archive until the format read routine * says it is done */ while (next_head(arcn) == 0) { int write_to_hard_link = 0; if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) { /* * we need to read, to get the real filename */ if (!(*frmt->rd_data)(arcn, -arcn->type, &cnt)) (void)rd_skip(cnt + arcn->pad); continue; } /* * check for pattern, and user specified options match. When * all the patterns are matched we are done */ if ((res = pat_match(arcn)) < 0) break; if ((res > 0) || (sel_chk(arcn) != 0)) { /* * file is not selected. skip past any file * data and padding and go back for the next * archive member */ (void)rd_skip(arcn->skip + arcn->pad); continue; } if (kflag && (lstat(arcn->name, &sb) == 0)) { (void)rd_skip(arcn->skip + arcn->pad); continue; } /* * with -u or -D only extract when the archive member is newer * than the file with the same name in the file system (no * test of being the same type is required). * NOTE: this test is done BEFORE name modifications as * specified by pax. this operation can be confusing to the * user who might expect the test to be done on an existing * file AFTER the name mod. In honesty the pax spec is probably * flawed in this respect. ignore this for GNU long links. */ if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) { if (uflag && Dflag) { if ((arcn->sb.st_mtime <= sb.st_mtime) && (arcn->sb.st_ctime <= sb.st_ctime)) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (Dflag) { if (arcn->sb.st_ctime <= sb.st_ctime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (arcn->sb.st_mtime <= sb.st_mtime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } /* * this archive member is now been selected. modify the name. */ if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn, RENM)) < 0)) break; if (res > 0) { /* * a bad name mod, skip and purge name from link table */ purg_lnk(arcn); (void)rd_skip(arcn->skip + arcn->pad); continue; } if (arcn->name[0] == '/' && !check_Aflag()) { memmove(arcn->name, arcn->name + 1, strlen(arcn->name)); } /* * Non standard -Y and -Z flag. When the existing file is * same age or newer skip; ignore this for GNU long links. */ if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) { if (Yflag && Zflag) { if ((arcn->sb.st_mtime <= sb.st_mtime) && (arcn->sb.st_ctime <= sb.st_ctime)) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (Yflag) { if (arcn->sb.st_ctime <= sb.st_ctime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } else if (arcn->sb.st_mtime <= sb.st_mtime) { (void)rd_skip(arcn->skip + arcn->pad); continue; } } if (vflag) { if (vflag > 1) ls_list(arcn, now, listf); else { (void)safe_print(arcn->name, listf); vfpart = 1; } } /* * if required, chdir around. */ if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL) && !to_stdout) dochdir(arcn->pat->chdname); if (secure && path_check(arcn, 0) != 0) { (void)rd_skip(arcn->skip + arcn->pad); continue; } /* * all ok, extract this member based on type */ if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) { /* * process archive members that are not regular files. * throw out padding and any data that might follow the * header (as determined by the format). */ if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) res = lnk_creat(arcn, &write_to_hard_link); else res = node_creat(arcn); if (!write_to_hard_link) { (void)rd_skip(arcn->skip + arcn->pad); if (res < 0) purg_lnk(arcn); if (vflag && vfpart) { (void)putc('\n', listf); vfpart = 0; } continue; } } if (to_stdout) fd = STDOUT_FILENO; else { /* * We have a file with data here. If we cannot create * it, skip over the data and purge the name from hard * link table. */ if ((fd = file_creat(arcn, write_to_hard_link)) < 0) { (void)fflush(listf); (void)rd_skip(arcn->skip + arcn->pad); purg_lnk(arcn); continue; } } /* * extract the file from the archive and skip over padding and * any unprocessed data */ res = (*frmt->rd_data)(arcn, fd, &cnt); if (!to_stdout) file_close(arcn, fd); if (vflag && vfpart) { (void)putc('\n', listf); vfpart = 0; } if (!res) (void)rd_skip(cnt + arcn->pad); /* * if required, chdir around. */ if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL)) fdochdir(cwdfd); } /* * all done, restore directory modes and times as required; make sure * all patterns supplied by the user were matched; block off signals * to avoid chance for multiple entry into the cleanup code. */ (void)(*frmt->end_rd)(); (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); ar_close(); proc_dir(); pat_chk(); return 0; }