Skip to content

Commit

Permalink
Merge branch 'at_empty_path' of https://github.com/charliemirabile/li…
Browse files Browse the repository at this point in the history
…buring

* 'at_empty_path' of https://github.com/charliemirabile/liburing:
  test/hardlink: Add test for `AT_EMPTY_PATH` flag
  test/hardlink: introduce `fd` variable to hold the file open file descriptor
  test/hardlink: add parameter for old's dirfd to `do_linkat`
  test/hardlink: Cleanup error handling
  • Loading branch information
axboe committed Nov 20, 2023
2 parents 9dc95a0 + f0fe051 commit 98f198b
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions test/hardlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "liburing.h"
#include "helpers.h"

static int do_linkat(struct io_uring *ring, const char *oldname,
static int do_linkat(struct io_uring *ring, int olddirfd, const char *oldname,
const char *newname, int flags)
{
struct io_uring_sqe *sqe;
Expand All @@ -24,7 +24,7 @@ static int do_linkat(struct io_uring *ring, const char *oldname,
fprintf(stderr, "sqe get failed\n");
return 1;
}
io_uring_prep_linkat(sqe, AT_FDCWD, oldname, AT_FDCWD, newname, flags);
io_uring_prep_linkat(sqe, olddirfd, oldname, AT_FDCWD, newname, flags);

ret = io_uring_submit(ring);
if (ret != 1) {
Expand Down Expand Up @@ -68,10 +68,11 @@ static int files_linked_ok(const char* fn1, const char *fn2)
int main(int argc, char *argv[])
{
static const char target[] = "io_uring-linkat-test-target";
static const char emptyname[] = "io_uring-linkat-test-empty";
static const char linkname[] = "io_uring-linkat-test-link";
static const char symlinkname[] = "io_uring-linkat-test-symlink";
struct io_uring ring;
int ret;
int ret, fd, exit_status = T_EXIT_FAIL;

if (argc > 1)
return T_EXIT_SKIP;
Expand All @@ -82,81 +83,88 @@ int main(int argc, char *argv[])
return ret;
}

ret = open(target, O_CREAT | O_RDWR | O_EXCL, 0600);
ret = fd = open(target, O_CREAT | O_RDWR | O_EXCL, 0600);
if (ret < 0) {
perror("open");
goto err;
goto out;
}
if (write(fd, "linktest", 8) != 8) {
close(fd);
goto out;
}
if (write(ret, "linktest", 8) != 8) {
close(ret);
goto err1;
if(geteuid()) {
fprintf(stdout, "not root, skipping AT_EMPTY_PATH test\n");
} else {
ret = do_linkat(&ring, fd, "", emptyname, AT_EMPTY_PATH);
if (ret < 0) {
if (ret == -EBADF || ret == -EINVAL) {
fprintf(stdout, "linkat not supported, skipping\n");
exit_status = T_EXIT_SKIP;
goto out;
}
fprintf(stderr, "linkat: %s\n", strerror(-ret));
goto out;
} else if (ret) {
goto out;
}
if (!files_linked_ok(emptyname, target))
goto out;
unlinkat(AT_FDCWD, emptyname, 0);
}
close(ret);
close(fd);

ret = symlink(target, symlinkname);
if (ret < 0) {
perror("open");
goto err1;
goto out;
}

ret = do_linkat(&ring, target, linkname, 0);
ret = do_linkat(&ring, AT_FDCWD, target, linkname, 0);
if (ret < 0) {
if (ret == -EBADF || ret == -EINVAL) {
fprintf(stdout, "linkat not supported, skipping\n");
goto skip;
exit_status = T_EXIT_SKIP;
goto out;
}
fprintf(stderr, "linkat: %s\n", strerror(-ret));
goto err2;
goto out;
} else if (ret) {
goto err2;
goto out;
}

if (!files_linked_ok(linkname, target))
goto err3;
goto out;

unlinkat(AT_FDCWD, linkname, 0);

ret = do_linkat(&ring, symlinkname, linkname, AT_SYMLINK_FOLLOW);
ret = do_linkat(&ring, AT_FDCWD, symlinkname, linkname, AT_SYMLINK_FOLLOW);
if (ret < 0) {
fprintf(stderr, "linkat: %s\n", strerror(-ret));
goto err2;
goto out;
} else if (ret) {
goto err2;
goto out;
}

if (!files_linked_ok(symlinkname, target))
goto err3;
goto out;

ret = do_linkat(&ring, target, linkname, 0);
ret = do_linkat(&ring, AT_FDCWD, target, linkname, 0);
if (ret != -EEXIST) {
fprintf(stderr, "test_linkat linkname already exists failed: %d\n", ret);
goto err3;
goto out;
}

ret = do_linkat(&ring, target, "surely/this/does/not/exist", 0);
ret = do_linkat(&ring, AT_FDCWD, target, "surely/this/does/not/exist", 0);
if (ret != -ENOENT) {
fprintf(stderr, "test_linkat no parent failed: %d\n", ret);
goto err3;
goto out;
}

unlinkat(AT_FDCWD, symlinkname, 0);
unlinkat(AT_FDCWD, linkname, 0);
unlinkat(AT_FDCWD, target, 0);
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
skip:
exit_status = T_EXIT_PASS;
out:
unlinkat(AT_FDCWD, symlinkname, 0);
unlinkat(AT_FDCWD, linkname, 0);
unlinkat(AT_FDCWD, emptyname, 0);
unlinkat(AT_FDCWD, target, 0);
io_uring_queue_exit(&ring);
return T_EXIT_SKIP;
err3:
unlinkat(AT_FDCWD, linkname, 0);
err2:
unlinkat(AT_FDCWD, symlinkname, 0);
err1:
unlinkat(AT_FDCWD, target, 0);
err:
io_uring_queue_exit(&ring);
return T_EXIT_FAIL;
return exit_status;
}

0 comments on commit 98f198b

Please sign in to comment.