file.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * file.c - file handling functions
  4. *
  5. * Copyright © 1994, 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
  7. *
  8. * This 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 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. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/subproc.h>
  32. #include <dpkg/command.h>
  33. #include <dpkg/file.h>
  34. /**
  35. * Copy file ownership and permissions from one file to another.
  36. *
  37. * @param src The source filename.
  38. * @param dst The destination filename.
  39. */
  40. void
  41. file_copy_perms(const char *src, const char *dst)
  42. {
  43. struct stat stab;
  44. if (stat(src, &stab) == -1) {
  45. if (errno == ENOENT)
  46. return;
  47. ohshite(_("unable to stat source file '%.250s'"), src);
  48. }
  49. if (chown(dst, stab.st_uid, stab.st_gid) == -1)
  50. ohshite(_("unable to change ownership of target file '%.250s'"),
  51. dst);
  52. if (chmod(dst, (stab.st_mode & 07777)) == -1)
  53. ohshite(_("unable to set mode of target file '%.250s'"), dst);
  54. }
  55. static void
  56. file_lock_setup(struct flock *fl, short type)
  57. {
  58. fl->l_type = type;
  59. fl->l_whence = SEEK_SET;
  60. fl->l_start = 0;
  61. fl->l_len = 0;
  62. fl->l_pid = 0;
  63. }
  64. /**
  65. * Unlock a previously locked file.
  66. */
  67. void
  68. file_unlock(int lockfd, const char *lock_desc)
  69. {
  70. struct flock fl;
  71. assert(lockfd >= 0);
  72. file_lock_setup(&fl, F_UNLCK);
  73. if (fcntl(lockfd, F_SETLK, &fl) == -1)
  74. ohshite(_("unable to unlock %s"), lock_desc);
  75. }
  76. static void
  77. file_unlock_cleanup(int argc, void **argv)
  78. {
  79. int lockfd = *(int *)argv[0];
  80. const char *lock_desc = argv[1];
  81. file_unlock(lockfd, lock_desc);
  82. }
  83. /**
  84. * Check if a file has a lock acquired.
  85. *
  86. * @param lockfd The file descriptor for the lock.
  87. * @param filename The file name associated to the file descriptor.
  88. */
  89. bool
  90. file_is_locked(int lockfd, const char *filename)
  91. {
  92. struct flock fl;
  93. file_lock_setup(&fl, F_WRLCK);
  94. if (fcntl(lockfd, F_GETLK, &fl) == -1)
  95. ohshit(_("unable to check file '%s' lock status"), filename);
  96. if (fl.l_type == F_WRLCK && fl.l_pid != getpid())
  97. return true;
  98. else
  99. return false;
  100. }
  101. /**
  102. * Lock a file.
  103. *
  104. * @param lockfd The pointer to the lock file descriptor. It must be allocated
  105. * statically as its addresses is passed to a cleanup handler.
  106. * @param flags The lock flags specifying what type of locking to perform.
  107. * @param filename The name of the file to lock.
  108. * @param desc The description of the file to lock.
  109. */
  110. void
  111. file_lock(int *lockfd, enum file_lock_flags flags, const char *filename,
  112. const char *desc)
  113. {
  114. struct flock fl;
  115. int lock_cmd;
  116. setcloexec(*lockfd, filename);
  117. file_lock_setup(&fl, F_WRLCK);
  118. if (flags == FILE_LOCK_WAIT)
  119. lock_cmd = F_SETLKW;
  120. else
  121. lock_cmd = F_SETLK;
  122. if (fcntl(*lockfd, lock_cmd, &fl) == -1) {
  123. if (errno == EACCES || errno == EAGAIN)
  124. ohshit(_("%s is locked by another process"), desc);
  125. else
  126. ohshite(_("unable to lock %s"), desc);
  127. }
  128. push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 2, lockfd, desc);
  129. }
  130. void
  131. file_show(const char *filename)
  132. {
  133. pid_t pid;
  134. if (filename == NULL)
  135. internerr("file '%s' does not exist", filename);
  136. pid = subproc_fork();
  137. if (pid == 0) {
  138. struct command cmd;
  139. const char *pager;
  140. pager = command_get_pager();
  141. command_init(&cmd, pager, _("showing file on pager"));
  142. command_add_arg(&cmd, pager);
  143. command_add_arg(&cmd, filename);
  144. command_exec(&cmd);
  145. }
  146. subproc_wait(pid, _("showing file on pager"));
  147. }