mkdir.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test io_uring mkdirat handling
  5. */
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #include "liburing.h"
  13. #include "helpers.h"
  14. static int do_mkdirat(struct io_uring *ring, const char *fn)
  15. {
  16. int ret;
  17. struct io_uring_sqe *sqe;
  18. struct io_uring_cqe *cqe;
  19. sqe = io_uring_get_sqe(ring);
  20. if (!sqe) {
  21. fprintf(stderr, "sqe get failed\n");
  22. goto err;
  23. }
  24. io_uring_prep_mkdirat(sqe, AT_FDCWD, fn, 0700);
  25. ret = io_uring_submit(ring);
  26. if (ret != 1) {
  27. fprintf(stderr, "submit failed: %d\n", ret);
  28. goto err;
  29. }
  30. ret = io_uring_wait_cqes(ring, &cqe, 1, 0, 0);
  31. if (ret) {
  32. fprintf(stderr, "wait_cqe failed: %d\n", ret);
  33. goto err;
  34. }
  35. ret = cqe->res;
  36. io_uring_cqe_seen(ring, cqe);
  37. return ret;
  38. err:
  39. return 1;
  40. }
  41. static int stat_file(const char *fn)
  42. {
  43. struct stat sb;
  44. if (!stat(fn, &sb))
  45. return 0;
  46. return errno;
  47. }
  48. int main(int argc, char *argv[])
  49. {
  50. static const char fn[] = "io_uring-mkdirat-test";
  51. int ret;
  52. struct io_uring ring;
  53. if (argc > 1)
  54. return T_EXIT_SKIP;
  55. ret = io_uring_queue_init(8, &ring, 0);
  56. if (ret) {
  57. fprintf(stderr, "queue init failed: %d\n", ret);
  58. return ret;
  59. }
  60. ret = do_mkdirat(&ring, fn);
  61. if (ret < 0) {
  62. if (ret == -EBADF || ret == -EINVAL) {
  63. fprintf(stdout, "mkdirat not supported, skipping\n");
  64. goto skip;
  65. }
  66. fprintf(stderr, "mkdirat: %s\n", strerror(-ret));
  67. goto err;
  68. } else if (ret) {
  69. goto err;
  70. }
  71. if (stat_file(fn)) {
  72. perror("stat");
  73. goto err;
  74. }
  75. ret = do_mkdirat(&ring, fn);
  76. if (ret != -EEXIST) {
  77. fprintf(stderr, "do_mkdirat already exists failed: %d\n", ret);
  78. goto err1;
  79. }
  80. ret = do_mkdirat(&ring, "surely/this/wont/exist");
  81. if (ret != -ENOENT) {
  82. fprintf(stderr, "do_mkdirat no parent failed: %d\n", ret);
  83. goto err1;
  84. }
  85. unlinkat(AT_FDCWD, fn, AT_REMOVEDIR);
  86. io_uring_queue_exit(&ring);
  87. return T_EXIT_PASS;
  88. skip:
  89. unlinkat(AT_FDCWD, fn, AT_REMOVEDIR);
  90. io_uring_queue_exit(&ring);
  91. return T_EXIT_SKIP;
  92. err1:
  93. unlinkat(AT_FDCWD, fn, AT_REMOVEDIR);
  94. err:
  95. io_uring_queue_exit(&ring);
  96. return T_EXIT_FAIL;
  97. }