Skip to content

Commit

Permalink
Add support for creating packages with a small hash in the filename
Browse files Browse the repository at this point in the history
This is work is to support making mirroring packages easier and avoiding the
bug where we atomically switch a new package set in while someone is
downloading from the set.
  • Loading branch information
brd committed Dec 1, 2017
1 parent ab5d49c commit b9f1e05
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion libpkg/pkg.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ int pkg_create_from_manifest(const char *, pkg_formats, const char *,
/**
* Create package from stage install with a metadata directory
*/
int pkg_create_staged(const char *, pkg_formats, const char *, const char *, char *);
int pkg_create_staged(const char *, pkg_formats, const char *, const char *, char *, bool);
int pkg_load_metadata(struct pkg *, const char *, const char *, const char *, const char *, bool);

/**
Expand Down
28 changes: 26 additions & 2 deletions libpkg/pkg_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,10 @@ pkg_load_metadata(struct pkg *pkg, const char *mfile, const char *md_dir,

int
pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir,
const char *md_dir, char *plist)
const char *md_dir, char *plist, bool hash)
{
char hash_dest[MAXPATHLEN];
char filename[MAXPATHLEN];
struct pkg *pkg = NULL;
struct pkg_file *file = NULL;
struct pkg_dir *dir = NULL;
Expand Down Expand Up @@ -430,8 +432,30 @@ pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir,
}

cleanup:
free(pkg);
packing_finish(pkg_archive);
if (hash && ret == EPKG_OK) {
/* Find the hash and rename the file and create a symlink */
pkg_snprintf(filename, sizeof(filename), "%n-%v.%S",
pkg, pkg, packing_format_to_string(format));
pkg->sum = pkg_checksum_file(filename,
PKG_HASH_TYPE_SHA256_HEX);
pkg_snprintf(hash_dest, sizeof(hash_dest), "%n-%v-%z.%S",
pkg, pkg, pkg, packing_format_to_string(format));

pkg_debug(1, "Rename the pkg file from: %s to: %s",
filename, hash_dest);

if (rename(filename, hash_dest) == -1) {
pkg_emit_errno("rename", hash_dest);
unlink(hash_dest);
return (EPKG_FATAL);
}
if (symlink(hash_dest, filename) == -1) {
pkg_emit_errno("symlink", hash_dest);
return (EPKG_FATAL);
}
}
free(pkg);
return (ret);
}

