|
Packit |
6c4009 |
/* Linux implementation for renameat2 function.
|
|
Packit |
6c4009 |
Copyright (C) 2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library. If not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <array_length.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <support/check.h>
|
|
Packit |
6c4009 |
#include <support/support.h>
|
|
Packit |
6c4009 |
#include <support/temp_file.h>
|
|
Packit |
6c4009 |
#include <support/xunistd.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Directory with the temporary files. */
|
|
Packit |
6c4009 |
static char *directory;
|
|
Packit |
6c4009 |
static int directory_fd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Paths within that directory. */
|
|
Packit |
6c4009 |
static char *old_path; /* File is called "old". */
|
|
Packit |
6c4009 |
static char *new_path; /* File is called "new". */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Subdirectory within the directory above. */
|
|
Packit |
6c4009 |
static char *subdirectory;
|
|
Packit |
6c4009 |
int subdirectory_fd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* And a pathname in that directory (called "file"). */
|
|
Packit |
6c4009 |
static char *subdir_path;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
prepare (int argc, char **argv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
directory = support_create_temp_directory ("tst-renameat2-");
|
|
Packit |
6c4009 |
directory_fd = xopen (directory, O_RDONLY | O_DIRECTORY, 0);
|
|
Packit |
6c4009 |
old_path = xasprintf ("%s/old", directory);
|
|
Packit |
6c4009 |
add_temp_file (old_path);
|
|
Packit |
6c4009 |
new_path = xasprintf ("%s/new", directory);
|
|
Packit |
6c4009 |
add_temp_file (new_path);
|
|
Packit |
6c4009 |
subdirectory = xasprintf ("%s/subdir", directory);
|
|
Packit |
6c4009 |
xmkdir (subdirectory, 0777);
|
|
Packit |
6c4009 |
add_temp_file (subdirectory);
|
|
Packit |
6c4009 |
subdirectory_fd = xopen (subdirectory, O_RDONLY | O_DIRECTORY, 0);
|
|
Packit |
6c4009 |
subdir_path = xasprintf ("%s/file", subdirectory);
|
|
Packit |
6c4009 |
add_temp_file (subdir_path);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Delete all files, preparing a clean slate for the next test. */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
delete_all_files (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *files[] = { old_path, new_path, subdir_path };
|
|
Packit |
6c4009 |
for (size_t i = 0; i < array_length (files); ++i)
|
|
Packit |
6c4009 |
if (unlink (files[i]) != 0 && errno != ENOENT)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("unlink (\"%s\"): %m", files[i]);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return true if PATH exists in the file system. */
|
|
Packit |
6c4009 |
static bool
|
|
Packit |
6c4009 |
file_exists (const char *path)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return access (path, F_OK) == 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check that PATH exists and has size EXPECTED_SIZE. */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
check_size (const char *path, off64_t expected_size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct stat64 st;
|
|
Packit |
6c4009 |
xstat (path, &st);
|
|
Packit |
6c4009 |
if (st.st_size != expected_size)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("file \"%s\": expected size %lld, actual size %lld",
|
|
Packit |
6c4009 |
path, (unsigned long long int) expected_size,
|
|
Packit |
6c4009 |
(unsigned long long int) st.st_size);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Rename tests where the target does not exist. */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
rename_without_existing_target (unsigned int flags)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "");
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (AT_FDCWD, old_path, AT_FDCWD, new_path, flags), 0);
|
|
Packit |
6c4009 |
TEST_VERIFY (!file_exists (old_path));
|
|
Packit |
6c4009 |
TEST_VERIFY (file_exists (new_path));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "");
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (directory_fd, "old", AT_FDCWD, new_path, flags), 0);
|
|
Packit |
6c4009 |
TEST_VERIFY (!file_exists (old_path));
|
|
Packit |
6c4009 |
TEST_VERIFY (file_exists (new_path));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "");
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (directory_fd, "old", subdirectory_fd, "file", 0),
|
|
Packit |
6c4009 |
0);
|
|
Packit |
6c4009 |
TEST_VERIFY (!file_exists (old_path));
|
|
Packit |
6c4009 |
TEST_VERIFY (file_exists (subdir_path));
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Tests with zero flags argument. These are expected to succeed
|
|
Packit |
6c4009 |
because this renameat2 variant can be implemented with
|
|
Packit |
6c4009 |
renameat. */
|
|
Packit |
6c4009 |
rename_without_existing_target (0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* renameat2 without flags replaces an existing destination. */
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "123");
|
|
Packit |
6c4009 |
support_write_file_string (new_path, "1234");
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (AT_FDCWD, old_path, AT_FDCWD, new_path, 0), 0);
|
|
Packit |
6c4009 |
TEST_VERIFY (!file_exists (old_path));
|
|
Packit |
6c4009 |
check_size (new_path, 3);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now we need to check for kernel support of renameat2 with
|
|
Packit |
6c4009 |
flags. */
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "");
|
|
Packit |
6c4009 |
if (renameat2 (AT_FDCWD, old_path, AT_FDCWD, new_path, RENAME_NOREPLACE)
|
|
Packit |
6c4009 |
!= 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (errno == EINVAL)
|
|
Packit |
6c4009 |
puts ("warning: no support for renameat2 with flags");
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("renameat2 probe failed: %m");
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We have full renameat2 support. */
|
|
Packit |
6c4009 |
rename_without_existing_target (RENAME_NOREPLACE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now test RENAME_NOREPLACE with an existing target. */
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "123");
|
|
Packit |
6c4009 |
support_write_file_string (new_path, "1234");
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (AT_FDCWD, old_path, AT_FDCWD, new_path,
|
|
Packit |
6c4009 |
RENAME_NOREPLACE), -1);
|
|
Packit |
6c4009 |
TEST_COMPARE (errno, EEXIST);
|
|
Packit |
6c4009 |
check_size (old_path, 3);
|
|
Packit |
6c4009 |
check_size (new_path, 4);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "123");
|
|
Packit |
6c4009 |
support_write_file_string (new_path, "1234");
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (directory_fd, "old", AT_FDCWD, new_path,
|
|
Packit |
6c4009 |
RENAME_NOREPLACE), -1);
|
|
Packit |
6c4009 |
TEST_COMPARE (errno, EEXIST);
|
|
Packit |
6c4009 |
check_size (old_path, 3);
|
|
Packit |
6c4009 |
check_size (new_path, 4);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
delete_all_files ();
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "123");
|
|
Packit |
6c4009 |
support_write_file_string (subdir_path, "1234");
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (directory_fd, "old", subdirectory_fd, "file",
|
|
Packit |
6c4009 |
RENAME_NOREPLACE), -1);
|
|
Packit |
6c4009 |
TEST_COMPARE (errno, EEXIST);
|
|
Packit |
6c4009 |
check_size (old_path, 3);
|
|
Packit |
6c4009 |
check_size (subdir_path, 4);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The flag combination of RENAME_NOREPLACE and RENAME_EXCHANGE
|
|
Packit |
6c4009 |
is invalid. */
|
|
Packit |
6c4009 |
TEST_COMPARE (renameat2 (directory_fd, "ignored",
|
|
Packit |
6c4009 |
subdirectory_fd, "ignored",
|
|
Packit |
6c4009 |
RENAME_NOREPLACE | RENAME_EXCHANGE), -1);
|
|
Packit |
6c4009 |
TEST_COMPARE (errno, EINVAL);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Create all the pathnames to avoid warnings from the test
|
|
Packit |
6c4009 |
harness. */
|
|
Packit |
6c4009 |
support_write_file_string (old_path, "");
|
|
Packit |
6c4009 |
support_write_file_string (new_path, "");
|
|
Packit |
6c4009 |
support_write_file_string (subdir_path, "");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
free (directory);
|
|
Packit |
6c4009 |
free (subdirectory);
|
|
Packit |
6c4009 |
free (old_path);
|
|
Packit |
6c4009 |
free (new_path);
|
|
Packit |
6c4009 |
free (subdir_path);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
xclose (directory_fd);
|
|
Packit |
6c4009 |
xclose (subdirectory_fd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define PREPARE prepare
|
|
Packit |
6c4009 |
#include <support/test-driver.c>
|