static void test_exclusion_and_inclusion(void) { struct archive_entry *ae; struct archive *m; const char *mp; const wchar_t *wp; if (!assert((m = archive_match_new()) != NULL)) return; if (!assert((ae = archive_entry_new()) != NULL)) { archive_match_free(m); return; } assertEqualIntA(m, 0, archive_match_exclude_pattern(m, "^aaa*")); assertEqualIntA(m, 0, archive_match_include_pattern_w(m, L"^aa*")); assertEqualIntA(m, 0, archive_match_include_pattern(m, "^a1*")); /* Test with 'aa1234', which should not be excluded. */ archive_entry_copy_pathname(ae, "aa1234"); failure("'aa1234' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"aa1234"); failure("'aa1234' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); /* Test with 'aaa1234', which should be excluded. */ archive_entry_copy_pathname(ae, "aaa1234"); failure("'aaa1234' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"aaa1234"); failure("'aaa1234' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); /* Verify unmatched_inclusions. */ assertEqualInt(1, archive_match_path_unmatched_inclusions(m)); /* Verify unmatched inclusion patterns. */ assertEqualIntA(m, ARCHIVE_OK, archive_match_path_unmatched_inclusions_next(m, &mp)); assertEqualString("^a1*", mp); assertEqualIntA(m, ARCHIVE_EOF, archive_match_path_unmatched_inclusions_next(m, &mp)); /* Verify unmatched inclusion patterns again in Wide-Char. */ assertEqualIntA(m, ARCHIVE_OK, archive_match_path_unmatched_inclusions_next_w(m, &wp)); assertEqualWString(L"^a1*", wp); assertEqualIntA(m, ARCHIVE_EOF, archive_match_path_unmatched_inclusions_next_w(m, &wp)); /* Clean up. */ archive_entry_free(ae); archive_match_free(m); }
static void test_inclusion_wcs(void) { struct archive_entry *ae; struct archive *m; const char *mp; if (!assert((m = archive_match_new()) != NULL)) return; if (!assert((ae = archive_entry_new()) != NULL)) { archive_match_free(m); return; } /* Test for pattern "^aa*" */ assertEqualIntA(m, 0, archive_match_include_pattern_w(m, L"^aa*")); /* Test with 'aa1234', which should not be excluded. */ archive_entry_copy_pathname(ae, "aa1234"); failure("'aa1234' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"aa1234"); failure("'aa1234' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); /* Test with 'a1234', which should be excluded. */ archive_entry_copy_pathname(ae, "a1234"); failure("'a1234' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"a1234"); failure("'a1234' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); /* Verify unmatched_inclusions. */ assertEqualInt(0, archive_match_path_unmatched_inclusions(m)); assertEqualIntA(m, ARCHIVE_EOF, archive_match_path_unmatched_inclusions_next(m, &mp)); /* Clean up. */ archive_entry_free(ae); archive_match_free(m); }
static void exclusion_from_file(struct archive *m) { struct archive_entry *ae; if (!assert((ae = archive_entry_new()) != NULL)) { archive_match_free(m); return; } /* Test with 'first', which should not be excluded. */ archive_entry_copy_pathname(ae, "first"); failure("'first' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"first"); failure("'first' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); /* Test with 'second', which should be excluded. */ archive_entry_copy_pathname(ae, "second"); failure("'second' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"second"); failure("'second' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); /* Test with 'third', which should not be excluded. */ archive_entry_copy_pathname(ae, "third"); failure("'third' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"third"); failure("'third' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); /* Test with 'four', which should be excluded. */ archive_entry_copy_pathname(ae, "four"); failure("'four' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"four"); failure("'four' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); /* Clean up. */ archive_entry_free(ae); }
static void test_exclusion_mbs(void) { struct archive_entry *ae; struct archive *m; if (!assert((m = archive_match_new()) != NULL)) return; if (!assert((ae = archive_entry_new()) != NULL)) { archive_match_free(m); return; } /* Test for pattern "^aa*" */ assertEqualIntA(m, 0, archive_match_exclude_pattern(m, "^aa*")); /* Test with 'aa1234', which should be excluded. */ archive_entry_copy_pathname(ae, "aa1234"); failure("'aa1234' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"aa1234"); failure("'aa1234' should be excluded"); assertEqualInt(1, archive_match_path_excluded(m, ae)); assertEqualInt(1, archive_match_excluded(m, ae)); /* Test with 'a1234', which should not be excluded. */ archive_entry_copy_pathname(ae, "a1234"); failure("'a1234' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); archive_entry_clear(ae); archive_entry_copy_pathname_w(ae, L"a1234"); failure("'a1234' should not be excluded"); assertEqualInt(0, archive_match_path_excluded(m, ae)); assertEqualInt(0, archive_match_excluded(m, ae)); /* Clean up. */ archive_entry_free(ae); archive_match_free(m); }
static void mode_list(struct cpio *cpio) { struct archive *a; struct archive_entry *entry; int r; a = archive_read_new(); if (a == NULL) lafe_errc(1, 0, "Couldn't allocate archive object"); archive_read_support_filter_all(a); archive_read_support_format_all(a); if (cpio->passphrase != NULL) r = archive_read_add_passphrase(a, cpio->passphrase); else r = archive_read_set_passphrase_callback(a, cpio, &passphrase_callback); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(a)); if (archive_read_open_filename(a, cpio->filename, cpio->bytes_per_block)) lafe_errc(1, archive_errno(a), "%s", archive_error_string(a)); for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { lafe_errc(1, archive_errno(a), "%s", archive_error_string(a)); } if (archive_match_path_excluded(cpio->matching, entry)) continue; if (cpio->verbose) list_item_verbose(cpio, entry); else fprintf(stdout, "%s\n", archive_entry_pathname(entry)); } r = archive_read_close(a); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(a)); if (!cpio->quiet) { int64_t blocks = (archive_filter_bytes(a, 0) + 511) / 512; fprintf(stderr, "%lu %s\n", (unsigned long)blocks, blocks == 1 ? "block" : "blocks"); } archive_read_free(a); exit(0); }
static void mode_in(struct cpio *cpio) { struct archive *a; struct archive_entry *entry; struct archive *ext; const char *destpath; int r; ext = archive_write_disk_new(); if (ext == NULL) lafe_errc(1, 0, "Couldn't allocate restore object"); r = archive_write_disk_set_options(ext, cpio->extract_flags); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(ext)); a = archive_read_new(); if (a == NULL) lafe_errc(1, 0, "Couldn't allocate archive object"); archive_read_support_filter_all(a); archive_read_support_format_all(a); if (archive_read_open_filename(a, cpio->filename, cpio->bytes_per_block)) lafe_errc(1, archive_errno(a), "%s", archive_error_string(a)); for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { lafe_errc(1, archive_errno(a), "%s", archive_error_string(a)); } if (archive_match_path_excluded(cpio->matching, entry)) continue; if (cpio->option_rename) { destpath = cpio_rename(archive_entry_pathname(entry)); archive_entry_set_pathname(entry, destpath); } else destpath = archive_entry_pathname(entry); if (destpath == NULL) continue; if (cpio->verbose) fprintf(stderr, "%s\n", destpath); if (cpio->dot) fprintf(stderr, "."); if (cpio->uid_override >= 0) archive_entry_set_uid(entry, cpio->uid_override); if (cpio->gid_override >= 0) archive_entry_set_gid(entry, cpio->gid_override); r = archive_write_header(ext, entry); if (r != ARCHIVE_OK) { fprintf(stderr, "%s: %s\n", archive_entry_pathname(entry), archive_error_string(ext)); } else if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) > 0) { r = extract_data(a, ext); if (r != ARCHIVE_OK) cpio->return_value = 1; } } r = archive_read_close(a); if (cpio->dot) fprintf(stderr, "\n"); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(a)); r = archive_write_close(ext); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(ext)); if (!cpio->quiet) { int64_t blocks = (archive_filter_bytes(a, 0) + 511) / 512; fprintf(stderr, "%lu %s\n", (unsigned long)blocks, blocks == 1 ? "block" : "blocks"); } archive_read_free(a); archive_write_free(ext); exit(cpio->return_value); }