Line data Source code
1 : /*-
2 : * Copyright (c) 2012-2013 Baptiste Daroussin <bapt@FreeBSD.org>
3 : * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
4 : * Copyright (c) 2014 Matthew Seaman <matthew@FreeBSD.org>
5 : * All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer
12 : * in this position and unchanged.
13 : * 2. Redistributions in binary form must reproduce the above copyright
14 : * notice, this list of conditions and the following disclaimer in the
15 : * documentation and/or other materials provided with the distribution.
16 : *
17 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 : * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 : */
28 :
29 : #include <sys/param.h>
30 : #include <sys/stat.h>
31 : #include <sys/sbuf.h>
32 :
33 : #include <err.h>
34 : #include <errno.h>
35 : #include <getopt.h>
36 : #include <string.h>
37 : #include <sysexits.h>
38 : #include <dirent.h>
39 : #include <unistd.h>
40 :
41 : #include <pkg.h>
42 :
43 : #include <bsd_compat.h>
44 :
45 : #include "pkgcli.h"
46 :
47 : void
48 0 : usage_convert(void)
49 : {
50 0 : fprintf(stderr, "Usage: pkg convert [-d dir] [-n]\n\n");
51 0 : fprintf(stderr, "For more information see 'pkg help convert'.\n");
52 0 : }
53 :
54 : static int
55 0 : convert_from_old(const char *pkg_add_dbdir, bool dry_run)
56 : {
57 : DIR *d;
58 : struct dirent *dp;
59 0 : struct pkg *p = NULL;
60 : char path[MAXPATHLEN];
61 0 : struct pkgdb *db = NULL;
62 : struct stat sb;
63 0 : int lock_type = PKGDB_LOCK_EXCLUSIVE;
64 : int ret;
65 :
66 0 : if (dry_run)
67 0 : ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_LOCAL);
68 : else
69 0 : ret = pkgdb_access(PKGDB_MODE_READ|PKGDB_MODE_WRITE|
70 : PKGDB_MODE_CREATE, PKGDB_DB_LOCAL);
71 :
72 0 : if (ret == EPKG_ENOACCESS) {
73 0 : warnx("Insufficient privileges to convert packages");
74 0 : return (EX_NOPERM);
75 0 : } else if (ret != EPKG_OK && ret != EPKG_ENODB) {
76 0 : warnx("Error accessing the package database");
77 0 : return (EX_SOFTWARE);
78 : }
79 :
80 0 : if ((d = opendir(pkg_add_dbdir)) == NULL)
81 0 : err(EX_NOINPUT, "%s", pkg_add_dbdir);
82 :
83 0 : if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
84 0 : closedir(d);
85 0 : return (EX_IOERR);
86 : }
87 0 : if (dry_run)
88 0 : lock_type = PKGDB_LOCK_READONLY;
89 0 : if (pkgdb_obtain_lock(db, lock_type) != EPKG_OK) {
90 0 : pkgdb_close(db);
91 0 : warnx("Cannot get an advisory lock on a database, it is locked"
92 : " by another process");
93 0 : return (EX_TEMPFAIL);
94 : }
95 0 : while ((dp = readdir(d)) != NULL) {
96 0 : if (fstatat(dirfd(d), dp->d_name, &sb, 0) == 0 &&
97 0 : S_ISDIR(sb.st_mode)) {
98 0 : if (strcmp(dp->d_name, ".") == 0 ||
99 0 : strcmp(dp->d_name, "..") == 0)
100 0 : continue;
101 0 : if (p != NULL)
102 0 : pkg_free(p);
103 0 : if (pkg_new(&p, PKG_OLD_FILE) != EPKG_OK)
104 0 : err(EX_OSERR, "malloc");
105 0 : printf("Converting %s...\n", dp->d_name);
106 0 : snprintf(path, sizeof(path), "%s/%s", pkg_add_dbdir, dp->d_name);
107 0 : if (pkg_old_load_from_path(p, path) != EPKG_OK) {
108 0 : fprintf(stderr, "Skipping invalid package: %s\n", path);
109 0 : continue;
110 : }
111 0 : pkg_from_old(p);
112 0 : if (!dry_run)
113 0 : pkgdb_register_ports(db, p);
114 : }
115 : }
116 :
117 0 : pkg_free(p);
118 0 : pkgdb_release_lock(db, lock_type);
119 0 : pkgdb_close(db);
120 0 : closedir(d);
121 0 : return (EX_OK);
122 : }
123 :
124 : int
125 0 : exec_convert(__unused int argc, __unused char **argv)
126 : {
127 : int ch;
128 0 : bool dry_run = false;
129 0 : const char *pkg_add_dbdir = "/var/db/pkg";
130 :
131 0 : struct option longopts[] = {
132 : { "pkg-dbdir", required_argument, NULL, 'd' },
133 : { "dry-run", no_argument, NULL, 'n' },
134 : { NULL, 0, NULL, 0 },
135 : };
136 :
137 0 : while ((ch = getopt_long(argc, argv, "+d:n", longopts, NULL)) != -1) {
138 0 : switch (ch) {
139 : case 'd':
140 0 : pkg_add_dbdir = optarg;
141 0 : break;
142 : case 'n':
143 0 : dry_run = true;
144 0 : break;
145 : default:
146 0 : usage_convert();
147 0 : return (EX_USAGE);
148 : }
149 : }
150 0 : argc -= optind;
151 0 : argv += optind;
152 :
153 0 : if (argc > 1) {
154 0 : usage_convert();
155 0 : return (EX_USAGE);
156 : }
157 :
158 0 : printf("Converting packages from %s\n", pkg_add_dbdir);
159 :
160 0 : return (convert_from_old(pkg_add_dbdir, dry_run));
161 : }
|