rename.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various rename tests
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <sys/stat.h>
  14. #include "liburing.h"
  15. /* test using a bad address for either old or new path */
  16. static int test_rename_badaddr(struct io_uring *ring, bool bad_old)
  17. {
  18. struct io_uring_cqe *cqe;
  19. struct io_uring_sqe *sqe;
  20. const char *path = ".foo.bar";
  21. const char *old, *new;
  22. int ret;
  23. if (bad_old) {
  24. old = (void *) (uintptr_t) 0x1234;
  25. new = path;
  26. } else {
  27. old = path;
  28. new = (void *) (uintptr_t) 0x1234;
  29. }
  30. sqe = io_uring_get_sqe(ring);
  31. if (!sqe) {
  32. fprintf(stderr, "get sqe failed\n");
  33. goto err;
  34. }
  35. memset(sqe, 0, sizeof(*sqe));
  36. io_uring_prep_rename(sqe, old, new);
  37. ret = io_uring_submit(ring);
  38. if (ret <= 0) {
  39. fprintf(stderr, "sqe submit failed: %d\n", ret);
  40. goto err;
  41. }
  42. ret = io_uring_wait_cqe(ring, &cqe);
  43. if (ret < 0) {
  44. fprintf(stderr, "wait completion %d\n", ret);
  45. goto err;
  46. }
  47. ret = cqe->res;
  48. io_uring_cqe_seen(ring, cqe);
  49. return ret;
  50. err:
  51. return 1;
  52. }
  53. static int test_rename(struct io_uring *ring, const char *old, const char *new)
  54. {
  55. struct io_uring_cqe *cqe;
  56. struct io_uring_sqe *sqe;
  57. int ret;
  58. sqe = io_uring_get_sqe(ring);
  59. if (!sqe) {
  60. fprintf(stderr, "get sqe failed\n");
  61. goto err;
  62. }
  63. memset(sqe, 0, sizeof(*sqe));
  64. io_uring_prep_rename(sqe, old, new);
  65. ret = io_uring_submit(ring);
  66. if (ret <= 0) {
  67. fprintf(stderr, "sqe submit failed: %d\n", ret);
  68. goto err;
  69. }
  70. ret = io_uring_wait_cqe(ring, &cqe);
  71. if (ret < 0) {
  72. fprintf(stderr, "wait completion %d\n", ret);
  73. goto err;
  74. }
  75. ret = cqe->res;
  76. io_uring_cqe_seen(ring, cqe);
  77. return ret;
  78. err:
  79. return 1;
  80. }
  81. static int stat_file(const char *buf)
  82. {
  83. struct stat sb;
  84. if (!stat(buf, &sb))
  85. return 0;
  86. return errno;
  87. }
  88. int main(int argc, char *argv[])
  89. {
  90. struct io_uring ring;
  91. char src[32] = "./XXXXXX";
  92. char dst[32] = "./XXXXXX";
  93. int ret;
  94. if (argc > 1)
  95. return 0;
  96. ret = io_uring_queue_init(1, &ring, 0);
  97. if (ret) {
  98. fprintf(stderr, "ring setup failed: %d\n", ret);
  99. return 1;
  100. }
  101. ret = mkstemp(src);
  102. if (ret < 0) {
  103. perror("mkstemp");
  104. return 1;
  105. }
  106. close(ret);
  107. ret = mkstemp(dst);
  108. if (ret < 0) {
  109. perror("mkstemp");
  110. return 1;
  111. }
  112. close(ret);
  113. if (stat_file(src) != 0) {
  114. perror("stat");
  115. return 1;
  116. }
  117. if (stat_file(dst) != 0) {
  118. perror("stat");
  119. return 1;
  120. }
  121. ret = test_rename(&ring, src, dst);
  122. if (ret < 0) {
  123. if (ret == -EBADF || ret == -EINVAL) {
  124. fprintf(stdout, "Rename not supported, skipping\n");
  125. goto out;
  126. }
  127. fprintf(stderr, "rename: %s\n", strerror(-ret));
  128. goto err;
  129. } else if (ret)
  130. goto err;
  131. if (stat_file(src) != ENOENT) {
  132. fprintf(stderr, "stat got %s\n", strerror(ret));
  133. return 1;
  134. }
  135. if (stat_file(dst) != 0) {
  136. perror("stat");
  137. return 1;
  138. }
  139. ret = test_rename(&ring, "/x/y/1/2", "/2/1/y/x");
  140. if (ret != -ENOENT) {
  141. fprintf(stderr, "test_rename invalid failed: %d\n", ret);
  142. return ret;
  143. }
  144. ret = test_rename_badaddr(&ring, 0);
  145. if (ret != -EFAULT) {
  146. fprintf(stderr, "test_badaddr 0 failed: %d\n", ret);
  147. return ret;
  148. }
  149. ret = test_rename_badaddr(&ring, 1);
  150. if (ret != -EFAULT) {
  151. fprintf(stderr, "test_badaddr 1 failed: %d\n", ret);
  152. return ret;
  153. }
  154. out:
  155. unlink(dst);
  156. return 0;
  157. err:
  158. unlink(src);
  159. unlink(dst);
  160. return 1;
  161. }