fdio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 HAVE_F_PREALLOCATE
  67. static void
  68. fd_preallocate_setup(fstore_t *fs, int flags, off_t offset, off_t len)
  69. {
  70. fs->fst_flags = flags;
  71. fs->fst_posmode = F_PEOFPOSMODE;
  72. fs->fst_offset = offset;
  73. fs->fst_length = len;
  74. fs->fst_bytesalloc = 0;
  75. }
  76. #endif
  77. /**
  78. * Request the kernel to allocate the specified size for a file descriptor.
  79. *
  80. * We only want to send a hint that we will be using the requested size. But
  81. * we do not want to unnecessarily write the file contents. That is why we
  82. * are not using posix_fallocate(3) directly if possible, and not at all
  83. * on glibc based systems (except on GNU/kFreeBSD).
  84. */
  85. int
  86. fd_allocate_size(int fd, off_t offset, off_t len)
  87. {
  88. int rc;
  89. if (len == 0)
  90. return 0;
  91. #if defined(HAVE_F_PREALLOCATE)
  92. /* On Mac OS X. */
  93. fstore_t fs;
  94. fd_preallocate_setup(&fs, F_ALLOCATECONTIG, offset, len);
  95. rc = fcntl(fd, F_PREALLOCATE, &fs);
  96. if (rc < 0 && errno == ENOSPC) {
  97. /* If we cannot get a contiguous allocation, then try
  98. * non-contiguous. */
  99. fd_preallocate_setup(&fs, F_ALLOCATEALL, offset, len);
  100. rc = fcntl(fd, F_PREALLOCATE, &fs);
  101. }
  102. #elif defined(HAVE_F_ALLOCSP64)
  103. /* On Solaris. */
  104. struct flock64 fl;
  105. fl.l_whence = SEEK_SET;
  106. fl.l_start = offset;
  107. fl.l_len = len;
  108. rc = fcntl(fd, F_ALLOCSP64, &fl);
  109. #elif defined(HAVE_FALLOCATE)
  110. /* On Linux. */
  111. do {
  112. rc = fallocate(fd, 0, offset, len);
  113. } while (rc < 0 && errno == EINTR);
  114. #elif defined(HAVE_POSIX_FALLOCATE) && \
  115. ((defined(__GLIBC__) && defined(__FreeBSD_kernel__)) || \
  116. !defined(__GLIBC__))
  117. /*
  118. * On BSDs, newer GNU/kFreeBSD and other non-glibc based systems
  119. * we can use posix_fallocate(2) which should be a simple syscall
  120. * wrapper. But not on other glibc systems, as there the function
  121. * will try to allocate the size by writing a '\0' to each block
  122. * if the syscall is not implemented or not supported by the
  123. * kernel or the filesystem, which we do not want.
  124. */
  125. rc = posix_fallocate(fd, offset, len);
  126. #else
  127. errno = ENOSYS;
  128. rc = -1;
  129. #endif
  130. return rc;
  131. }