file.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 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/file.h>
  32. void
  33. file_copy_perms(const char *src, const char *dst)
  34. {
  35. struct stat stab;
  36. if (stat(src, &stab) == -1) {
  37. if (errno == ENOENT)
  38. return;
  39. ohshite(_("unable to stat source file '%.250s'"), src);
  40. }
  41. if (chown(dst, stab.st_uid, stab.st_gid) == -1)
  42. ohshite(_("unable to change ownership of target file '%.250s'"),
  43. dst);
  44. if (chmod(dst, (stab.st_mode & 07777)) == -1)
  45. ohshite(_("unable to set mode of target file '%.250s'"), dst);
  46. }
  47. static void
  48. file_unlock_cleanup(int argc, void **argv)
  49. {
  50. int lockfd = *(int*)argv[0];
  51. struct flock fl;
  52. assert(lockfd >= 0);
  53. fl.l_type = F_UNLCK;
  54. fl.l_whence = SEEK_SET;
  55. fl.l_start = 0;
  56. fl.l_len = 0;
  57. if (fcntl(lockfd, F_SETLK, &fl) == -1)
  58. ohshite(_("unable to unlock dpkg status database"));
  59. }
  60. void
  61. file_unlock(void)
  62. {
  63. pop_cleanup(ehflag_normaltidy); /* Calls file_unlock_cleanup. */
  64. }
  65. /* lockfd must be allocated statically as its addresses is passed to
  66. * a cleanup handler. */
  67. void
  68. file_lock(int *lockfd, const char *filename,
  69. const char *emsg, const char *emsg_eagain)
  70. {
  71. struct flock fl;
  72. setcloexec(*lockfd, filename);
  73. fl.l_type = F_WRLCK;
  74. fl.l_whence = SEEK_SET;
  75. fl.l_start = 0;
  76. fl.l_len = 0;
  77. if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
  78. if (emsg_eagain && (errno == EACCES || errno == EAGAIN))
  79. ohshit(emsg_eagain);
  80. ohshite(emsg);
  81. }
  82. push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 1, lockfd);
  83. }