rename.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. static int test_rename(struct io_uring *ring, const char *old, const char *new)
  16. {
  17. struct io_uring_cqe *cqe;
  18. struct io_uring_sqe *sqe;
  19. int ret;
  20. sqe = io_uring_get_sqe(ring);
  21. if (!sqe) {
  22. fprintf(stderr, "get sqe failed\n");
  23. goto err;
  24. }
  25. memset(sqe, 0, sizeof(*sqe));
  26. io_uring_prep_rename(sqe, old, new);
  27. ret = io_uring_submit(ring);
  28. if (ret <= 0) {
  29. fprintf(stderr, "sqe submit failed: %d\n", ret);
  30. goto err;
  31. }
  32. ret = io_uring_wait_cqe(ring, &cqe);
  33. if (ret < 0) {
  34. fprintf(stderr, "wait completion %d\n", ret);
  35. goto err;
  36. }
  37. ret = cqe->res;
  38. io_uring_cqe_seen(ring, cqe);
  39. return ret;
  40. err:
  41. return 1;
  42. }
  43. static int stat_file(const char *buf)
  44. {
  45. struct stat sb;
  46. if (!stat(buf, &sb))
  47. return 0;
  48. return errno;
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. struct io_uring ring;
  53. char src[32] = "./XXXXXX";
  54. char dst[32] = "./XXXXXX";
  55. int ret;
  56. if (argc > 1)
  57. return 0;
  58. ret = io_uring_queue_init(1, &ring, 0);
  59. if (ret) {
  60. fprintf(stderr, "ring setup failed: %d\n", ret);
  61. return 1;
  62. }
  63. ret = mkstemp(src);
  64. if (ret < 0) {
  65. perror("mkstemp");
  66. return 1;
  67. }
  68. close(ret);
  69. ret = mkstemp(dst);
  70. if (ret < 0) {
  71. perror("mkstemp");
  72. return 1;
  73. }
  74. close(ret);
  75. if (stat_file(src) != 0) {
  76. perror("stat");
  77. return 1;
  78. }
  79. if (stat_file(dst) != 0) {
  80. perror("stat");
  81. return 1;
  82. }
  83. ret = test_rename(&ring, src, dst);
  84. if (ret < 0) {
  85. if (ret == -EBADF || ret == -EINVAL) {
  86. fprintf(stdout, "Rename not supported, skipping\n");
  87. goto out;
  88. }
  89. fprintf(stderr, "rename: %s\n", strerror(-ret));
  90. goto err;
  91. } else if (ret)
  92. goto err;
  93. if (stat_file(src) != ENOENT) {
  94. fprintf(stderr, "stat got %s\n", strerror(ret));
  95. return 1;
  96. }
  97. if (stat_file(dst) != 0) {
  98. perror("stat");
  99. return 1;
  100. }
  101. ret = test_rename(&ring, "/x/y/1/2", "/2/1/y/x");
  102. if (ret != -ENOENT) {
  103. fprintf(stderr, "test_rename invalid failed: %d\n", ret);
  104. return ret;
  105. }
  106. out:
  107. unlink(dst);
  108. return 0;
  109. err:
  110. unlink(src);
  111. unlink(dst);
  112. return 1;
  113. }