dpkg-distaddfile.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 qw(:errno_h :fcntl_h);
  23. use Dpkg;
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::File;
  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<files-list-file> write files here instead of debian/files.
  41. -?, --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/^-(\?|-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 $lockfh;
  68. sysopen($lockfh, 'debian/control', O_WRONLY) ||
  69. syserr(_g('cannot write %s'), 'debian/control');
  70. file_lock($lockfh, 'debian/control');
  71. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  72. open(my $fileslistnew_fh, '>', "$fileslistfile.new") ||
  73. syserr(_g('open new files list file'));
  74. if (open(my $fileslist_fh, '<', $fileslistfile)) {
  75. while (<$fileslist_fh>) {
  76. s/\n$//;
  77. next if m/^(\S+) / && $1 eq $file;
  78. print($fileslistnew_fh "$_\n") ||
  79. syserr(_g('copy old entry to new files list file'));
  80. }
  81. close $fileslist_fh or syserr(_g('cannot close %s'), $fileslistfile);
  82. } elsif ($! != ENOENT) {
  83. syserr(_g('read old files list file'));
  84. }
  85. print($fileslistnew_fh "$file $section $priority\n")
  86. || syserr(_g('write new entry to new files list file'));
  87. close($fileslistnew_fh) || syserr(_g('close new files list file'));
  88. rename("$fileslistfile.new", $fileslistfile) ||
  89. syserr(_g('install new files list file'));
  90. # Release the lock
  91. close($lockfh) || syserr(_g('cannot close %s'), 'debian/control');