Expand Down
1 change: 1 addition & 0 deletions libpkg/private/pkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ int packing_append_buffer(struct packing *pack, const char *buffer,
const char *path, int size);
int packing_append_tree(struct packing *pack, const char *treepath,
const char *newroot);
void packing_get_filename(struct packing *pack, const char *filename);
void packing_finish(struct packing *pack);
pkg_formats packing_format_from_string(const char *str);
const char* packing_format_to_string(pkg_formats format);
Expand Down
52 changes: 29 additions & 23 deletions src/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ struct pkg_entry *pkg_head = NULL;
void
usage_create(void)
{
fprintf(stderr, "Usage: pkg create [-Onqv] [-f format] [-o outdir] "
fprintf(stderr, "Usage: pkg create [-Ohnqv] [-f format] [-o outdir] "
"[-p plist] [-r rootdir] -m metadatadir\n");
fprintf(stderr, "Usage: pkg create [-Onqv] [-f format] [-o outdir] "
fprintf(stderr, "Usage: pkg create [-Ohnqv] [-f format] [-o outdir] "
"[-r rootdir] -M manifest\n");
fprintf(stderr, " pkg create [-Ognqvx] [-f format] [-o outdir] "
fprintf(stderr, " pkg create [-Ohgnqvx] [-f format] [-o outdir] "
"[-r rootdir] pkg-name ...\n");
fprintf(stderr, " pkg create [-Onqv] [-f format] [-o outdir] "
fprintf(stderr, " pkg create [-Ohnqv] [-f format] [-o outdir] "
"[-r rootdir] -a\n\n");
fprintf(stderr, "For more information see 'pkg help create'.\n");
}
Expand Down Expand Up @@ -179,14 +179,15 @@ pkg_create_matches(int argc, char **argv, match_t match, pkg_formats fmt,

/*
* options:
* -x: regex
* -g: globbing
* -r: rootdir for the package
* -m: path to dir where to find the metadata
* -q: quiet mode
* -M: manifest file
* -f <format>: format could be txz, tgz, tbz or tar
* -g: globbing
* -h: pkg name with hash and symlink
* -m: path to dir where to find the metadata
* -o: output directory where to create packages by default ./ is used
* -q: quiet mode
* -r: rootdir for the package
* -x: regex
*/

int
Expand All @@ -202,6 +203,7 @@ exec_create(int argc, char **argv)
pkg_formats fmt;
int ch;
bool overwrite = true;
bool hash = false;


/* POLA: pkg create is quiet by default, unless
Expand All @@ -212,39 +214,34 @@ exec_create(int argc, char **argv)

struct option longopts[] = {
{ "all", no_argument, NULL, 'a' },
{ "format", required_argument, NULL, 'f' },
{ "glob", no_argument, NULL, 'g' },
{ "hash", no_argument, NULL, 'h' },
{ "regex", no_argument, NULL, 'x' },
{ "format", required_argument, NULL, 'f' },
{ "root-dir", required_argument, NULL, 'r' },
{ "metadata", required_argument, NULL, 'm' },
{ "manifest", required_argument, NULL, 'M' },
{ "out-dir", required_argument, NULL, 'o' },
{ "no-clobber", no_argument, NULL, 'n' },
{ "out-dir", required_argument, NULL, 'o' },
{ "plist", required_argument, NULL, 'p' },
{ "quiet", no_argument, NULL, 'q' },
{ "verbose", no_argument, NULL, 'v' },
{ NULL, 0, NULL, 0 },
};

while ((ch = getopt_long(argc, argv, "+agxf:r:m:M:o:np:qv", longopts, NULL)) != -1) {
while ((ch = getopt_long(argc, argv, "+aghxf:r:m:M:o:np:qv", longopts, NULL)) != -1) {
switch (ch) {
case 'a':
match = MATCH_ALL;
break;
case 'g':
match = MATCH_GLOB;
break;
case 'x':
match = MATCH_REGEX;
break;
case 'f':
format = optarg;
break;
case 'o':
outdir = optarg;
case 'g':
match = MATCH_GLOB;
break;
case 'r':
rootdir = optarg;
case 'h':
hash = true;
break;
case 'm':
metadatadir = optarg;
Expand All @@ -255,15 +252,24 @@ exec_create(int argc, char **argv)
case 'n':
overwrite = false;
break;
case 'o':
outdir = optarg;
break;
case 'p':
plist = optarg;
break;
case 'q':
quiet = true;
break;
case 'r':
rootdir = optarg;
break;
case 'v':
quiet = false;
break;
case 'x':
match = MATCH_REGEX;
break;
default:
usage_create();
return (EX_USAGE);
Expand Down Expand Up @@ -312,7 +318,7 @@ exec_create(int argc, char **argv)
overwrite) == EPKG_OK ? EX_OK : EX_SOFTWARE);
} else if (metadatadir != NULL) {
return (pkg_create_staged(outdir, fmt, rootdir, metadatadir,
plist) == EPKG_OK ? EX_OK : EX_SOFTWARE);
plist, hash) == EPKG_OK ? EX_OK : EX_SOFTWARE);
} else { /* (manifest != NULL) */
return (pkg_create_from_manifest(outdir, fmt, rootdir,
manifest, plist) == EPKG_OK ? EX_OK : EX_SOFTWARE);
Expand Down
14 changes: 14 additions & 0 deletions tests/frontend/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests_init \
create_from_plist_with_keyword_arguments \
create_from_manifest_and_plist \
create_from_plist_pkg_descr \
create_from_plist_hash \
create_from_plist_with_keyword_and_message

genmanifest() {
Expand Down Expand Up @@ -391,6 +392,19 @@ message upgrade

}

create_from_plist_hash_body() {
touch file1
genmanifest
genplist "file1"

atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg create -h -o ${TMPDIR} -m . -p test.plist -r .

}

create_from_plist_with_keyword_and_message_body() {
genmanifest
genplist "@showmsg plop"
Expand Down

0 comments on commit b9f1e05

Please sign in to comment.