file_size.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #define STR_HELPER(x) #x
  11. #define STR(x) STR_HELPER(x)
  12. #define BUF_LENGTH 4096
  13. #define HOLE_LENGTH 8192
  14. #define TEST_DIR "tmp"
  15. #define TEST_FILE "__testfile__"
  16. ssize_t rw_file(int fd, char* buf, size_t bytes, bool write_flag) {
  17. ssize_t rv = 0;
  18. ssize_t ret;
  19. while (bytes > rv) {
  20. if (write_flag)
  21. ret = write(fd, buf + rv, bytes - rv);
  22. else
  23. ret = read(fd, buf + rv, bytes - rv);
  24. if (ret > 0) {
  25. rv += ret;
  26. } else {
  27. if (ret < 0 && (errno == EAGAIN || errno == EINTR)) {
  28. continue;
  29. } else {
  30. fprintf(stderr, "%s failed:%s\n", write_flag ? "write" : "read", strerror(errno));
  31. return ret;
  32. }
  33. }
  34. }
  35. return rv;
  36. }
  37. int main(int argc, const char** argv) {
  38. char buf[BUF_LENGTH];
  39. ssize_t bytes;
  40. int fd = 0;
  41. int rv = 0;
  42. memset(buf, 0, BUF_LENGTH);
  43. /* setup the test directory and file */
  44. rv = mkdir(TEST_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  45. if (rv < 0 && errno != EEXIST) {
  46. perror("mkdir failed");
  47. return 1;
  48. }
  49. rv = unlink(TEST_DIR"/"TEST_FILE);
  50. if (rv < 0 && errno != ENOENT) {
  51. perror("unlink failed");
  52. return 1;
  53. }
  54. fd = open(TEST_DIR"/"TEST_FILE, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  55. if (fd < 0) {
  56. perror("open failed");
  57. return 1;
  58. }
  59. /* test file size: write a file of type != FILEBUF_MAP */
  60. bytes = rw_file(fd, buf, BUF_LENGTH, true);
  61. if (bytes != BUF_LENGTH) {
  62. perror("writing " STR(BUF_LENGTH) " bytes to test file failed");
  63. return 1;
  64. }
  65. rv = lseek(fd, 0, SEEK_SET);
  66. if (rv < 0) {
  67. perror("lseek to beginning of file failed");
  68. return 1;
  69. }
  70. bytes = rw_file(fd, buf, 1, true);
  71. if (bytes != 1) {
  72. perror("writing one byte to test file failed");
  73. return 1;
  74. }
  75. rv = lseek(fd, HOLE_LENGTH, SEEK_SET);
  76. if (rv < 0) {
  77. perror("lseek to " STR(HOLE_LENGTH) " offset failed");
  78. return 1;
  79. }
  80. bytes = rw_file(fd, buf, BUF_LENGTH, true);
  81. if (bytes != BUF_LENGTH) {
  82. perror("writing " STR(BUF_LENGTH) " bytes to test file failed");
  83. return 1;
  84. }
  85. rv = close(fd);
  86. if (rv < 0) {
  87. perror("close failed");
  88. return 1;
  89. }
  90. fd = open(TEST_DIR"/"TEST_FILE, O_RDONLY);
  91. if (fd < 0) {
  92. perror("open failed");
  93. return 1;
  94. }
  95. /* reopen file: file size should be HOLE_LENGTH + BUF_LENGTH */
  96. rv = lseek(fd, HOLE_LENGTH, SEEK_SET);
  97. if (rv < 0) {
  98. perror("lseek to " STR(HOLE_LENGTH) " offset failed");
  99. return 1;
  100. }
  101. bytes = rw_file(fd, buf, BUF_LENGTH, false);
  102. if (bytes != BUF_LENGTH) {
  103. perror("reading " STR(BUF_LENGTH) " bytes from test file failed");
  104. return 1;
  105. }
  106. rv = close(fd);
  107. if (rv < 0) {
  108. perror("close failed");
  109. return 1;
  110. }
  111. rv = unlink(TEST_DIR"/"TEST_FILE);
  112. if (rv < 0) {
  113. perror("unlink failed");
  114. return 1;
  115. }
  116. printf("test completed successfully\n");
  117. return 0;
  118. }