unlink.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various unlink 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_unlink(struct io_uring *ring, const char *old)
  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. io_uring_prep_unlink(sqe, old, 0);
  26. ret = io_uring_submit(ring);
  27. if (ret <= 0) {
  28. fprintf(stderr, "sqe submit failed: %d\n", ret);
  29. goto err;
  30. }
  31. ret = io_uring_wait_cqe(ring, &cqe);
  32. if (ret < 0) {
  33. fprintf(stderr, "wait completion %d\n", ret);
  34. goto err;
  35. }
  36. ret = cqe->res;
  37. io_uring_cqe_seen(ring, cqe);
  38. return ret;
  39. err:
  40. return 1;
  41. }
  42. static int stat_file(const char *buf)
  43. {
  44. struct stat sb;
  45. if (!stat(buf, &sb))
  46. return 0;
  47. return errno;
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. struct io_uring ring;
  52. char buf[32] = "./XXXXXX";
  53. int ret;
  54. if (argc > 1)
  55. return 0;
  56. ret = io_uring_queue_init(1, &ring, 0);
  57. if (ret) {
  58. fprintf(stderr, "ring setup failed: %d\n", ret);
  59. return 1;
  60. }
  61. ret = mkstemp(buf);
  62. if (ret < 0) {
  63. perror("mkstemp");
  64. return 1;
  65. }
  66. close(ret);
  67. if (stat_file(buf) != 0) {
  68. perror("stat");
  69. return 1;
  70. }
  71. ret = test_unlink(&ring, buf);
  72. if (ret < 0) {
  73. if (ret == -EBADF || ret == -EINVAL) {
  74. fprintf(stdout, "Unlink not supported, skipping\n");
  75. unlink(buf);
  76. return 0;
  77. }
  78. fprintf(stderr, "rename: %s\n", strerror(-ret));
  79. goto err;
  80. } else if (ret)
  81. goto err;
  82. ret = stat_file(buf);
  83. if (ret != ENOENT) {
  84. fprintf(stderr, "stat got %s\n", strerror(ret));
  85. return 1;
  86. }
  87. ret = test_unlink(&ring, "/3/2/3/1/z/y");
  88. if (ret != -ENOENT) {
  89. fprintf(stderr, "invalid unlink got %s\n", strerror(-ret));
  90. return 1;
  91. }
  92. return 0;
  93. err:
  94. unlink(buf);
  95. return 1;
  96. }