/* * A single file can have multiple lines contribute specifications. * Parse as many lines as necessary, then pull additional information * from a backing file on disk as necessary. */ static int parse_file(struct archive_read *a, struct archive_entry *entry, struct mtree *mtree, struct mtree_entry *mentry, int *use_next) { const char *path; struct stat st_storage, *st; struct mtree_entry *mp; struct archive_entry *sparse_entry; int r = ARCHIVE_OK, r1, parsed_kws; mentry->used = 1; /* Initialize reasonable defaults. */ archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); archive_string_empty(&mtree->contents_name); /* Parse options from this line. */ parsed_kws = 0; r = parse_line(a, entry, mtree, mentry, &parsed_kws); if (mentry->full) { archive_entry_copy_pathname(entry, mentry->name); /* * "Full" entries are allowed to have multiple lines * and those lines aren't required to be adjacent. We * don't support multiple lines for "relative" entries * nor do we make any attempt to merge data from * separate "relative" and "full" entries. (Merging * "relative" and "full" entries would require dealing * with pathname canonicalization, which is a very * tricky subject.) */ for (mp = mentry->next; mp != NULL; mp = mp->next) { if (mp->full && !mp->used && strcmp(mentry->name, mp->name) == 0) { /* Later lines override earlier ones. */ mp->used = 1; r1 = parse_line(a, entry, mtree, mp, &parsed_kws); if (r1 < r) r = r1; } } } else { /* * Relative entries require us to construct * the full path and possibly update the * current directory. */ size_t n = archive_strlen(&mtree->current_dir); if (n > 0) archive_strcat(&mtree->current_dir, "/"); archive_strcat(&mtree->current_dir, mentry->name); archive_entry_copy_pathname(entry, mtree->current_dir.s); if (archive_entry_filetype(entry) != AE_IFDIR) mtree->current_dir.length = n; } if (mtree->checkfs) { /* * Try to open and stat the file to get the real size * and other file info. It would be nice to avoid * this here so that getting a listing of an mtree * wouldn't require opening every referenced contents * file. But then we wouldn't know the actual * contents size, so I don't see a really viable way * around this. (Also, we may want to someday pull * other unspecified info from the contents file on * disk.) */ mtree->fd = -1; if (archive_strlen(&mtree->contents_name) > 0) path = mtree->contents_name.s; else path = archive_entry_pathname(entry); if (archive_entry_filetype(entry) == AE_IFREG || archive_entry_filetype(entry) == AE_IFDIR) { mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC); __archive_ensure_cloexec_flag(mtree->fd); if (mtree->fd == -1 && (errno != ENOENT || archive_strlen(&mtree->contents_name) > 0)) { archive_set_error(&a->archive, errno, "Can't open %s", path); r = ARCHIVE_WARN; } } st = &st_storage; if (mtree->fd >= 0) { if (fstat(mtree->fd, st) == -1) { archive_set_error(&a->archive, errno, "Could not fstat %s", path); r = ARCHIVE_WARN; /* If we can't stat it, don't keep it open. */ close(mtree->fd); mtree->fd = -1; st = NULL; } } else if (lstat(path, st) == -1) { st = NULL; } /* * Check for a mismatch between the type in the specification * and the type of the contents object on disk. */ if (st != NULL) { if (((st->st_mode & S_IFMT) == S_IFREG && archive_entry_filetype(entry) == AE_IFREG) #ifdef S_IFLNK ||((st->st_mode & S_IFMT) == S_IFLNK && archive_entry_filetype(entry) == AE_IFLNK) #endif #ifdef S_IFSOCK ||((st->st_mode & S_IFSOCK) == S_IFSOCK && archive_entry_filetype(entry) == AE_IFSOCK) #endif #ifdef S_IFCHR ||((st->st_mode & S_IFMT) == S_IFCHR && archive_entry_filetype(entry) == AE_IFCHR) #endif #ifdef S_IFBLK ||((st->st_mode & S_IFMT) == S_IFBLK && archive_entry_filetype(entry) == AE_IFBLK) #endif ||((st->st_mode & S_IFMT) == S_IFDIR && archive_entry_filetype(entry) == AE_IFDIR) #ifdef S_IFIFO ||((st->st_mode & S_IFMT) == S_IFIFO && archive_entry_filetype(entry) == AE_IFIFO) #endif ) { /* Types match. */ } else { /* Types don't match; bail out gracefully. */ if (mtree->fd >= 0) close(mtree->fd); mtree->fd = -1; if (parsed_kws & MTREE_HAS_OPTIONAL) { /* It's not an error for an optional * entry to not match disk. */ *use_next = 1; } else if (r == ARCHIVE_OK) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "mtree specification has different" " type for %s", archive_entry_pathname(entry)); r = ARCHIVE_WARN; } return (r); } } /* * If there is a contents file on disk, pick some of the * metadata from that file. For most of these, we only * set it from the contents if it wasn't already parsed * from the specification. */ if (st != NULL) { if (((parsed_kws & MTREE_HAS_DEVICE) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) && (archive_entry_filetype(entry) == AE_IFCHR || archive_entry_filetype(entry) == AE_IFBLK)) archive_entry_set_rdev(entry, st->st_rdev); if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME)) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_gid(entry, st->st_gid); if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME)) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_uid(entry, st->st_uid); if ((parsed_kws & MTREE_HAS_MTIME) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) { #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC archive_entry_set_mtime(entry, st->st_mtime, st->st_mtimespec.tv_nsec); #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC archive_entry_set_mtime(entry, st->st_mtime, st->st_mtim.tv_nsec); #elif HAVE_STRUCT_STAT_ST_MTIME_N archive_entry_set_mtime(entry, st->st_mtime, st->st_mtime_n); #elif HAVE_STRUCT_STAT_ST_UMTIME archive_entry_set_mtime(entry, st->st_mtime, st->st_umtime*1000); #elif HAVE_STRUCT_STAT_ST_MTIME_USEC archive_entry_set_mtime(entry, st->st_mtime, st->st_mtime_usec*1000); #else archive_entry_set_mtime(entry, st->st_mtime, 0); #endif } if ((parsed_kws & MTREE_HAS_NLINK) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_nlink(entry, st->st_nlink); if ((parsed_kws & MTREE_HAS_PERM) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_perm(entry, st->st_mode); if ((parsed_kws & MTREE_HAS_SIZE) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_size(entry, st->st_size); archive_entry_set_ino(entry, st->st_ino); archive_entry_set_dev(entry, st->st_dev); archive_entry_linkify(mtree->resolver, &entry, &sparse_entry); } else if (parsed_kws & MTREE_HAS_OPTIONAL) { /* * Couldn't open the entry, stat it or the on-disk type * didn't match. If this entry is optional, just * ignore it and read the next header entry. */ *use_next = 1; return ARCHIVE_OK; } } mtree->cur_size = archive_entry_size(entry); mtree->offset = 0; return r; }
/* * Parse a single keyword and its value. */ static int parse_keyword(struct archive_read *a, struct mtree *mtree, struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws) { char *val, *key; key = opt->value; if (*key == '\0') return (ARCHIVE_OK); if (strcmp(key, "nochange") == 0) { *parsed_kws |= MTREE_HAS_NOCHANGE; return (ARCHIVE_OK); } if (strcmp(key, "optional") == 0) { *parsed_kws |= MTREE_HAS_OPTIONAL; return (ARCHIVE_OK); } if (strcmp(key, "ignore") == 0) { /* * The mtree processing is not recursive, so * recursion will only happen for explicitly listed * entries. */ return (ARCHIVE_OK); } val = strchr(key, '='); if (val == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Malformed attribute \"%s\" (%d)", key, key[0]); return (ARCHIVE_WARN); } *val = '\0'; ++val; switch (key[0]) { case 'c': if (strcmp(key, "content") == 0 || strcmp(key, "contents") == 0) { parse_escapes(val, NULL); archive_strcpy(&mtree->contents_name, val); break; } if (strcmp(key, "cksum") == 0) break; case 'd': if (strcmp(key, "device") == 0) { /* stat(2) st_rdev field, e.g. the major/minor IDs * of a char/block special file */ int r; dev_t dev; *parsed_kws |= MTREE_HAS_DEVICE; r = parse_device(&dev, &a->archive, val); if (r == ARCHIVE_OK) archive_entry_set_rdev(entry, dev); return r; } case 'f': if (strcmp(key, "flags") == 0) { *parsed_kws |= MTREE_HAS_FFLAGS; archive_entry_copy_fflags_text(entry, val); break; } case 'g': if (strcmp(key, "gid") == 0) { *parsed_kws |= MTREE_HAS_GID; archive_entry_set_gid(entry, mtree_atol10(&val)); break; } if (strcmp(key, "gname") == 0) { *parsed_kws |= MTREE_HAS_GNAME; archive_entry_copy_gname(entry, val); break; } case 'i': if (strcmp(key, "inode") == 0) { archive_entry_set_ino(entry, mtree_atol10(&val)); break; } case 'l': if (strcmp(key, "link") == 0) { archive_entry_copy_symlink(entry, val); break; } case 'm': if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0) break; if (strcmp(key, "mode") == 0) { if (val[0] >= '0' && val[0] <= '9') { *parsed_kws |= MTREE_HAS_PERM; archive_entry_set_perm(entry, (mode_t)mtree_atol8(&val)); } else { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Symbolic mode \"%s\" unsupported", val); return ARCHIVE_WARN; } break; } case 'n': if (strcmp(key, "nlink") == 0) { *parsed_kws |= MTREE_HAS_NLINK; archive_entry_set_nlink(entry, (unsigned int)mtree_atol10(&val)); break; } case 'r': if (strcmp(key, "resdevice") == 0) { /* stat(2) st_dev field, e.g. the device ID where the * inode resides */ int r; dev_t dev; r = parse_device(&dev, &a->archive, val); if (r == ARCHIVE_OK) archive_entry_set_dev(entry, dev); return r; } if (strcmp(key, "rmd160") == 0 || strcmp(key, "rmd160digest") == 0) break; case 's': if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0) break; if (strcmp(key, "sha256") == 0 || strcmp(key, "sha256digest") == 0) break; if (strcmp(key, "sha384") == 0 || strcmp(key, "sha384digest") == 0) break; if (strcmp(key, "sha512") == 0 || strcmp(key, "sha512digest") == 0) break; if (strcmp(key, "size") == 0) { archive_entry_set_size(entry, mtree_atol10(&val)); break; } case 't': if (strcmp(key, "tags") == 0) { /* * Comma delimited list of tags. * Ignore the tags for now, but the interface * should be extended to allow inclusion/exclusion. */ break; } if (strcmp(key, "time") == 0) { int64_t m; int64_t my_time_t_max = get_time_t_max(); int64_t my_time_t_min = get_time_t_min(); long ns = 0; *parsed_kws |= MTREE_HAS_MTIME; m = mtree_atol10(&val); /* Replicate an old mtree bug: * 123456789.1 represents 123456789 * seconds and 1 nanosecond. */ if (*val == '.') { ++val; ns = (long)mtree_atol10(&val); } else ns = 0; if (m > my_time_t_max) m = my_time_t_max; else if (m < my_time_t_min) m = my_time_t_min; archive_entry_set_mtime(entry, (time_t)m, ns); break; } if (strcmp(key, "type") == 0) { switch (val[0]) { case 'b': if (strcmp(val, "block") == 0) { archive_entry_set_filetype(entry, AE_IFBLK); break; } case 'c': if (strcmp(val, "char") == 0) { archive_entry_set_filetype(entry, AE_IFCHR); break; } case 'd': if (strcmp(val, "dir") == 0) { archive_entry_set_filetype(entry, AE_IFDIR); break; } case 'f': if (strcmp(val, "fifo") == 0) { archive_entry_set_filetype(entry, AE_IFIFO); break; } if (strcmp(val, "file") == 0) { archive_entry_set_filetype(entry, AE_IFREG); break; } case 'l': if (strcmp(val, "link") == 0) { archive_entry_set_filetype(entry, AE_IFLNK); break; } default: archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Unrecognized file type \"%s\"; " "assuming \"file\"", val); archive_entry_set_filetype(entry, AE_IFREG); return (ARCHIVE_WARN); } *parsed_kws |= MTREE_HAS_TYPE; break; } case 'u': if (strcmp(key, "uid") == 0) { *parsed_kws |= MTREE_HAS_UID; archive_entry_set_uid(entry, mtree_atol10(&val)); break; } if (strcmp(key, "uname") == 0) { *parsed_kws |= MTREE_HAS_UNAME; archive_entry_copy_uname(entry, val); break; } default: archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Unrecognized key %s=%s", key, val); return (ARCHIVE_WARN); } return (ARCHIVE_OK); }
static void test_format(int (*set_format)(struct archive *)) { char filedata[64]; struct archive_entry *ae; struct archive *a; size_t used; size_t buffsize = 1000000; char *buff; const char *err; buff = malloc(buffsize); /* Create a new archive in memory. */ assert((a = archive_write_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, (*set_format)(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used)); /* * Write a file to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_pathname(ae, "test"); archive_entry_set_filetype(ae, AE_IFREG); assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); archive_entry_free(ae); assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* * Read from it. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_none(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualIntA(a, 9, archive_read_data(a, filedata, 10)); assertEqualMem(filedata, "12345678", 9); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); /* Create a new archive */ assert((a = archive_write_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, (*set_format)(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used)); /* write first file: that should succeed */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_pathname(ae, "test"); archive_entry_set_filetype(ae, AE_IFREG); assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); archive_entry_free(ae); assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); /* write second file: this should fail */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_pathname(ae, "test2"); archive_entry_set_filetype(ae, AE_IFREG); assertEqualIntA(a, ARCHIVE_FATAL, archive_write_header(a, ae)); err = archive_error_string(a); assertEqualMem(err, "Raw format only supports one entry per archive", 47); archive_entry_free(ae); assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* Create a new archive */ assert((a = archive_write_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, (*set_format)(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used)); /* write a directory: this should fail */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "dir"); archive_entry_set_filetype(ae, AE_IFDIR); archive_entry_set_size(ae, 512); assertEqualIntA(a, ARCHIVE_FATAL, archive_write_header(a, ae)); err = archive_error_string(a); assertEqualMem(err, "Raw format only supports filetype AE_IFREG", 43); archive_entry_free(ae); assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); free(buff); }
/* * Parse a single keyword and its value. */ static int parse_keyword(struct archive_read *a, struct mtree *mtree, struct archive_entry *entry, struct mtree_option *option, int *parsed_kws) { char *val, *key; key = option->value; if (*key == '\0') return (ARCHIVE_OK); if (strcmp(key, "optional") == 0) { *parsed_kws |= MTREE_HAS_OPTIONAL; return (ARCHIVE_OK); } if (strcmp(key, "ignore") == 0) { /* * The mtree processing is not recursive, so * recursion will only happen for explicitly listed * entries. */ return (ARCHIVE_OK); } val = strchr(key, '='); if (val == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Malformed attribute \"%s\" (%d)", key, key[0]); return (ARCHIVE_WARN); } *val = '\0'; ++val; switch (key[0]) { case 'c': if (strcmp(key, "content") == 0 || strcmp(key, "contents") == 0) { parse_escapes(val, NULL); archive_strcpy(&mtree->contents_name, val); break; } if (strcmp(key, "cksum") == 0) break; case 'd': if (strcmp(key, "device") == 0) { *parsed_kws |= MTREE_HAS_DEVICE; return parse_device(&a->archive, entry, val); } case 'f': if (strcmp(key, "flags") == 0) { *parsed_kws |= MTREE_HAS_FFLAGS; archive_entry_copy_fflags_text(entry, val); break; } case 'g': if (strcmp(key, "gid") == 0) { *parsed_kws |= MTREE_HAS_GID; archive_entry_set_gid(entry, mtree_atol10(&val)); break; } if (strcmp(key, "gname") == 0) { *parsed_kws |= MTREE_HAS_GNAME; archive_entry_copy_gname(entry, val); break; } case 'l': if (strcmp(key, "link") == 0) { archive_entry_copy_symlink(entry, val); break; } case 'm': if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0) break; if (strcmp(key, "mode") == 0) { if (val[0] >= '0' && val[0] <= '9') { *parsed_kws |= MTREE_HAS_PERM; archive_entry_set_perm(entry, mtree_atol8(&val)); } else { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Symbolic mode \"%s\" unsupported", val); return ARCHIVE_WARN; } break; } case 'n': if (strcmp(key, "nlink") == 0) { *parsed_kws |= MTREE_HAS_NLINK; archive_entry_set_nlink(entry, mtree_atol10(&val)); break; } case 'r': if (strcmp(key, "rmd160") == 0 || strcmp(key, "rmd160digest") == 0) break; case 's': if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0) break; if (strcmp(key, "sha256") == 0 || strcmp(key, "sha256digest") == 0) break; if (strcmp(key, "sha384") == 0 || strcmp(key, "sha384digest") == 0) break; if (strcmp(key, "sha512") == 0 || strcmp(key, "sha512digest") == 0) break; if (strcmp(key, "size") == 0) { archive_entry_set_size(entry, mtree_atol10(&val)); break; } case 't': if (strcmp(key, "tags") == 0) { /* * Comma delimited list of tags. * Ignore the tags for now, but the interface * should be extended to allow inclusion/exclusion. */ break; } if (strcmp(key, "time") == 0) { time_t m; long ns; *parsed_kws |= MTREE_HAS_MTIME; m = (time_t)mtree_atol10(&val); if (*val == '.') { ++val; ns = (long)mtree_atol10(&val); } else ns = 0; archive_entry_set_mtime(entry, m, ns); break; } if (strcmp(key, "type") == 0) { *parsed_kws |= MTREE_HAS_TYPE; switch (val[0]) { case 'b': if (strcmp(val, "block") == 0) { mtree->filetype = AE_IFBLK; break; } case 'c': if (strcmp(val, "char") == 0) { mtree->filetype = AE_IFCHR; break; } case 'd': if (strcmp(val, "dir") == 0) { mtree->filetype = AE_IFDIR; break; } case 'f': if (strcmp(val, "fifo") == 0) { mtree->filetype = AE_IFIFO; break; } if (strcmp(val, "file") == 0) { mtree->filetype = AE_IFREG; break; } case 'l': if (strcmp(val, "link") == 0) { mtree->filetype = AE_IFLNK; break; } default: archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Unrecognized file type \"%s\"", val); return (ARCHIVE_WARN); } archive_entry_set_filetype(entry, mtree->filetype); break; } case 'u': if (strcmp(key, "uid") == 0) { *parsed_kws |= MTREE_HAS_UID; archive_entry_set_uid(entry, mtree_atol10(&val)); break; } if (strcmp(key, "uname") == 0) { *parsed_kws |= MTREE_HAS_UNAME; archive_entry_copy_uname(entry, val); break; } default: archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Unrecognized key %s=%s", key, val); return (ARCHIVE_WARN); } return (ARCHIVE_OK); }
static void test_zip_filename_encoding_CP932(void) { struct archive *a; struct archive_entry *entry; char buff[4096]; size_t used; if (NULL == setlocale(LC_ALL, "Japanese_Japan") && NULL == setlocale(LC_ALL, "ja_JP.SJIS")) { skipping("CP932/SJIS locale not available on this system."); return; } /* * Verify that EUC-JP filenames are correctly translated to UTF-8. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); if (archive_write_set_options(a, "hdrcharset=UTF-8") != ARCHIVE_OK) { skipping("This system cannot convert character-set" " from CP932/SJIS to UTF-8."); archive_write_free(a); return; } assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a CP932/SJIS filename. */ archive_entry_set_pathname(entry, "\x95\x5C.txt"); /* Check the Unicode version. */ archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0x08, * which indicates the filename charset is UTF-8. */ assertEqualInt(0x08, buff[7]); /* Check UTF-8 version. */ assertEqualMem(buff + 30, "\xE8\xA1\xA8.txt", 7); /* * Verify that CP932/SJIS filenames are not translated to UTF-8. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a CP932/SJIS filename. */ archive_entry_set_pathname(entry, "\x95\x5C.txt"); /* Check the Unicode version. */ archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0, * which indicates the filename charset is unknown. */ assertEqualInt(0, buff[7]); /* Above three characters in CP932/SJIS should not translate to * any character-set. */ assertEqualMem(buff + 30, "\x95\x5C.txt", 6); /* * Verify that A bit 11 of general purpose flag is not set * when ASCII filenames are stored even if hdrcharset=UTF-8 * is specified. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); if (archive_write_set_options(a, "hdrcharset=UTF-8") != ARCHIVE_OK) { skipping("This system cannot convert character-set" " from CP932/SJIS to UTF-8."); archive_write_free(a); return; } assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set an ASCII filename. */ archive_entry_set_pathname(entry, "abcABC"); /* Check the Unicode version. */ archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0, * which indicates the filename charset is unknown. */ assertEqualInt(0, buff[7]); assertEqualMem(buff + 30, "abcABC", 6); }
int fwfile_add_local_file(struct archive *a, const char *resource_name, const char *local_paths, const struct fwfile_assertions *assertions) { int rc = 0; off_t copy_buffer_len = 64 * 1024; char *copy_buffer = (char *) malloc(copy_buffer_len); struct archive_entry *entry = archive_entry_new(); off_t total_read = 0; char *paths = strdup(local_paths); FILE *fp = NULL; if (*paths == '\0') ERR_CLEANUP_MSG("must specify a host-path for resource '%s'", resource_name); off_t total_len; if (calculate_total_filesize(local_paths, &total_len) < 0) goto cleanup; // Error set by calculate_total_filesize() if (assertions) { if (assertions->assert_gte >= 0 && !(total_len >= assertions->assert_gte)) ERR_CLEANUP_MSG("file size assertion failed on '%s'. Size is %d bytes. It must be >= %d bytes (%d blocks)", local_paths, total_len, assertions->assert_gte, assertions->assert_gte / 512); if (assertions->assert_lte >= 0 && !(total_len <= assertions->assert_lte)) ERR_CLEANUP_MSG("file size assertion failed on '%s'. Size is %d bytes. It must be <= %d bytes (%d blocks)", local_paths, total_len, assertions->assert_lte, assertions->assert_lte / 512); } // Convert the resource name to an archive path (most resources should be in the data directory) char archive_path[FWFILE_MAX_ARCHIVE_PATH]; size_t resource_name_len = strlen(resource_name); if (resource_name_len + 6 > sizeof(archive_path)) ERR_CLEANUP_MSG("resource name '%s' is too long", resource_name); if (resource_name_len == '\0') ERR_CLEANUP_MSG("resource name can't be empty"); if (resource_name[resource_name_len - 1] == '/') ERR_CLEANUP_MSG("resource name '%s' can't end in a '/'", resource_name); if (resource_name[0] == '/') { if (resource_name[1] == '\0') ERR_CLEANUP_MSG("resource name can't be the root directory"); // This seems like it's just asking for trouble, so error out. if (strcmp(resource_name, "/meta.conf") == 0) ERR_CLEANUP_MSG("resources can't be named /meta.conf"); // Absolute paths are not intended to be commonly used and ones // in /data won't work when applying the updates, so error out. if (memcmp(resource_name, "/data/", 6) == 0 || strcmp(resource_name, "/data") == 0) ERR_CLEANUP_MSG("use a normal resource name rather than specifying /data"); strcpy(archive_path, &resource_name[1]); } else { sprintf(archive_path, "data/%s", resource_name); } archive_entry_set_pathname(entry, archive_path); archive_entry_set_size(entry, total_len); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); for (char *path = strtok(paths, ";"); path != NULL; path = strtok(NULL, ";")) { fp = fopen(path, "rb"); if (!fp) ERR_CLEANUP_MSG("can't open '%s'", path); size_t len = fread(copy_buffer, 1, (size_t) copy_buffer_len, fp); off_t file_read = (off_t) len; while (len > 0) { off_t written = archive_write_data(a, copy_buffer, len); if (written != (off_t) len) ERR_CLEANUP_MSG("error writing to archive"); len = fread(copy_buffer, 1, copy_buffer_len, fp); file_read += len; } total_read += file_read; fclose(fp); fp = NULL; } if (total_read != total_len) ERR_CLEANUP_MSG("read error for '%s'", paths); cleanup: archive_entry_free(entry); if (fp) fclose(fp); free(copy_buffer); free(paths); return rc; }
static void test_zip_filename_encoding_UTF8(void) { struct archive *a; struct archive_entry *entry; char buff[4096]; size_t used; if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) { skipping("en_US.UTF-8 locale not available on this system."); return; } /* * Verify that UTF-8 filenames are correctly stored with * hdrcharset=UTF-8 option. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); if (archive_write_set_options(a, "hdrcharset=UTF-8") != ARCHIVE_OK) { skipping("This system cannot convert character-set" " for UTF-8."); archive_write_free(a); return; } assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a UTF-8 filename. */ archive_entry_set_pathname(entry, "\xD0\xBF\xD1\x80\xD0\xB8"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0x08, * which indicates the filename charset is UTF-8. */ assertEqualInt(0x08, buff[7]); assertEqualMem(buff + 30, "\xD0\xBF\xD1\x80\xD0\xB8", 6); /* * Verify that UTF-8 filenames are correctly stored without * hdrcharset=UTF-8 option. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a UTF-8 filename. */ archive_entry_set_pathname(entry, "\xD0\xBF\xD1\x80\xD0\xB8"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0x08, * which indicates the filename charset is UTF-8. */ assertEqualInt(0x08, buff[7]); assertEqualMem(buff + 30, "\xD0\xBF\xD1\x80\xD0\xB8", 6); /* * Verify that A bit 11 of general purpose flag is not set * when ASCII filenames are stored. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set an ASCII filename. */ archive_entry_set_pathname(entry, "abcABC"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0, * which indicates the filename charset is unknown. */ assertEqualInt(0, buff[7]); assertEqualMem(buff + 30, "abcABC", 6); }
/* * Other archiver applications on Windows translate CP1251 filenames * into CP866 filenames and store it in the zip file. * Test above behavior works well. */ static void test_zip_filename_encoding_Russian_Russia(void) { struct archive *a; struct archive_entry *entry; char buff[4096]; size_t used; if (NULL == setlocale(LC_ALL, "Russian_Russia")) { skipping("Russian_Russia locale not available on this system."); return; } /* * Verify that Russian_Russia(CP1251) filenames are correctly translated * to UTF-8. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); if (archive_write_set_options(a, "hdrcharset=UTF-8") != ARCHIVE_OK) { skipping("This system cannot convert character-set" " from Russian_Russia.CP1251 to UTF-8."); archive_write_free(a); return; } assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a CP1251 filename. */ archive_entry_set_pathname(entry, "\xEF\xF0\xE8"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0x08, * which indicates the filename charset is UTF-8. */ assertEqualInt(0x08, buff[7]); /* Above three characters in CP1251 should translate to the following * three characters (two bytes each) in UTF-8. */ assertEqualMem(buff + 30, "\xD0\xBF\xD1\x80\xD0\xB8", 6); /* * Verify that Russian_Russia(CP1251) filenames are correctly translated * to CP866. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a CP1251 filename. */ archive_entry_set_pathname(entry, "\xEF\xF0\xE8"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0, * which indicates the filename charset is unknown. */ assertEqualInt(0, buff[7]); /* Above three characters in CP1251 should translate to the following * three characters in CP866. */ assertEqualMem(buff + 30, "\xAF\xE0\xA8", 3); }
/* * Set the locale and write a pathname containing invalid characters. * This should work; the underlying implementation should automatically * fall back to storing the pathname in binary. */ static void test_pax_filename_encoding_2(void) { char filename[] = "abc\314\214mno\374xyz"; struct archive *a; struct archive_entry *entry; char buff[65536]; char longname[] = "abc\314\214mno\374xyz" "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz" "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz" "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz" "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz" "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz" "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz" ; size_t used; /* * We need a starting locale which has invalid sequences. * en_US.UTF-8 seems to be commonly supported. */ /* If it doesn't exist, just warn and return. */ if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) { skipping("invalid encoding tests require a suitable locale;" " en_US.UTF-8 not available on this system"); return; } assert((a = archive_write_new()) != NULL); assertEqualIntA(a, 0, archive_write_set_format_pax(a)); assertEqualIntA(a, 0, archive_write_add_filter_none(a)); assertEqualIntA(a, 0, archive_write_set_bytes_per_block(a, 0)); assertEqualInt(0, archive_write_open_memory(a, buff, sizeof(buff), &used)); assert((entry = archive_entry_new()) != NULL); /* Set pathname, gname, uname, hardlink to nonconvertible values. */ archive_entry_copy_pathname(entry, filename); archive_entry_copy_gname(entry, filename); archive_entry_copy_uname(entry, filename); archive_entry_copy_hardlink(entry, filename); archive_entry_set_filetype(entry, AE_IFREG); failure("This should generate a warning for nonconvertible names."); assertEqualInt(ARCHIVE_WARN, archive_write_header(a, entry)); archive_entry_free(entry); assert((entry = archive_entry_new()) != NULL); /* Set path, gname, uname, and symlink to nonconvertible values. */ archive_entry_copy_pathname(entry, filename); archive_entry_copy_gname(entry, filename); archive_entry_copy_uname(entry, filename); archive_entry_copy_symlink(entry, filename); archive_entry_set_filetype(entry, AE_IFLNK); failure("This should generate a warning for nonconvertible names."); assertEqualInt(ARCHIVE_WARN, archive_write_header(a, entry)); archive_entry_free(entry); assert((entry = archive_entry_new()) != NULL); /* Set pathname to a very long nonconvertible value. */ archive_entry_copy_pathname(entry, longname); archive_entry_set_filetype(entry, AE_IFREG); failure("This should generate a warning for nonconvertible names."); assertEqualInt(ARCHIVE_WARN, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* * Now read the entries back. */ assert((a = archive_read_new()) != NULL); assertEqualInt(0, archive_read_support_format_tar(a)); assertEqualInt(0, archive_read_open_memory(a, buff, used)); assertEqualInt(0, archive_read_next_header(a, &entry)); assertEqualString(filename, archive_entry_pathname(entry)); assertEqualString(filename, archive_entry_gname(entry)); assertEqualString(filename, archive_entry_uname(entry)); assertEqualString(filename, archive_entry_hardlink(entry)); assertEqualInt(0, archive_read_next_header(a, &entry)); assertEqualString(filename, archive_entry_pathname(entry)); assertEqualString(filename, archive_entry_gname(entry)); assertEqualString(filename, archive_entry_uname(entry)); assertEqualString(filename, archive_entry_symlink(entry)); assertEqualInt(0, archive_read_next_header(a, &entry)); assertEqualString(longname, archive_entry_pathname(entry)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void test_zip_filename_encoding_KOI8R(void) { struct archive *a; struct archive_entry *entry; char buff[4096]; size_t used; if (NULL == setlocale(LC_ALL, "ru_RU.KOI8-R")) { skipping("KOI8-R locale not available on this system."); return; } /* * Verify that KOI8-R filenames are correctly translated to UTF-8. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); if (archive_write_set_options(a, "hdrcharset=UTF-8") != ARCHIVE_OK) { skipping("This system cannot convert character-set" " from KOI8-R to UTF-8."); archive_write_free(a); return; } assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a KOI8-R filename. */ archive_entry_set_pathname(entry, "\xD0\xD2\xC9"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0x08, * which indicates the filename charset is UTF-8. */ assertEqualInt(0x08, buff[7]); /* Above three characters in KOI8-R should translate to the following * three characters (two bytes each) in UTF-8. */ assertEqualMem(buff + 30, "\xD0\xBF\xD1\x80\xD0\xB8", 6); /* * Verify that KOI8-R filenames are not translated to UTF-8. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set a KOI8-R filename. */ archive_entry_set_pathname(entry, "\xD0\xD2\xC9"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0, * which indicates the filename charset is unknown. */ assertEqualInt(0, buff[7]); /* Above three characters in KOI8-R should not translate to * any character-set. */ assertEqualMem(buff + 30, "\xD0\xD2\xC9", 3); /* * Verify that A bit 11 of general purpose flag is not set * when ASCII filenames are stored even if hdrcharset=UTF-8 * is specified. */ a = archive_write_new(); assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a)); if (archive_write_set_options(a, "hdrcharset=UTF-8") != ARCHIVE_OK) { skipping("This system cannot convert character-set" " from KOI8-R to UTF-8."); archive_write_free(a); return; } assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); entry = archive_entry_new2(a); /* Set an ASCII filename. */ archive_entry_set_pathname(entry, "abcABC"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* A bit 11 of general purpose flag should be 0, * which indicates the filename charset is unknown. */ assertEqualInt(0, buff[7]); assertEqualMem(buff + 30, "abcABC", 6); }
/* * Create an entry starting from a wide-character Unicode pathname, * read it back into "C" locale, which doesn't support the name. * TODO: Figure out the "right" behavior here. */ static void test_pax_filename_encoding_3(void) { wchar_t badname[] = L"xxxAyyyBzzz"; const char badname_utf8[] = "xxx\xE1\x88\xB4yyy\xE5\x99\xB8zzz"; struct archive *a; struct archive_entry *entry; char buff[65536]; size_t used; badname[3] = 0x1234; badname[7] = 0x5678; /* If it doesn't exist, just warn and return. */ if (NULL == setlocale(LC_ALL, "C")) { skipping("Can't set \"C\" locale, so can't exercise " "certain character-conversion failures"); return; } /* If wctomb is broken, warn and return. */ if (wctomb(buff, 0x1234) > 0) { skipping("Cannot test conversion failures because \"C\" " "locale on this system has no invalid characters."); return; } /* If wctomb is broken, warn and return. */ if (wctomb(buff, 0x1234) > 0) { skipping("Cannot test conversion failures because \"C\" " "locale on this system has no invalid characters."); return; } /* Skip test if archive_entry_update_pathname_utf8() is broken. */ /* In particular, this is currently broken on Win32 because * setlocale() does not set the default encoding for CP_ACP. */ entry = archive_entry_new(); if (archive_entry_update_pathname_utf8(entry, badname_utf8)) { archive_entry_free(entry); skipping("Cannot test conversion failures."); return; } archive_entry_free(entry); assert((a = archive_write_new()) != NULL); assertEqualIntA(a, 0, archive_write_set_format_pax(a)); assertEqualIntA(a, 0, archive_write_add_filter_none(a)); assertEqualIntA(a, 0, archive_write_set_bytes_per_block(a, 0)); assertEqualInt(0, archive_write_open_memory(a, buff, sizeof(buff), &used)); assert((entry = archive_entry_new()) != NULL); /* Set pathname to non-convertible wide value. */ archive_entry_copy_pathname_w(entry, badname); archive_entry_set_filetype(entry, AE_IFREG); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assert((entry = archive_entry_new()) != NULL); archive_entry_copy_pathname_w(entry, L"abc"); /* Set gname to non-convertible wide value. */ archive_entry_copy_gname_w(entry, badname); archive_entry_set_filetype(entry, AE_IFREG); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assert((entry = archive_entry_new()) != NULL); archive_entry_copy_pathname_w(entry, L"abc"); /* Set uname to non-convertible wide value. */ archive_entry_copy_uname_w(entry, badname); archive_entry_set_filetype(entry, AE_IFREG); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assert((entry = archive_entry_new()) != NULL); archive_entry_copy_pathname_w(entry, L"abc"); /* Set hardlink to non-convertible wide value. */ archive_entry_copy_hardlink_w(entry, badname); archive_entry_set_filetype(entry, AE_IFREG); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assert((entry = archive_entry_new()) != NULL); archive_entry_copy_pathname_w(entry, L"abc"); /* Set symlink to non-convertible wide value. */ archive_entry_copy_symlink_w(entry, badname); archive_entry_set_filetype(entry, AE_IFLNK); assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry)); archive_entry_free(entry); assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* * Now read the entries back. */ assert((a = archive_read_new()) != NULL); assertEqualInt(0, archive_read_support_format_tar(a)); assertEqualInt(0, archive_read_open_memory(a, buff, used)); failure("A non-convertible pathname should cause a warning."); assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry)); assertEqualWString(badname, archive_entry_pathname_w(entry)); failure("If native locale can't convert, we should get UTF-8 back."); assertEqualString(badname_utf8, archive_entry_pathname(entry)); failure("A non-convertible gname should cause a warning."); assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry)); assertEqualWString(badname, archive_entry_gname_w(entry)); failure("If native locale can't convert, we should get UTF-8 back."); assertEqualString(badname_utf8, archive_entry_gname(entry)); failure("A non-convertible uname should cause a warning."); assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry)); assertEqualWString(badname, archive_entry_uname_w(entry)); failure("If native locale can't convert, we should get UTF-8 back."); assertEqualString(badname_utf8, archive_entry_uname(entry)); failure("A non-convertible hardlink should cause a warning."); assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry)); assertEqualWString(badname, archive_entry_hardlink_w(entry)); failure("If native locale can't convert, we should get UTF-8 back."); assertEqualString(badname_utf8, archive_entry_hardlink(entry)); failure("A non-convertible symlink should cause a warning."); assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry)); assertEqualWString(badname, archive_entry_symlink_w(entry)); assertEqualWString(NULL, archive_entry_hardlink_w(entry)); failure("If native locale can't convert, we should get UTF-8 back."); assertEqualString(badname_utf8, archive_entry_symlink(entry)); assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &entry)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static int _ar_read_header(struct archive_read *a, struct archive_entry *entry, struct ar *ar, const char *h, size_t *unconsumed) { char filename[AR_name_size + 1]; uint64_t number; /* Used to hold parsed numbers before validation. */ size_t bsd_name_length, entry_size; char *p, *st; const void *b; int r; /* Verify the magic signature on the file header. */ if (strncmp(h + AR_fmag_offset, "`\n", 2) != 0) { archive_set_error(&a->archive, EINVAL, "Incorrect file header signature"); return (ARCHIVE_WARN); } /* Copy filename into work buffer. */ strncpy(filename, h + AR_name_offset, AR_name_size); filename[AR_name_size] = '\0'; /* * Guess the format variant based on the filename. */ if (a->archive.archive_format == ARCHIVE_FORMAT_AR) { /* We don't already know the variant, so let's guess. */ /* * Biggest clue is presence of '/': GNU starts special * filenames with '/', appends '/' as terminator to * non-special names, so anything with '/' should be * GNU except for BSD long filenames. */ if (strncmp(filename, "#1/", 3) == 0) a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD; else if (strchr(filename, '/') != NULL) a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU; else if (strncmp(filename, "__.SYMDEF", 9) == 0) a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD; /* * XXX Do GNU/SVR4 'ar' programs ever omit trailing '/' * if name exactly fills 16-byte field? If so, we * can't assume entries without '/' are BSD. XXX */ } /* Update format name from the code. */ if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU) a->archive.archive_format_name = "ar (GNU/SVR4)"; else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD) a->archive.archive_format_name = "ar (BSD)"; else a->archive.archive_format_name = "ar"; /* * Remove trailing spaces from the filename. GNU and BSD * variants both pad filename area out with spaces. * This will only be wrong if GNU/SVR4 'ar' implementations * omit trailing '/' for 16-char filenames and we have * a 16-char filename that ends in ' '. */ p = filename + AR_name_size - 1; while (p >= filename && *p == ' ') { *p = '\0'; p--; } /* * Remove trailing slash unless first character is '/'. * (BSD entries never end in '/', so this will only trim * GNU-format entries. GNU special entries start with '/' * and are not terminated in '/', so we don't trim anything * that starts with '/'.) */ if (filename[0] != '/' && *p == '/') *p = '\0'; /* * '//' is the GNU filename table. * Later entries can refer to names in this table. */ if (strcmp(filename, "//") == 0) { /* This must come before any call to _read_ahead. */ ar_parse_common_header(ar, entry, h); archive_entry_copy_pathname(entry, filename); archive_entry_set_filetype(entry, AE_IFREG); /* Get the size of the filename table. */ number = ar_atol10(h + AR_size_offset, AR_size_size); if (number > SIZE_MAX) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Filename table too large"); return (ARCHIVE_FATAL); } entry_size = (size_t)number; if (entry_size == 0) { archive_set_error(&a->archive, EINVAL, "Invalid string table"); return (ARCHIVE_WARN); } if (ar->strtab != NULL) { archive_set_error(&a->archive, EINVAL, "More than one string tables exist"); return (ARCHIVE_WARN); } /* Read the filename table into memory. */ st = malloc(entry_size); if (st == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate filename table buffer"); return (ARCHIVE_FATAL); } ar->strtab = st; ar->strtab_size = entry_size; if (*unconsumed) { __archive_read_consume(a, *unconsumed); *unconsumed = 0; } if ((b = __archive_read_ahead(a, entry_size, NULL)) == NULL) return (ARCHIVE_FATAL); memcpy(st, b, entry_size); __archive_read_consume(a, entry_size); /* All contents are consumed. */ ar->entry_bytes_remaining = 0; archive_entry_set_size(entry, ar->entry_bytes_remaining); /* Parse the filename table. */ return (ar_parse_gnu_filename_table(a)); } /* * GNU variant handles long filenames by storing /<number> * to indicate a name stored in the filename table. * XXX TODO: Verify that it's all digits... Don't be fooled * by "/9xyz" XXX */ if (filename[0] == '/' && filename[1] >= '0' && filename[1] <= '9') { number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1); /* * If we can't look up the real name, warn and return * the entry with the wrong name. */ if (ar->strtab == NULL || number > ar->strtab_size) { archive_set_error(&a->archive, EINVAL, "Can't find long filename for entry"); archive_entry_copy_pathname(entry, filename); /* Parse the time, owner, mode, size fields. */ ar_parse_common_header(ar, entry, h); return (ARCHIVE_WARN); } archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]); /* Parse the time, owner, mode, size fields. */ return (ar_parse_common_header(ar, entry, h)); } /* * BSD handles long filenames by storing "#1/" followed by the * length of filename as a decimal number, then prepends the * the filename to the file contents. */ if (strncmp(filename, "#1/", 3) == 0) { /* Parse the time, owner, mode, size fields. */ /* This must occur before _read_ahead is called again. */ ar_parse_common_header(ar, entry, h); /* Parse the size of the name, adjust the file size. */ number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3); bsd_name_length = (size_t)number; /* Guard against the filename + trailing NUL * overflowing a size_t and against the filename size * being larger than the entire entry. */ if (number > (uint64_t)(bsd_name_length + 1) || (int64_t)bsd_name_length > ar->entry_bytes_remaining) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Bad input file size"); return (ARCHIVE_FATAL); } ar->entry_bytes_remaining -= bsd_name_length; /* Adjust file size reported to client. */ archive_entry_set_size(entry, ar->entry_bytes_remaining); if (*unconsumed) { __archive_read_consume(a, *unconsumed); *unconsumed = 0; } /* Read the long name into memory. */ if ((b = __archive_read_ahead(a, bsd_name_length, NULL)) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Truncated input file"); return (ARCHIVE_FATAL); } /* Store it in the entry. */ p = (char *)malloc(bsd_name_length + 1); if (p == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate fname buffer"); return (ARCHIVE_FATAL); } strncpy(p, b, bsd_name_length); p[bsd_name_length] = '\0'; __archive_read_consume(a, bsd_name_length); archive_entry_copy_pathname(entry, p); free(p); return (ARCHIVE_OK); } /* * "/" is the SVR4/GNU archive symbol table. */ if (strcmp(filename, "/") == 0) { archive_entry_copy_pathname(entry, "/"); /* Parse the time, owner, mode, size fields. */ r = ar_parse_common_header(ar, entry, h); /* Force the file type to a regular file. */ archive_entry_set_filetype(entry, AE_IFREG); return (r); } /* * "__.SYMDEF" is a BSD archive symbol table. */ if (strcmp(filename, "__.SYMDEF") == 0) { archive_entry_copy_pathname(entry, filename); /* Parse the time, owner, mode, size fields. */ return (ar_parse_common_header(ar, entry, h)); } /* * Otherwise, this is a standard entry. The filename * has already been trimmed as much as possible, based * on our current knowledge of the format. */ archive_entry_copy_pathname(entry, filename); return (ar_parse_common_header(ar, entry, h)); }
int fwfile_add_local_file(struct archive *a, const char *resource_name, const char *local_path) { int rc = 0; off_t copy_buffer_len = 64 * 1024; char *copy_buffer = (char *) malloc(copy_buffer_len); struct archive_entry *entry = 0; FILE *fp = fopen(local_path, "rb"); if (!fp) ERR_CLEANUP_MSG("can't open local file"); fseeko(fp, 0, SEEK_END); off_t total_len = ftello(fp); fseeko(fp, 0, SEEK_SET); entry = archive_entry_new(); // Convert the resource name to an archive path (most resources should be in the data directory) char archive_path[FWFILE_MAX_ARCHIVE_PATH]; size_t resource_name_len = strlen(resource_name); if (resource_name_len + 6 > sizeof(archive_path)) ERR_CLEANUP_MSG("resource name is too long"); if (resource_name_len == '\0') ERR_CLEANUP_MSG("resource name can't be empty"); if (resource_name[resource_name_len - 1] == '/') ERR_CLEANUP_MSG("resource name can't end in a '/'"); if (resource_name[0] == '/') { if (resource_name[1] == '\0') ERR_CLEANUP_MSG("resource name can't be the root directory"); // This seems like it's just asking for trouble, so error out. if (strcmp(resource_name, "/meta.conf") == 0) ERR_CLEANUP_MSG("resources can't be named /meta.conf"); // Absolute paths are not intended to be commonly used and ones // in /data won't work when applying the updates, so error out. if (memcmp(resource_name, "/data/", 6) == 0 || strcmp(resource_name, "/data") == 0) ERR_CLEANUP_MSG("use a normal resource name rather than specifying /data"); strcpy(archive_path, &resource_name[1]); } else { sprintf(archive_path, "data/%s", resource_name); } archive_entry_set_pathname(entry, archive_path); archive_entry_set_size(entry, total_len); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); size_t len = fread(copy_buffer, 1, (size_t)copy_buffer_len, fp); off_t total_read = (off_t)len; while (len > 0) { off_t written = archive_write_data(a, copy_buffer, len); if (written != (off_t) len) ERR_CLEANUP_MSG("error writing to archive"); len = fread(copy_buffer, 1, copy_buffer_len, fp); total_read += len; } if (total_read != total_len) ERR_CLEANUP_MSG("read an unexpected amount of data"); cleanup: archive_entry_free(entry); if (fp) fclose(fp); free(copy_buffer); return rc; }
static void test_format(int (*set_format)(struct archive *)) { char filedata[64]; struct archive_entry *ae; struct archive *a; char *p; size_t used; size_t buffsize = 1000000; char *buff; int damaged = 0; buff = malloc(buffsize); /* Create a new archive in memory. */ assert((a = archive_write_new()) != NULL); assertA(0 == (*set_format)(a)); assertA(0 == archive_write_add_filter_none(a)); assertA(0 == archive_write_open_memory(a, buff, buffsize, &used)); /* * Write a file to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_mtime(ae, 1, 10); assert(1 == archive_entry_mtime(ae)); assert(10 == archive_entry_mtime_nsec(ae)); p = strdup("file"); archive_entry_copy_pathname(ae, p); strcpy(p, "XXXX"); free(p); assertEqualString("file", archive_entry_pathname(ae)); archive_entry_set_mode(ae, S_IFREG | 0755); assert((S_IFREG | 0755) == archive_entry_mode(ae)); archive_entry_set_size(ae, 8); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); assertA(8 == archive_write_data(a, "12345678", 9)); /* * Write another file to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_mtime(ae, 1, 10); assert(1 == archive_entry_mtime(ae)); assert(10 == archive_entry_mtime_nsec(ae)); p = strdup("file2"); archive_entry_copy_pathname(ae, p); strcpy(p, "XXXX"); free(p); assertEqualString("file2", archive_entry_pathname(ae)); archive_entry_set_mode(ae, S_IFREG | 0755); assert((S_IFREG | 0755) == archive_entry_mode(ae)); archive_entry_set_size(ae, 4); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); assertA(4 == archive_write_data(a, "1234", 5)); /* * Write a file with a name, filetype, and size. */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "name"); archive_entry_set_size(ae, 0); archive_entry_set_filetype(ae, AE_IFREG); assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); assert(archive_error_string(a) == NULL); archive_entry_free(ae); /* * Write a file with a name and filetype but no size. */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "name"); archive_entry_unset_size(ae); archive_entry_set_filetype(ae, AE_IFREG); assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae)); assert(archive_error_string(a) != NULL); archive_entry_free(ae); /* * Write a file with a name and size but no filetype. */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "name"); archive_entry_set_size(ae, 0); assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae)); assert(archive_error_string(a) != NULL); archive_entry_free(ae); /* * Write a file with a size and filetype but no name. */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_size(ae, 0); archive_entry_set_filetype(ae, AE_IFREG); assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae)); assert(archive_error_string(a) != NULL); archive_entry_free(ae); /* * Write a directory to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_mtime(ae, 11, 110); archive_entry_copy_pathname(ae, "dir"); archive_entry_set_mode(ae, S_IFDIR | 0755); archive_entry_set_size(ae, 512); assertA(0 == archive_write_header(a, ae)); assertEqualInt(0, archive_entry_size(ae)); archive_entry_free(ae); assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9)); /* Close out the archive. */ assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* * Damage the second entry to test the search-ahead recovery. * TODO: Move the damage-recovery checking to a separate test; * it doesn't really belong in this write test. */ { int i; for (i = 80; i < 150; i++) { if (memcmp(buff + i, "07070", 5) == 0) { damaged = 1; buff[i] = 'X'; break; } } } failure("Unable to locate the second header for damage-recovery test."); assert(damaged == 1); /* * Now, read the data back. */ assert((a = archive_read_new()) != NULL); assertA(0 == archive_read_support_format_all(a)); assertA(0 == archive_read_support_filter_all(a)); assertA(0 == archive_read_open_memory(a, buff, used)); if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae))) { archive_read_free(a); return; } assertEqualInt(1, archive_entry_mtime(ae)); /* Not the same as above: cpio doesn't store hi-res times. */ assert(0 == archive_entry_mtime_nsec(ae)); assert(0 == archive_entry_atime(ae)); assert(0 == archive_entry_ctime(ae)); assertEqualString("file", archive_entry_pathname(ae)); assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae)); assertEqualInt(8, archive_entry_size(ae)); assertA(8 == archive_read_data(a, filedata, 10)); assertEqualMem(filedata, "12345678", 8); /* * The second file can't be read because we damaged its header. */ /* * Read the third file back. * ARCHIVE_WARN here because the damaged entry was skipped. */ assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); assertEqualString("name", archive_entry_pathname(ae)); /* * Read the dir entry back. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt(11, archive_entry_mtime(ae)); assert(0 == archive_entry_mtime_nsec(ae)); assert(0 == archive_entry_atime(ae)); assert(0 == archive_entry_ctime(ae)); assertEqualString("dir", archive_entry_pathname(ae)); assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae)); assertEqualInt(0, archive_entry_size(ae)); assertEqualIntA(a, 0, archive_read_data(a, filedata, 10)); /* Verify the end of the archive. */ assertEqualIntA(a, 1, archive_read_next_header(a, &ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); free(buff); }
static void test_linkify_tar(void) { struct archive_entry *entry, *e2; struct archive_entry_linkresolver *resolver; /* Initialize the resolver. */ assert(NULL != (resolver = archive_entry_linkresolver_new())); archive_entry_linkresolver_set_strategy(resolver, ARCHIVE_FORMAT_TAR_USTAR); /* Create an entry with only 1 link and try to linkify it. */ assert(NULL != (entry = archive_entry_new())); archive_entry_set_pathname(entry, "test1"); archive_entry_set_ino(entry, 1); archive_entry_set_dev(entry, 2); archive_entry_set_nlink(entry, 1); archive_entry_set_size(entry, 10); archive_entry_linkify(resolver, &entry, &e2); /* Shouldn't have been changed. */ assert(e2 == NULL); assertEqualInt(10, archive_entry_size(entry)); assertEqualString("test1", archive_entry_pathname(entry)); /* Now, try again with an entry that has 2 links. */ archive_entry_set_pathname(entry, "test2"); archive_entry_set_nlink(entry, 2); archive_entry_set_ino(entry, 2); archive_entry_linkify(resolver, &entry, &e2); /* Shouldn't be altered, since it wasn't seen before. */ assert(e2 == NULL); assertEqualString("test2", archive_entry_pathname(entry)); assertEqualString(NULL, archive_entry_hardlink(entry)); assertEqualInt(10, archive_entry_size(entry)); /* Match again and make sure it does get altered. */ archive_entry_linkify(resolver, &entry, &e2); assert(e2 == NULL); assertEqualString("test2", archive_entry_pathname(entry)); assertEqualString("test2", archive_entry_hardlink(entry)); assertEqualInt(0, archive_entry_size(entry)); /* Dirs should never be matched as hardlinks, regardless. */ archive_entry_set_pathname(entry, "test3"); archive_entry_set_nlink(entry, 2); archive_entry_set_filetype(entry, AE_IFDIR); archive_entry_set_ino(entry, 3); archive_entry_set_hardlink(entry, NULL); archive_entry_linkify(resolver, &entry, &e2); /* Shouldn't be altered, since it wasn't seen before. */ assert(e2 == NULL); assertEqualString("test3", archive_entry_pathname(entry)); assertEqualString(NULL, archive_entry_hardlink(entry)); /* Dir, so it shouldn't get matched. */ archive_entry_linkify(resolver, &entry, &e2); assert(e2 == NULL); assertEqualString("test3", archive_entry_pathname(entry)); assertEqualString(NULL, archive_entry_hardlink(entry)); archive_entry_free(entry); archive_entry_linkresolver_free(resolver); }