CRUX-ARM : Home

Home :: Documentation :: Download :: Development :: Community :: Ports :: Packages :: Bugs :: Links :: About :: Donors
tell the user when we didn't install a file because an INSTALL rule kicked in
[pkgutils-cross.git] / pkgutil.cc
1 //
2 // pkgutils
3 //
4 // Copyright (c) 2000-2005 Per Liden
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 //
21
22 #include "pkgutil.h"
23 #include <iostream>
24 #include <fstream>
25 #include <iterator>
26 #include <algorithm>
27 #include <cstdio>
28 #include <cstring>
29 #include <cerrno>
30 #include <csignal>
31 #include <ext/stdio_filebuf.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <sys/file.h>
38 #include <sys/param.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <zlib.h>
42 #include <libgen.h>
43 #include <libtar.h>
44
45 using __gnu_cxx::stdio_filebuf;
46
47 static tartype_t gztype = {
48 (openfunc_t)unistd_gzopen,
49 (closefunc_t)gzclose,
50 (readfunc_t)gzread,
51 (writefunc_t)gzwrite
52 };
53
54 pkgutil::pkgutil(const string& name)
55 : utilname(name)
56 {
57 // Ignore signals
58 struct sigaction sa;
59 memset(&sa, 0, sizeof(sa));
60 sa.sa_handler = SIG_IGN;
61 sigaction(SIGHUP, &sa, 0);
62 sigaction(SIGINT, &sa, 0);
63 sigaction(SIGQUIT, &sa, 0);
64 sigaction(SIGTERM, &sa, 0);
65 }
66
67 void pkgutil::db_open(const string& path)
68 {
69 // Read database
70 root = trim_filename(path + "/");
71 const string filename = root + PKG_DB;
72
73 int fd = open(filename.c_str(), O_RDONLY);
74 if (fd == -1)
75 throw runtime_error_with_errno("could not open " + filename);
76
77 stdio_filebuf<char> filebuf(fd, ios::in, getpagesize());
78 istream in(&filebuf);
79 if (!in)
80 throw runtime_error_with_errno("could not read " + filename);
81
82 while (!in.eof()) {
83 // Read record
84 string name;
85 pkginfo_t info;
86 getline(in, name);
87 getline(in, info.version);
88 for (;;) {
89 string file;
90 getline(in, file);
91
92 if (file.empty())
93 break; // End of record
94
95 info.files.insert(info.files.end(), file);
96 }
97 if (!info.files.empty())
98 packages[name] = info;
99 }
100
101 #ifndef NDEBUG
102 cerr << packages.size() << " packages found in database" << endl;
103 #endif
104 }
105
106 void pkgutil::db_commit()
107 {
108 const string dbfilename = root + PKG_DB;
109 const string dbfilename_new = dbfilename + ".incomplete_transaction";
110 const string dbfilename_bak = dbfilename + ".backup";
111
112 // Remove failed transaction (if it exists)
113 if (unlink(dbfilename_new.c_str()) == -1 && errno != ENOENT)
114 throw runtime_error_with_errno("could not remove " + dbfilename_new);
115
116 // Write new database
117 int fd_new = creat(dbfilename_new.c_str(), 0444);
118 if (fd_new == -1)
119 throw runtime_error_with_errno("could not create " + dbfilename_new);
120
121 stdio_filebuf<char> filebuf_new(fd_new, ios::out, getpagesize());
122 ostream db_new(&filebuf_new);
123 for (packages_t::const_iterator i = packages.begin(); i != packages.end(); ++i) {
124 if (!i->second.files.empty()) {
125 db_new << i->first << "\n";
126 db_new << i->second.version << "\n";
127 copy(i->second.files.begin(), i->second.files.end(), ostream_iterator<string>(db_new, "\n"));
128 db_new << "\n";
129 }
130 }
131
132 db_new.flush();
133
134 // Make sure the new database was successfully written
135 if (!db_new)
136 throw runtime_error("could not write " + dbfilename_new);
137
138 // Synchronize file to disk
139 if (fsync(fd_new) == -1)
140 throw runtime_error_with_errno("could not synchronize " + dbfilename_new);
141
142 // Relink database backup
143 if (unlink(dbfilename_bak.c_str()) == -1 && errno != ENOENT)
144 throw runtime_error_with_errno("could not remove " + dbfilename_bak);
145 if (link(dbfilename.c_str(), dbfilename_bak.c_str()) == -1)
146 throw runtime_error_with_errno("could not create " + dbfilename_bak);
147
148 // Move new database into place
149 if (rename(dbfilename_new.c_str(), dbfilename.c_str()) == -1)
150 throw runtime_error_with_errno("could not rename " + dbfilename_new + " to " + dbfilename);
151
152 #ifndef NDEBUG
153 cerr << packages.size() << " packages written to database" << endl;
154 #endif
155 }
156
157 void pkgutil::db_add_pkg(const string& name, const pkginfo_t& info)
158 {
159 packages[name] = info;
160 }
161
162 bool pkgutil::db_find_pkg(const string& name)
163 {
164 return (packages.find(name) != packages.end());
165 }
166
167 void pkgutil::db_rm_pkg(const string& name)
168 {
169 set<string> files = packages[name].files;
170 packages.erase(name);
171
172 #ifndef NDEBUG
173 cerr << "Removing package phase 1 (all files in package):" << endl;
174 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
175 cerr << endl;
176 #endif
177
178 // Don't delete files that still have references
179 for (packages_t::const_iterator i = packages.begin(); i != packages.end(); ++i)
180 for (set<string>::const_iterator j = i->second.files.begin(); j != i->second.files.end(); ++j)
181 files.erase(*j);
182
183 #ifndef NDEBUG
184 cerr << "Removing package phase 2 (files that still have references excluded):" << endl;
185 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
186 cerr << endl;
187 #endif
188
189 // Delete the files
190 for (set<string>::const_reverse_iterator i = files.rbegin(); i != files.rend(); ++i) {
191 const string filename = root + *i;
192 if (file_exists(filename) && remove(filename.c_str()) == -1) {
193 const char* msg = strerror(errno);
194 cerr << utilname << ": could not remove " << filename << ": " << msg << endl;
195 }
196 }
197 }
198
199 void pkgutil::db_rm_pkg(const string& name, const set<string>& keep_list)
200 {
201 set<string> files = packages[name].files;
202 packages.erase(name);
203
204 #ifndef NDEBUG
205 cerr << "Removing package phase 1 (all files in package):" << endl;
206 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
207 cerr << endl;
208 #endif
209
210 // Don't delete files found in the keep list
211 for (set<string>::const_iterator i = keep_list.begin(); i != keep_list.end(); ++i)
212 files.erase(*i);
213
214 #ifndef NDEBUG
215 cerr << "Removing package phase 2 (files that is in the keep list excluded):" << endl;
216 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
217 cerr << endl;
218 #endif
219
220 // Don't delete files that still have references
221 for (packages_t::const_iterator i = packages.begin(); i != packages.end(); ++i)
222 for (set<string>::const_iterator j = i->second.files.begin(); j != i->second.files.end(); ++j)
223 files.erase(*j);
224
225 #ifndef NDEBUG
226 cerr << "Removing package phase 3 (files that still have references excluded):" << endl;
227 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
228 cerr << endl;
229 #endif
230
231 // Delete the files
232 for (set<string>::const_reverse_iterator i = files.rbegin(); i != files.rend(); ++i) {
233 const string filename = root + *i;
234 if (file_exists(filename) && remove(filename.c_str()) == -1) {
235 if (errno == ENOTEMPTY)
236 continue;
237 const char* msg = strerror(errno);
238 cerr << utilname << ": could not remove " << filename << ": " << msg << endl;
239 }
240 }
241 }
242
243 void pkgutil::db_rm_files(set<string> files, const set<string>& keep_list)
244 {
245 // Remove all references
246 for (packages_t::iterator i = packages.begin(); i != packages.end(); ++i)
247 for (set<string>::const_iterator j = files.begin(); j != files.end(); ++j)
248 i->second.files.erase(*j);
249
250 #ifndef NDEBUG
251 cerr << "Removing files:" << endl;
252 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
253 cerr << endl;
254 #endif
255
256 // Don't delete files found in the keep list
257 for (set<string>::const_iterator i = keep_list.begin(); i != keep_list.end(); ++i)
258 files.erase(*i);
259
260 // Delete the files
261 for (set<string>::const_reverse_iterator i = files.rbegin(); i != files.rend(); ++i) {
262 const string filename = root + *i;
263 if (file_exists(filename) && remove(filename.c_str()) == -1) {
264 if (errno == ENOTEMPTY)
265 continue;
266 const char* msg = strerror(errno);
267 cerr << utilname << ": could not remove " << filename << ": " << msg << endl;
268 }
269 }
270 }
271
272 set<string> pkgutil::db_find_conflicts(const string& name, const pkginfo_t& info)
273 {
274 set<string> files;
275
276 // Find conflicting files in database
277 for (packages_t::const_iterator i = packages.begin(); i != packages.end(); ++i) {
278 if (i->first != name) {
279 set_intersection(info.files.begin(), info.files.end(),
280 i->second.files.begin(), i->second.files.end(),
281 inserter(files, files.end()));
282 }
283 }
284
285 #ifndef NDEBUG
286 cerr << "Conflicts phase 1 (conflicts in database):" << endl;
287 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
288 cerr << endl;
289 #endif
290
291 // Find conflicting files in filesystem
292 for (set<string>::iterator i = info.files.begin(); i != info.files.end(); ++i) {
293 const string filename = root + *i;
294 if (file_exists(filename) && files.find(*i) == files.end())
295 files.insert(files.end(), *i);
296 }
297
298 #ifndef NDEBUG
299 cerr << "Conflicts phase 2 (conflicts in filesystem added):" << endl;
300 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
301 cerr << endl;
302 #endif
303
304 // Exclude directories
305 set<string> tmp = files;
306 for (set<string>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
307 if ((*i)[i->length() - 1] == '/')
308 files.erase(*i);
309 }
310
311 #ifndef NDEBUG
312 cerr << "Conflicts phase 3 (directories excluded):" << endl;
313 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
314 cerr << endl;
315 #endif
316
317 // If this is an upgrade, remove files already owned by this package
318 if (packages.find(name) != packages.end()) {
319 for (set<string>::const_iterator i = packages[name].files.begin(); i != packages[name].files.end(); ++i)
320 files.erase(*i);
321
322 #ifndef NDEBUG
323 cerr << "Conflicts phase 4 (files already owned by this package excluded):" << endl;
324 copy(files.begin(), files.end(), ostream_iterator<string>(cerr, "\n"));
325 cerr << endl;
326 #endif
327 }
328
329 return files;
330 }
331
332 pair<string, pkgutil::pkginfo_t> pkgutil::pkg_open(const string& filename) const
333 {
334 pair<string, pkginfo_t> result;
335 unsigned int i;
336 TAR* t;
337
338 // Extract name and version from filename
339 string basename(filename, filename.rfind('/') + 1);
340 string name(basename, 0, basename.find(VERSION_DELIM));
341 string version(basename, 0, basename.rfind(PKG_EXT));
342 version.erase(0, version.find(VERSION_DELIM) == string::npos ? string::npos : version.find(VERSION_DELIM) + 1);
343
344 if (name.empty() || version.empty())
345 throw runtime_error("could not determine name and/or version of " + basename + ": Invalid package name");
346
347 result.first = name;
348 result.second.version = version;
349
350 if (tar_open(&t, const_cast<char*>(filename.c_str()), &gztype, O_RDONLY, 0, TAR_GNU) == -1)
351 throw runtime_error_with_errno("could not open " + filename);
352
353 for (i = 0; !th_read(t); ++i) {
354 result.second.files.insert(result.second.files.end(), th_get_pathname(t));
355 if (TH_ISREG(t) && tar_skip_regfile(t))
356 throw runtime_error_with_errno("could not read " + filename);
357 }
358
359 if (i == 0) {
360 if (errno == 0)
361 throw runtime_error("empty package");
362 else
363 throw runtime_error("could not read " + filename);
364 }
365
366 tar_close(t);
367
368 return result;
369 }
370
371 void pkgutil::pkg_install(const string& filename, const set<string>& keep_list, const set<string>& non_install_list) const
372 {
373 TAR* t;
374 unsigned int i;
375
376 if (tar_open(&t, const_cast<char*>(filename.c_str()), &gztype, O_RDONLY, 0, TAR_GNU) == -1)
377 throw runtime_error_with_errno("could not open " + filename);
378
379 for (i = 0; !th_read(t); ++i) {
380 string archive_filename = th_get_pathname(t);
381 string reject_dir = trim_filename(root + string("/") + string(PKG_REJECTED));
382 string original_filename = trim_filename(root + string("/") + archive_filename);
383 string real_filename = original_filename;
384
385 // Check if file is filtered out via INSTALL
386 if (non_install_list.find(archive_filename) != non_install_list.end()) {
387 cout << utilname << ": ignoring " << archive_filename << endl;
388
389 if (TH_ISREG(t))
390 tar_skip_regfile(t);
391
392 continue;
393 }
394
395 // Check if file should be rejected
396 if (file_exists(real_filename) && keep_list.find(archive_filename) != keep_list.end())
397 real_filename = trim_filename(reject_dir + string("/") + archive_filename);
398
399 // Extract file
400 if (tar_extract_file(t, const_cast<char*>(real_filename.c_str()))) {
401 // If a file fails to install we just print an error message and
402 // continue trying to install the rest of the package.
403 const char* msg = strerror(errno);
404 cerr << utilname << ": could not install " + archive_filename << ": " << msg << endl;
405 continue;
406 }
407
408 // Check rejected file
409 if (real_filename != original_filename) {
410 bool remove_file = false;
411
412 // Directory
413 if (TH_ISDIR(t))
414 remove_file = permissions_equal(real_filename, original_filename);
415 // Other files
416 else
417 remove_file = permissions_equal(real_filename, original_filename) &&
418 (file_empty(real_filename) || file_equal(real_filename, original_filename));
419
420 // Remove rejected file or signal about its existence
421 if (remove_file)
422 file_remove(reject_dir, real_filename);
423 else
424 cout << utilname << ": rejecting " << archive_filename << ", keeping existing version" << endl;
425 }
426 }
427
428 if (i == 0) {
429 if (errno == 0)
430 throw runtime_error("empty package");
431 else
432 throw runtime_error("could not read " + filename);
433 }
434
435 tar_close(t);
436 }
437
438 void pkgutil::ldconfig() const
439 {
440 // Only execute ldconfig if /etc/ld.so.conf exists
441 if (file_exists(root + LDCONFIG_CONF)) {
442 pid_t pid = fork();
443
444 if (pid == -1)
445 throw runtime_error_with_errno("fork() failed");
446
447 if (pid == 0) {
448 execl(LDCONFIG, LDCONFIG, "-r", root.c_str(), (char *) 0);
449 const char* msg = strerror(errno);
450 cerr << utilname << ": could not execute " << LDCONFIG << ": " << msg << endl;
451 exit(EXIT_FAILURE);
452 } else {
453 if (waitpid(pid, 0, 0) == -1)
454 throw runtime_error_with_errno("waitpid() failed");
455 }
456 }
457 }
458
459 void pkgutil::pkg_footprint(string& filename) const
460 {
461 unsigned int i;
462 TAR* t;
463
464 if (tar_open(&t, const_cast<char*>(filename.c_str()), &gztype, O_RDONLY, 0, TAR_GNU) == -1)
465 throw runtime_error_with_errno("could not open " + filename);
466
467 for (i = 0; !th_read(t); ++i) {
468 // Access permissions
469 if (TH_ISSYM(t)) {
470 // Access permissions on symlinks differ among filesystems, e.g. XFS and ext2 have different.
471 // To avoid getting different footprints we always use "lrwxrwxrwx".
472 cout << "lrwxrwxrwx";
473 } else {
474 cout << mtos(th_get_mode(t));
475 }
476
477 cout << '\t';
478
479 // User
480 uid_t uid = th_get_uid(t);
481 struct passwd* pw = getpwuid(uid);
482 if (pw)
483 cout << pw->pw_name;
484 else
485 cout << uid;
486
487 cout << '/';
488
489 // Group
490 gid_t gid = th_get_gid(t);
491 struct group* gr = getgrgid(gid);
492 if (gr)
493 cout << gr->gr_name;
494 else
495 cout << gid;
496
497 // Filename
498 cout << '\t' << th_get_pathname(t);
499
500 // Special cases
501 if (TH_ISSYM(t)) {
502 // Symlink
503 cout << " -> " << th_get_linkname(t);
504 } else if (TH_ISCHR(t) || TH_ISBLK(t)) {
505 // Device
506 cout << " (" << th_get_devmajor(t) << ", " << th_get_devminor(t) << ")";
507 } else if (TH_ISREG(t) && !th_get_size(t)) {
508 // Empty regular file
509 cout << " (EMPTY)";
510 }
511
512 cout << '\n';
513
514 if (TH_ISREG(t) && tar_skip_regfile(t))
515 throw runtime_error_with_errno("could not read " + filename);
516 }
517
518 if (i == 0) {
519 if (errno == 0)
520 throw runtime_error("empty package");
521 else
522 throw runtime_error("could not read " + filename);
523 }
524
525 tar_close(t);
526 }
527
528 void pkgutil::print_version() const
529 {
530 cout << utilname << " (pkgutils) " << VERSION << endl;
531 }
532
533 db_lock::db_lock(const string& root, bool exclusive)
534 : dir(0)
535 {
536 const string dirname = trim_filename(root + string("/") + PKG_DIR);
537
538 if (!(dir = opendir(dirname.c_str())))
539 throw runtime_error_with_errno("could not read directory " + dirname);
540
541 if (flock(dirfd(dir), (exclusive ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
542 if (errno == EWOULDBLOCK)
543 throw runtime_error("package database is currently locked by another process");
544 else
545 throw runtime_error_with_errno("could not lock directory " + dirname);
546 }
547 }
548
549 db_lock::~db_lock()
550 {
551 if (dir) {
552 flock(dirfd(dir), LOCK_UN);
553 closedir(dir);
554 }
555 }
556
557 void assert_argument(char** argv, int argc, int index)
558 {
559 if (argc - 1 < index + 1)
560 throw runtime_error("option " + string(argv[index]) + " requires an argument");
561 }
562
563 string itos(unsigned int value)
564 {
565 static char buf[20];
566 sprintf(buf, "%u", value);
567 return buf;
568 }
569
570 string mtos(mode_t mode)
571 {
572 string s;
573
574 // File type
575 switch (mode & S_IFMT) {
576 case S_IFREG: s += '-'; break; // Regular
577 case S_IFDIR: s += 'd'; break; // Directory
578 case S_IFLNK: s += 'l'; break; // Symbolic link
579 case S_IFCHR: s += 'c'; break; // Character special
580 case S_IFBLK: s += 'b'; break; // Block special
581 case S_IFSOCK: s += 's'; break; // Socket
582 case S_IFIFO: s += 'p'; break; // Fifo
583 default: s += '?'; break; // Unknown
584 }
585
586 // User permissions
587 s += (mode & S_IRUSR) ? 'r' : '-';
588 s += (mode & S_IWUSR) ? 'w' : '-';
589 switch (mode & (S_IXUSR | S_ISUID)) {
590 case S_IXUSR: s += 'x'; break;
591 case S_ISUID: s += 'S'; break;
592 case S_IXUSR | S_ISUID: s += 's'; break;
593 default: s += '-'; break;
594 }
595
596 // Group permissions
597 s += (mode & S_IRGRP) ? 'r' : '-';
598 s += (mode & S_IWGRP) ? 'w' : '-';
599 switch (mode & (S_IXGRP | S_ISGID)) {
600 case S_IXGRP: s += 'x'; break;
601 case S_ISGID: s += 'S'; break;
602 case S_IXGRP | S_ISGID: s += 's'; break;
603 default: s += '-'; break;
604 }
605
606 // Other permissions
607 s += (mode & S_IROTH) ? 'r' : '-';
608 s += (mode & S_IWOTH) ? 'w' : '-';
609 switch (mode & (S_IXOTH | S_ISVTX)) {
610 case S_IXOTH: s += 'x'; break;
611 case S_ISVTX: s += 'T'; break;
612 case S_IXOTH | S_ISVTX: s += 't'; break;
613 default: s += '-'; break;
614 }
615
616 return s;
617 }
618
619 int unistd_gzopen(char* pathname, int flags, mode_t mode)
620 {
621 char* gz_mode;
622
623 switch (flags & O_ACCMODE) {
624 case O_WRONLY:
625 gz_mode = "w";
626 break;
627
628 case O_RDONLY:
629 gz_mode = "r";
630 break;
631
632 case O_RDWR:
633 default:
634 errno = EINVAL;
635 return -1;
636 }
637
638 int fd;
639 gzFile gz_file;
640
641 if ((fd = open(pathname, flags, mode)) == -1)
642 return -1;
643
644 if ((flags & O_CREAT) && fchmod(fd, mode))
645 return -1;
646
647 if (!(gz_file = gzdopen(fd, gz_mode))) {
648 errno = ENOMEM;
649 return -1;
650 }
651
652 return (int)gz_file;
653 }
654
655 string trim_filename(const string& filename)
656 {
657 string search("//");
658 string result = filename;
659
660 for (string::size_type pos = result.find(search); pos != string::npos; pos = result.find(search))
661 result.replace(pos, search.size(), "/");
662
663 return result;
664 }
665
666 bool file_exists(const string& filename)
667 {
668 struct stat buf;
669 return !lstat(filename.c_str(), &buf);
670 }
671
672 bool file_empty(const string& filename)
673 {
674 struct stat buf;
675
676 if (lstat(filename.c_str(), &buf) == -1)
677 return false;
678
679 return (S_ISREG(buf.st_mode) && buf.st_size == 0);
680 }
681
682 bool file_equal(const string& file1, const string& file2)
683 {
684 struct stat buf1, buf2;
685
686 if (lstat(file1.c_str(), &buf1) == -1)
687 return false;
688
689 if (lstat(file2.c_str(), &buf2) == -1)
690 return false;
691
692 // Regular files
693 if (S_ISREG(buf1.st_mode) && S_ISREG(buf2.st_mode)) {
694 ifstream f1(file1.c_str());
695 ifstream f2(file2.c_str());
696
697 if (!f1 || !f2)
698 return false;
699
700 while (!f1.eof()) {
701 char buffer1[4096];
702 char buffer2[4096];
703 f1.read(buffer1, 4096);
704 f2.read(buffer2, 4096);
705 if (f1.gcount() != f2.gcount() ||
706 memcmp(buffer1, buffer2, f1.gcount()) ||
707 f1.eof() != f2.eof())
708 return false;
709 }
710
711 return true;
712 }
713 // Symlinks
714 else if (S_ISLNK(buf1.st_mode) && S_ISLNK(buf2.st_mode)) {
715 char symlink1[MAXPATHLEN];
716 char symlink2[MAXPATHLEN];
717
718 memset(symlink1, 0, MAXPATHLEN);
719 memset(symlink2, 0, MAXPATHLEN);
720
721 if (readlink(file1.c_str(), symlink1, MAXPATHLEN - 1) == -1)
722 return false;
723
724 if (readlink(file2.c_str(), symlink2, MAXPATHLEN - 1) == -1)
725 return false;
726
727 return !strncmp(symlink1, symlink2, MAXPATHLEN);
728 }
729 // Character devices
730 else if (S_ISCHR(buf1.st_mode) && S_ISCHR(buf2.st_mode)) {
731 return buf1.st_dev == buf2.st_dev;
732 }
733 // Block devices
734 else if (S_ISBLK(buf1.st_mode) && S_ISBLK(buf2.st_mode)) {
735 return buf1.st_dev == buf2.st_dev;
736 }
737
738 return false;
739 }
740
741 bool permissions_equal(const string& file1, const string& file2)
742 {
743 struct stat buf1;
744 struct stat buf2;
745
746 if (lstat(file1.c_str(), &buf1) == -1)
747 return false;
748
749 if (lstat(file2.c_str(), &buf2) == -1)
750 return false;
751
752 return(buf1.st_mode == buf2.st_mode) &&
753 (buf1.st_uid == buf2.st_uid) &&
754 (buf1.st_gid == buf2.st_gid);
755 }
756
757 void file_remove(const string& basedir, const string& filename)
758 {
759 if (filename != basedir && !remove(filename.c_str())) {
760 char* path = strdup(filename.c_str());
761 file_remove(basedir, dirname(path));
762 free(path);
763 }
764 }