dpkg-distaddfile.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-distaddfile
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. # Copyright © 2006-2008,2010,2012 Guillem Jover <guillem@debian.org>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. use strict;
  21. use warnings;
  22. use POSIX;
  23. use POSIX qw(:errno_h :signal_h);
  24. use Dpkg;
  25. use Dpkg::Gettext;
  26. use Dpkg::ErrorHandling;
  27. use Dpkg::File;
  28. textdomain("dpkg-dev");
  29. my $fileslistfile = 'debian/files';
  30. sub version {
  31. printf _g("Debian %s version %s.\n"), $progname, $version;
  32. printf _g("
  33. This is free software; see the GNU General Public License version 2 or
  34. later for copying conditions. There is NO warranty.
  35. ");
  36. }
  37. sub usage {
  38. printf _g(
  39. "Usage: %s [<option>...] <filename> <section> <priority>
  40. Options:
  41. -f<files-list-file> write files here instead of debian/files.
  42. -?, --help show this help message.
  43. --version show the version.
  44. "), $progname;
  45. }
  46. while (@ARGV && $ARGV[0] =~ m/^-/) {
  47. $_=shift(@ARGV);
  48. if (m/^-f/) {
  49. $fileslistfile= $';
  50. } elsif (m/^-(\?|-help)$/) {
  51. usage();
  52. exit(0);
  53. } elsif (m/^--version$/) {
  54. version();
  55. exit(0);
  56. } elsif (m/^--$/) {
  57. last;
  58. } else {
  59. usageerr(_g("unknown option \`%s'"), $_);
  60. }
  61. }
  62. @ARGV == 3 || usageerr(_g("need exactly a filename, section and priority"));
  63. my ($file, $section, $priority) = @ARGV;
  64. ($file =~ m/\s/ || $section =~ m/\s/ || $priority =~ m/\s/) &&
  65. error(_g("filename, section and priority may contain no whitespace"));
  66. # Obtain a lock on debian/control to avoid simultaneous updates
  67. # of debian/files when parallel building is in use
  68. my $lockfh;
  69. sysopen($lockfh, "debian/control", O_WRONLY) ||
  70. syserr(_g("cannot write %s"), "debian/control");
  71. file_lock($lockfh, "debian/control");
  72. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  73. open(Y, "> $fileslistfile.new") || syserr(_g("open new files list file"));
  74. if (open(X,"< $fileslistfile")) {
  75. while (<X>) {
  76. s/\n$//;
  77. next if m/^(\S+) / && $1 eq $file;
  78. print(Y "$_\n") || syserr(_g("copy old entry to new files list file"));
  79. }
  80. } elsif ($! != ENOENT) {
  81. syserr(_g("read old files list file"));
  82. }
  83. print(Y "$file $section $priority\n")
  84. || syserr(_g("write new entry to new files list file"));
  85. close(Y) || syserr(_g("close new files list file"));
  86. rename("$fileslistfile.new", $fileslistfile) ||
  87. syserr(_g("install new files list file"));
  88. # Release the lock
  89. close($lockfh) || syserr(_g("cannot close %s"), "debian/control");