fdio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * fdio.c - safe file descriptor based input/output
  4. *
  5. * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
  6. *
  7. * This 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 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 <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <dpkg/fdio.h>
  26. ssize_t
  27. fd_read(int fd, void *buf, size_t len)
  28. {
  29. ssize_t total = 0;
  30. char *ptr = buf;
  31. while (len > 0) {
  32. ssize_t n;
  33. n = read(fd, ptr + total, len);
  34. if (n == -1) {
  35. if (errno == EINTR || errno == EAGAIN)
  36. continue;
  37. return total ? -total : n;
  38. }
  39. if (n == 0)
  40. break;
  41. total += n;
  42. len -= n;
  43. }
  44. return total;
  45. }
  46. ssize_t
  47. fd_write(int fd, const void *buf, size_t len)
  48. {
  49. ssize_t total = 0;
  50. const char *ptr = buf;
  51. while (len > 0) {
  52. ssize_t n;
  53. n = write(fd, ptr + total, len);
  54. if (n == -1) {
  55. if (errno == EINTR || errno == EAGAIN)
  56. continue;
  57. return total ? -total : n;
  58. }
  59. if (n == 0)
  60. break;
  61. total += n;
  62. len -= n;
  63. }
  64. return total;
  65. }
  66. #ifdef USE_DISK_PREALLOCATE
  67. #ifdef HAVE_F_PREALLOCATE
  68. static void
  69. fd_preallocate_setup(fstore_t *fs, int flags, off_t offset, off_t len)
  70. {
  71. fs->fst_flags = flags;
  72. fs->fst_posmode = F_PEOFPOSMODE;
  73. fs->fst_offset = offset;
  74. fs->fst_length = len;
  75. fs->fst_bytesalloc = 0;
  76. }
  77. #endif
  78. /**
  79. * Request the kernel to allocate the specified size for a file descriptor.
  80. *
  81. * We only want to send a hint that we will be using the requested size. But
  82. * we do not want to unnecessarily write the file contents. That is why we
  83. * are not using posix_fallocate(3) directly if possible, and not at all
  84. * on glibc based systems (except on GNU/kFreeBSD).
  85. */
  86. int
  87. fd_allocate_size(int fd, off_t offset, off_t len)
  88. {
  89. int rc;
  90. /* Do not preallocate on very small files as that degrades performance
  91. * on some filesystems. */
  92. if (len < (4 * 4096) - 1)
  93. return 0;
  94. #if defined(HAVE_F_PREALLOCATE)
  95. /* On Mac OS X. */
  96. fstore_t fs;
  97. fd_preallocate_setup(&fs, F_ALLOCATECONTIG, offset, len);
  98. rc = fcntl(fd, F_PREALLOCATE, &fs);
  99. if (rc < 0 && errno == ENOSPC) {
  100. /* If we cannot get a contiguous allocation, then try
  101. * non-contiguous. */
  102. fd_preallocate_setup(&fs, F_ALLOCATEALL, offset, len);
  103. rc = fcntl(fd, F_PREALLOCATE, &fs);
  104. }
  105. #elif defined(HAVE_F_ALLOCSP64)
  106. /* On Solaris. */
  107. struct flock64 fl;
  108. fl.l_whence = SEEK_SET;
  109. fl.l_start = offset;
  110. fl.l_len = len;
  111. rc = fcntl(fd, F_ALLOCSP64, &fl);
  112. #elif defined(HAVE_FALLOCATE)
  113. /* On Linux. */
  114. do {
  115. rc = fallocate(fd, 0, offset, len);
  116. } while (rc < 0 && errno == EINTR);
  117. #elif defined(HAVE_POSIX_FALLOCATE) && \
  118. ((defined(__GLIBC__) && defined(__FreeBSD_kernel__)) || \
  119. !defined(__GLIBC__))
  120. /*
  121. * On BSDs, newer GNU/kFreeBSD and other non-glibc based systems
  122. * we can use posix_fallocate(2) which should be a simple syscall
  123. * wrapper. But not on other glibc systems, as there the function
  124. * will try to allocate the size by writing a '\0' to each block
  125. * if the syscall is not implemented or not supported by the
  126. * kernel or the filesystem, which we do not want.
  127. */
  128. rc = posix_fallocate(fd, offset, len);
  129. #else
  130. errno = ENOSYS;
  131. rc = -1;
  132. #endif
  133. return rc;
  134. }
  135. #else
  136. int
  137. fd_allocate_size(int fd, off_t offset, off_t len)
  138. {
  139. errno = ENOSYS;
  140. return -1;
  141. }
  142. #endif