4 // Copyright (c) 2000-2005 Per Liden
5 // Copyright (c) 2006-2010 by CRUX team (http://crux.nu)
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
32 #include <ext/stdio_filebuf.h>
35 #include <sys/types.h>
39 #include <sys/param.h>
44 #include <archive_entry.h>
46 #define INIT_ARCHIVE(ar) \
47 archive_read_support_filter_gzip((ar)); \
48 archive_read_support_filter_bzip2((ar)); \
49 archive_read_support_filter_xz((ar)); \
50 archive_read_support_format_tar((ar))
52 #define DEFAULT_BYTES_PER_BLOCK (20 * 512)
54 using __gnu_cxx::stdio_filebuf
;
56 pkgutil::pkgutil(const string
& name
)
61 memset(&sa
, 0, sizeof(sa
));
62 sa
.sa_handler
= SIG_IGN
;
63 sigaction(SIGHUP
, &sa
, 0);
64 sigaction(SIGINT
, &sa
, 0);
65 sigaction(SIGQUIT
, &sa
, 0);
66 sigaction(SIGTERM
, &sa
, 0);
69 void pkgutil::db_open(const string
& path
)
72 root
= trim_filename(path
+ "/");
73 const string filename
= root
+ PKG_DB
;
75 int fd
= open(filename
.c_str(), O_RDONLY
);
77 throw runtime_error_with_errno("could not open " + filename
);
79 stdio_filebuf
<char> filebuf(fd
, ios::in
, getpagesize());
82 throw runtime_error_with_errno("could not read " + filename
);
89 getline(in
, info
.version
);
95 break; // End of record
97 info
.files
.insert(info
.files
.end(), file
);
99 if (!info
.files
.empty())
100 packages
[name
] = info
;
104 cerr
<< packages
.size() << " packages found in database" << endl
;
108 void pkgutil::db_commit()
110 const string dbfilename
= root
+ PKG_DB
;
111 const string dbfilename_new
= dbfilename
+ ".incomplete_transaction";
112 const string dbfilename_bak
= dbfilename
+ ".backup";
114 // Remove failed transaction (if it exists)
115 if (unlink(dbfilename_new
.c_str()) == -1 && errno
!= ENOENT
)
116 throw runtime_error_with_errno("could not remove " + dbfilename_new
);
118 // Write new database
119 int fd_new
= creat(dbfilename_new
.c_str(), 0444);
121 throw runtime_error_with_errno("could not create " + dbfilename_new
);
123 stdio_filebuf
<char> filebuf_new(fd_new
, ios::out
, getpagesize());
124 ostream
db_new(&filebuf_new
);
125 for (packages_t::const_iterator i
= packages
.begin(); i
!= packages
.end(); ++i
) {
126 if (!i
->second
.files
.empty()) {
127 db_new
<< i
->first
<< "\n";
128 db_new
<< i
->second
.version
<< "\n";
129 copy(i
->second
.files
.begin(), i
->second
.files
.end(), ostream_iterator
<string
>(db_new
, "\n"));
136 // Make sure the new database was successfully written
138 throw runtime_error("could not write " + dbfilename_new
);
140 // Synchronize file to disk
141 if (fsync(fd_new
) == -1)
142 throw runtime_error_with_errno("could not synchronize " + dbfilename_new
);
144 // Relink database backup
145 if (unlink(dbfilename_bak
.c_str()) == -1 && errno
!= ENOENT
)
146 throw runtime_error_with_errno("could not remove " + dbfilename_bak
);
147 if (link(dbfilename
.c_str(), dbfilename_bak
.c_str()) == -1)
148 throw runtime_error_with_errno("could not create " + dbfilename_bak
);
150 // Move new database into place
151 if (rename(dbfilename_new
.c_str(), dbfilename
.c_str()) == -1)
152 throw runtime_error_with_errno("could not rename " + dbfilename_new
+ " to " + dbfilename
);
155 cerr
<< packages
.size() << " packages written to database" << endl
;
159 void pkgutil::db_add_pkg(const string
& name
, const pkginfo_t
& info
)
161 packages
[name
] = info
;
164 bool pkgutil::db_find_pkg(const string
& name
)
166 return (packages
.find(name
) != packages
.end());
169 void pkgutil::db_rm_pkg(const string
& name
)
171 set
<string
> files
= packages
[name
].files
;
172 packages
.erase(name
);
175 cerr
<< "Removing package phase 1 (all files in package):" << endl
;
176 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
180 // Don't delete files that still have references
181 for (packages_t::const_iterator i
= packages
.begin(); i
!= packages
.end(); ++i
)
182 for (set
<string
>::const_iterator j
= i
->second
.files
.begin(); j
!= i
->second
.files
.end(); ++j
)
186 cerr
<< "Removing package phase 2 (files that still have references excluded):" << endl
;
187 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
192 for (set
<string
>::const_reverse_iterator i
= files
.rbegin(); i
!= files
.rend(); ++i
) {
193 const string filename
= root
+ *i
;
194 if (file_exists(filename
) && remove(filename
.c_str()) == -1) {
195 const char* msg
= strerror(errno
);
196 cerr
<< utilname
<< ": could not remove " << filename
<< ": " << msg
<< endl
;
201 void pkgutil::db_rm_pkg(const string
& name
, const set
<string
>& keep_list
)
203 set
<string
> files
= packages
[name
].files
;
204 packages
.erase(name
);
207 cerr
<< "Removing package phase 1 (all files in package):" << endl
;
208 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
212 // Don't delete files found in the keep list
213 for (set
<string
>::const_iterator i
= keep_list
.begin(); i
!= keep_list
.end(); ++i
)
217 cerr
<< "Removing package phase 2 (files that is in the keep list excluded):" << endl
;
218 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
222 // Don't delete files that still have references
223 for (packages_t::const_iterator i
= packages
.begin(); i
!= packages
.end(); ++i
)
224 for (set
<string
>::const_iterator j
= i
->second
.files
.begin(); j
!= i
->second
.files
.end(); ++j
)
228 cerr
<< "Removing package phase 3 (files that still have references excluded):" << endl
;
229 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
234 for (set
<string
>::const_reverse_iterator i
= files
.rbegin(); i
!= files
.rend(); ++i
) {
235 const string filename
= root
+ *i
;
236 if (file_exists(filename
) && remove(filename
.c_str()) == -1) {
237 if (errno
== ENOTEMPTY
)
239 const char* msg
= strerror(errno
);
240 cerr
<< utilname
<< ": could not remove " << filename
<< ": " << msg
<< endl
;
245 void pkgutil::db_rm_files(set
<string
> files
, const set
<string
>& keep_list
)
247 // Remove all references
248 for (packages_t::iterator i
= packages
.begin(); i
!= packages
.end(); ++i
)
249 for (set
<string
>::const_iterator j
= files
.begin(); j
!= files
.end(); ++j
)
250 i
->second
.files
.erase(*j
);
253 cerr
<< "Removing files:" << endl
;
254 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
258 // Don't delete files found in the keep list
259 for (set
<string
>::const_iterator i
= keep_list
.begin(); i
!= keep_list
.end(); ++i
)
263 for (set
<string
>::const_reverse_iterator i
= files
.rbegin(); i
!= files
.rend(); ++i
) {
264 const string filename
= root
+ *i
;
265 if (file_exists(filename
) && remove(filename
.c_str()) == -1) {
266 if (errno
== ENOTEMPTY
)
268 const char* msg
= strerror(errno
);
269 cerr
<< utilname
<< ": could not remove " << filename
<< ": " << msg
<< endl
;
274 set
<string
> pkgutil::db_find_conflicts(const string
& name
, const pkginfo_t
& info
)
278 // Find conflicting files in database
279 for (packages_t::const_iterator i
= packages
.begin(); i
!= packages
.end(); ++i
) {
280 if (i
->first
!= name
) {
281 set_intersection(info
.files
.begin(), info
.files
.end(),
282 i
->second
.files
.begin(), i
->second
.files
.end(),
283 inserter(files
, files
.end()));
288 cerr
<< "Conflicts phase 1 (conflicts in database):" << endl
;
289 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
293 // Find conflicting files in filesystem
294 for (set
<string
>::iterator i
= info
.files
.begin(); i
!= info
.files
.end(); ++i
) {
295 const string filename
= root
+ *i
;
296 if (file_exists(filename
) && files
.find(*i
) == files
.end())
297 files
.insert(files
.end(), *i
);
301 cerr
<< "Conflicts phase 2 (conflicts in filesystem added):" << endl
;
302 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
306 // Exclude directories
307 set
<string
> tmp
= files
;
308 for (set
<string
>::const_iterator i
= tmp
.begin(); i
!= tmp
.end(); ++i
) {
309 if ((*i
)[i
->length() - 1] == '/')
314 cerr
<< "Conflicts phase 3 (directories excluded):" << endl
;
315 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
319 // If this is an upgrade, remove files already owned by this package
320 if (packages
.find(name
) != packages
.end()) {
321 for (set
<string
>::const_iterator i
= packages
[name
].files
.begin(); i
!= packages
[name
].files
.end(); ++i
)
325 cerr
<< "Conflicts phase 4 (files already owned by this package excluded):" << endl
;
326 copy(files
.begin(), files
.end(), ostream_iterator
<string
>(cerr
, "\n"));
334 pair
<string
, pkgutil::pkginfo_t
> pkgutil::pkg_open(const string
& filename
) const
336 pair
<string
, pkginfo_t
> result
;
338 struct archive
* archive
;
339 struct archive_entry
* entry
;
341 // Extract name and version from filename
342 string
basename(filename
, filename
.rfind('/') + 1);
343 string
name(basename
, 0, basename
.find(VERSION_DELIM
));
344 string
version(basename
, 0, basename
.rfind(PKG_EXT
));
345 version
.erase(0, version
.find(VERSION_DELIM
) == string::npos
? string::npos
: version
.find(VERSION_DELIM
) + 1);
347 if (name
.empty() || version
.empty())
348 throw runtime_error("could not determine name and/or version of " + basename
+ ": Invalid package name");
351 result
.second
.version
= version
;
353 archive
= archive_read_new();
354 INIT_ARCHIVE(archive
);
356 if (archive_read_open_filename(archive
,
358 DEFAULT_BYTES_PER_BLOCK
) != ARCHIVE_OK
)
359 throw runtime_error_with_errno("could not open " + filename
, archive_errno(archive
));
361 for (i
= 0; archive_read_next_header(archive
, &entry
) ==
364 result
.second
.files
.insert(result
.second
.files
.end(),
365 archive_entry_pathname(entry
));
367 mode_t mode
= archive_entry_mode(entry
);
370 archive_read_data_skip(archive
) != ARCHIVE_OK
)
371 throw runtime_error_with_errno("could not read " + filename
, archive_errno(archive
));
375 if (archive_errno(archive
) == 0)
376 throw runtime_error("empty package");
378 throw runtime_error("could not read " + filename
);
381 archive_read_free(archive
);
386 void pkgutil::pkg_install(const string
& filename
, const set
<string
>& keep_list
, const set
<string
>& non_install_list
) const
388 struct archive
* archive
;
389 struct archive_entry
* entry
;
394 archive
= archive_read_new();
395 INIT_ARCHIVE(archive
);
397 if (archive_read_open_filename(archive
,
399 DEFAULT_BYTES_PER_BLOCK
) != ARCHIVE_OK
)
400 throw runtime_error_with_errno("could not open " + filename
, archive_errno(archive
));
403 absroot
= getcwd(buf
, sizeof(buf
));
405 for (i
= 0; archive_read_next_header(archive
, &entry
) ==
407 string archive_filename
= archive_entry_pathname(entry
);
408 string reject_dir
= trim_filename(absroot
+ string("/") + string(PKG_REJECTED
));
409 string original_filename
= trim_filename(absroot
+ string("/") + archive_filename
);
410 string real_filename
= original_filename
;
412 // Check if file is filtered out via INSTALL
413 if (non_install_list
.find(archive_filename
) != non_install_list
.end()) {
416 cout
<< utilname
<< ": ignoring " << archive_filename
<< endl
;
418 mode
= archive_entry_mode(entry
);
421 archive_read_data_skip(archive
);
426 // Check if file should be rejected
427 if (file_exists(real_filename
) && keep_list
.find(archive_filename
) != keep_list
.end())
428 real_filename
= trim_filename(reject_dir
+ string("/") + archive_filename
);
430 archive_entry_set_pathname(entry
, const_cast<char*>
431 (real_filename
.c_str()));
434 unsigned int flags
= ARCHIVE_EXTRACT_OWNER
| ARCHIVE_EXTRACT_PERM
| ARCHIVE_EXTRACT_TIME
| ARCHIVE_EXTRACT_UNLINK
;
436 if (archive_read_extract(archive
, entry
, flags
) != ARCHIVE_OK
) {
437 // If a file fails to install we just print an error message and
438 // continue trying to install the rest of the package.
439 const char* msg
= archive_error_string(archive
);
440 cerr
<< utilname
<< ": could not install " + archive_filename
<< ": " << msg
<< endl
;
444 // Check rejected file
445 if (real_filename
!= original_filename
) {
446 bool remove_file
= false;
447 mode_t mode
= archive_entry_mode(entry
);
451 remove_file
= permissions_equal(real_filename
, original_filename
);
454 remove_file
= permissions_equal(real_filename
, original_filename
) &&
455 (file_empty(real_filename
) || file_equal(real_filename
, original_filename
));
457 // Remove rejected file or signal about its existence
459 file_remove(reject_dir
, real_filename
);
461 cout
<< utilname
<< ": rejecting " << archive_filename
<< ", keeping existing version" << endl
;
466 if (archive_errno(archive
) == 0)
467 throw runtime_error("empty package");
469 throw runtime_error("could not read " + filename
);
472 archive_read_free(archive
);
475 void pkgutil::ldconfig() const
477 // Only execute ldconfig if /etc/ld.so.conf exists
478 if (file_exists(root
+ LDCONFIG_CONF
)) {
482 throw runtime_error_with_errno("fork() failed");
485 execl(LDCONFIG
, LDCONFIG
, "-r", root
.c_str(), (char *) 0);
486 const char* msg
= strerror(errno
);
487 cerr
<< utilname
<< ": could not execute " << LDCONFIG
<< ": " << msg
<< endl
;
490 if (waitpid(pid
, 0, 0) == -1)
491 throw runtime_error_with_errno("waitpid() failed");
496 void pkgutil::pkg_footprint(string
& filename
) const
499 struct archive
* archive
;
500 struct archive_entry
* entry
;
502 map
<string
, mode_t
> hardlink_target_modes
;
504 // We first do a run over the archive and remember the modes
506 // In the second run, we print the footprint - using the stored
507 // modes for hardlinks.
509 // FIXME the code duplication here is butt ugly
510 archive
= archive_read_new();
511 INIT_ARCHIVE(archive
);
513 if (archive_read_open_filename(archive
,
515 DEFAULT_BYTES_PER_BLOCK
) != ARCHIVE_OK
)
516 throw runtime_error_with_errno("could not open " + filename
, archive_errno(archive
));
518 for (i
= 0; archive_read_next_header(archive
, &entry
) ==
521 mode_t mode
= archive_entry_mode(entry
);
523 if (!archive_entry_hardlink(entry
)) {
524 const char *s
= archive_entry_pathname(entry
);
526 hardlink_target_modes
[s
] = mode
;
529 if (S_ISREG(mode
) && archive_read_data_skip(archive
))
530 throw runtime_error_with_errno("could not read " + filename
, archive_errno(archive
));
533 archive_read_free(archive
);
535 // Too bad, there doesn't seem to be a way to reuse our archive
537 archive
= archive_read_new();
538 INIT_ARCHIVE(archive
);
540 if (archive_read_open_filename(archive
,
542 DEFAULT_BYTES_PER_BLOCK
) != ARCHIVE_OK
)
543 throw runtime_error_with_errno("could not open " + filename
, archive_errno(archive
));
545 for (i
= 0; archive_read_next_header(archive
, &entry
) ==
547 mode_t mode
= archive_entry_mode(entry
);
549 // Access permissions
551 // Access permissions on symlinks differ among filesystems, e.g. XFS and ext2 have different.
552 // To avoid getting different footprints we always use "lrwxrwxrwx".
553 cout
<< "lrwxrwxrwx";
555 const char *h
= archive_entry_hardlink(entry
);
558 cout
<< mtos(hardlink_target_modes
[h
]);
566 uid_t uid
= archive_entry_uid(entry
);
567 struct passwd
* pw
= getpwuid(uid
);
576 gid_t gid
= archive_entry_gid(entry
);
577 struct group
* gr
= getgrgid(gid
);
584 cout
<< '\t' << archive_entry_pathname(entry
);
589 cout
<< " -> " << archive_entry_symlink(entry
);
590 } else if (S_ISCHR(mode
) ||
593 cout
<< " (" << archive_entry_rdevmajor(entry
)
594 << ", " << archive_entry_rdevminor(entry
)
596 } else if (S_ISREG(mode
) &&
597 archive_entry_size(entry
) == 0) {
598 // Empty regular file
604 if (S_ISREG(mode
) && archive_read_data_skip(archive
))
605 throw runtime_error_with_errno("could not read " + filename
, archive_errno(archive
));
609 if (archive_errno(archive
) == 0)
610 throw runtime_error("empty package");
612 throw runtime_error("could not read " + filename
);
615 archive_read_free(archive
);
618 void pkgutil::print_version() const
620 cout
<< utilname
<< " (pkgutils) " << VERSION
<< endl
;
623 db_lock::db_lock(const string
& root
, bool exclusive
)
626 const string dirname
= trim_filename(root
+ string("/") + PKG_DIR
);
628 if (!(dir
= opendir(dirname
.c_str())))
629 throw runtime_error_with_errno("could not read directory " + dirname
);
631 if (flock(dirfd(dir
), (exclusive
? LOCK_EX
: LOCK_SH
) | LOCK_NB
) == -1) {
632 if (errno
== EWOULDBLOCK
)
633 throw runtime_error("package database is currently locked by another process");
635 throw runtime_error_with_errno("could not lock directory " + dirname
);
642 flock(dirfd(dir
), LOCK_UN
);
647 void assert_argument(char** argv
, int argc
, int index
)
649 if (argc
- 1 < index
+ 1)
650 throw runtime_error("option " + string(argv
[index
]) + " requires an argument");
653 string
itos(unsigned int value
)
656 sprintf(buf
, "%u", value
);
660 string
mtos(mode_t mode
)
665 switch (mode
& S_IFMT
) {
666 case S_IFREG
: s
+= '-'; break; // Regular
667 case S_IFDIR
: s
+= 'd'; break; // Directory
668 case S_IFLNK
: s
+= 'l'; break; // Symbolic link
669 case S_IFCHR
: s
+= 'c'; break; // Character special
670 case S_IFBLK
: s
+= 'b'; break; // Block special
671 case S_IFSOCK
: s
+= 's'; break; // Socket
672 case S_IFIFO
: s
+= 'p'; break; // Fifo
673 default: s
+= '?'; break; // Unknown
677 s
+= (mode
& S_IRUSR
) ? 'r' : '-';
678 s
+= (mode
& S_IWUSR
) ? 'w' : '-';
679 switch (mode
& (S_IXUSR
| S_ISUID
)) {
680 case S_IXUSR
: s
+= 'x'; break;
681 case S_ISUID
: s
+= 'S'; break;
682 case S_IXUSR
| S_ISUID
: s
+= 's'; break;
683 default: s
+= '-'; break;
687 s
+= (mode
& S_IRGRP
) ? 'r' : '-';
688 s
+= (mode
& S_IWGRP
) ? 'w' : '-';
689 switch (mode
& (S_IXGRP
| S_ISGID
)) {
690 case S_IXGRP
: s
+= 'x'; break;
691 case S_ISGID
: s
+= 'S'; break;
692 case S_IXGRP
| S_ISGID
: s
+= 's'; break;
693 default: s
+= '-'; break;
697 s
+= (mode
& S_IROTH
) ? 'r' : '-';
698 s
+= (mode
& S_IWOTH
) ? 'w' : '-';
699 switch (mode
& (S_IXOTH
| S_ISVTX
)) {
700 case S_IXOTH
: s
+= 'x'; break;
701 case S_ISVTX
: s
+= 'T'; break;
702 case S_IXOTH
| S_ISVTX
: s
+= 't'; break;
703 default: s
+= '-'; break;
709 string
trim_filename(const string
& filename
)
712 string result
= filename
;
714 for (string::size_type pos
= result
.find(search
); pos
!= string::npos
; pos
= result
.find(search
))
715 result
.replace(pos
, search
.size(), "/");
720 bool file_exists(const string
& filename
)
723 return !lstat(filename
.c_str(), &buf
);
726 bool file_empty(const string
& filename
)
730 if (lstat(filename
.c_str(), &buf
) == -1)
733 return (S_ISREG(buf
.st_mode
) && buf
.st_size
== 0);
736 bool file_equal(const string
& file1
, const string
& file2
)
738 struct stat buf1
, buf2
;
740 if (lstat(file1
.c_str(), &buf1
) == -1)
743 if (lstat(file2
.c_str(), &buf2
) == -1)
747 if (S_ISREG(buf1
.st_mode
) && S_ISREG(buf2
.st_mode
)) {
748 ifstream
f1(file1
.c_str());
749 ifstream
f2(file2
.c_str());
757 f1
.read(buffer1
, 4096);
758 f2
.read(buffer2
, 4096);
759 if (f1
.gcount() != f2
.gcount() ||
760 memcmp(buffer1
, buffer2
, f1
.gcount()) ||
761 f1
.eof() != f2
.eof())
768 else if (S_ISLNK(buf1
.st_mode
) && S_ISLNK(buf2
.st_mode
)) {
769 char symlink1
[MAXPATHLEN
];
770 char symlink2
[MAXPATHLEN
];
772 memset(symlink1
, 0, MAXPATHLEN
);
773 memset(symlink2
, 0, MAXPATHLEN
);
775 if (readlink(file1
.c_str(), symlink1
, MAXPATHLEN
- 1) == -1)
778 if (readlink(file2
.c_str(), symlink2
, MAXPATHLEN
- 1) == -1)
781 return !strncmp(symlink1
, symlink2
, MAXPATHLEN
);
784 else if (S_ISCHR(buf1
.st_mode
) && S_ISCHR(buf2
.st_mode
)) {
785 return buf1
.st_dev
== buf2
.st_dev
;
788 else if (S_ISBLK(buf1
.st_mode
) && S_ISBLK(buf2
.st_mode
)) {
789 return buf1
.st_dev
== buf2
.st_dev
;
795 bool permissions_equal(const string
& file1
, const string
& file2
)
800 if (lstat(file1
.c_str(), &buf1
) == -1)
803 if (lstat(file2
.c_str(), &buf2
) == -1)
806 return(buf1
.st_mode
== buf2
.st_mode
) &&
807 (buf1
.st_uid
== buf2
.st_uid
) &&
808 (buf1
.st_gid
== buf2
.st_gid
);
811 void file_remove(const string
& basedir
, const string
& filename
)
813 if (filename
!= basedir
&& !remove(filename
.c_str())) {
814 char* path
= strdup(filename
.c_str());
815 file_remove(basedir
, dirname(path
));