dpkg-distaddfile.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-distaddfile
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. #
  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.
  11. #
  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.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. use strict;
  20. use warnings;
  21. use File::FcntlLock;
  22. use POSIX;
  23. use POSIX qw(:errno_h :signal_h);
  24. use Dpkg;
  25. use Dpkg::Gettext;
  26. use Dpkg::ErrorHandling;
  27. textdomain("dpkg-dev");
  28. my $fileslistfile = 'debian/files';
  29. sub version {
  30. printf _g("Debian %s version %s.\n"), $progname, $version;
  31. printf _g("
  32. This is free software; see the GNU General Public License version 2 or
  33. later for copying conditions. There is NO warranty.
  34. ");
  35. }
  36. sub usage {
  37. printf _g(
  38. "Usage: %s [<option>...] <filename> <section> <priority>
  39. Options:
  40. -f<fileslistfile> write files here instead of debian/files.
  41. -h, --help show this help message.
  42. --version show the version.
  43. "), $progname;
  44. }
  45. while (@ARGV && $ARGV[0] =~ m/^-/) {
  46. $_=shift(@ARGV);
  47. if (m/^-f/) {
  48. $fileslistfile= $';
  49. } elsif (m/^-(h|-help)$/) {
  50. usage();
  51. exit(0);
  52. } elsif (m/^--version$/) {
  53. version();
  54. exit(0);
  55. } elsif (m/^--$/) {
  56. last;
  57. } else {
  58. usageerr(_g("unknown option \`%s'"), $_);
  59. }
  60. }
  61. @ARGV == 3 || usageerr(_g("need exactly a filename, section and priority"));
  62. my ($file, $section, $priority) = @ARGV;
  63. ($file =~ m/\s/ || $section =~ m/\s/ || $priority =~ m/\s/) &&
  64. error(_g("filename, section and priority may contain no whitespace"));
  65. # Obtain a lock on debian/control to avoid simultaneous updates
  66. # of debian/files when parallel building is in use
  67. my $fs = File::FcntlLock->new(l_type => F_WRLCK);
  68. my $lockfh;
  69. sysopen($lockfh, "debian/control", O_WRONLY) ||
  70. syserr(_g("cannot write %s"), "debian/control");
  71. $fs->lock($lockfh, F_SETLKW) ||
  72. syserr(_("failed to get a write lock on %s"), "debian/control");
  73. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  74. open(Y, "> $fileslistfile.new") || syserr(_g("open new files list file"));
  75. if (open(X,"< $fileslistfile")) {
  76. while (<X>) {
  77. s/\n$//;
  78. next if m/^(\S+) / && $1 eq $file;
  79. print(Y "$_\n") || syserr(_g("copy old entry to new files list file"));
  80. }
  81. } elsif ($! != ENOENT) {
  82. syserr(_g("read old files list file"));
  83. }
  84. print(Y "$file $section $priority\n")
  85. || syserr(_g("write new entry to new files list file"));
  86. close(Y) || syserr(_g("close new files list file"));
  87. rename("$fileslistfile.new", $fileslistfile) ||
  88. syserr(_g("install new files list file"));
  89. # Release the lock
  90. close($lockfh) || syserr(_g("cannot close %s"), "debian/control");