Ejemplo n.º 1
0
static void test_copy_file_fd(void) {
        char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
        char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
        _cleanup_close_ int in_fd = -1, out_fd = -1;
        char text[] = "boohoo\nfoo\n\tbar\n";
        char buf[64] = {0};

        log_info("%s", __func__);

        in_fd = mkostemp_safe(in_fn);
        assert_se(in_fd >= 0);
        out_fd = mkostemp_safe(out_fn);
        assert_se(out_fd >= 0);

        assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
        assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0);
        assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0);
        assert_se(lseek(out_fd, SEEK_SET, 0) == 0);

        assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
        assert_se(streq(buf, text));

        unlink(in_fn);
        unlink(out_fn);
}
Ejemplo n.º 2
0
static void read_header_files(void)
{
	unsigned long long size, check_size;
	char *path;
	int fd;

	path = get_tracing_file("events/header_page");
	fd = open(path, O_RDONLY);
	if (fd < 0)
		die("can't read '%s'", path);

	/* unfortunately, you can not stat debugfs files for size */
	size = get_size_fd(fd);

	write_or_die("header_page", 12);
	write_or_die(&size, 8);
	check_size = copy_file_fd(fd);
	close(fd);

	if (size != check_size)
		die("wrong size for '%s' size=%lld read=%lld",
		    path, size, check_size);
	put_tracing_file(path);

	path = get_tracing_file("events/header_event");
	fd = open(path, O_RDONLY);
	if (fd < 0)
		die("can't read '%s'", path);

	size = get_size_fd(fd);

	write_or_die("header_event", 13);
	write_or_die(&size, 8);
	check_size = copy_file_fd(fd);
	if (size != check_size)
		die("wrong size for '%s'", path);
	put_tracing_file(path);
	close(fd);
}
Ejemplo n.º 3
0
static unsigned long long copy_file(const char *file)
{
	unsigned long long size = 0;
	int fd;

	fd = open(file, O_RDONLY);
	if (fd < 0)
		die("Can't read '%s'", file);
	size = copy_file_fd(fd);
	close(fd);

	return size;
}
Ejemplo n.º 4
0
static void test_copy_file_fd(void) {
        char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
        char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
        _cleanup_close_ int in_fd = -1, out_fd = -1;
        char text[] = "boohoo\nfoo\n\tbar\n";
        char buf[64] = {0};

        in_fd = mkostemp_safe(in_fn, O_RDWR);
        assert_se(in_fd >= 0);
        out_fd = mkostemp_safe(out_fn, O_RDWR);
        assert_se(out_fd >= 0);

        assert_se(write_string_file(in_fn, text) == 0);
        assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd) < 0);
        assert_se(copy_file_fd(in_fn, out_fd) >= 0);
        assert_se(lseek(out_fd, SEEK_SET, 0) == 0);

        assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
        assert_se(streq(buf, text));

        unlink(in_fn);
        unlink(out_fn);
}
Ejemplo n.º 5
0
int copy_file(const char *from, const char *to, int flags, mode_t mode) {
        int fdt, r;

        assert(from);
        assert(to);

        fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
        if (fdt < 0)
                return -errno;

        r = copy_file_fd(from, fdt, true);
        if (r < 0) {
                close(fdt);
                unlink(to);
                return r;
        }

        if (close(fdt) < 0) {
                unlink_noerrno(to);
                return -errno;
        }

        return 0;
}