File: sys-copy_file_range.c

package info (click to toggle)
valgrind 1%3A3.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 170,080 kB
  • sloc: ansic: 782,981; exp: 26,134; xml: 22,780; asm: 14,072; cpp: 7,903; makefile: 6,768; perl: 6,097; sh: 5,669; javascript: 981; awk: 148
file content (67 lines) | stat: -rw-r--r-- 1,746 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "../../memcheck.h"

int main(int argc, char **argv)
{
    int fd_in, fd_out;
    struct stat stat;
    loff_t len, ret;

    fd_in = open("copy_file_range_source", O_CREAT | O_RDWR, 0644);
    if (fd_in == -1) {
        perror("open copy_file_range_source");
        exit(EXIT_FAILURE);
    }

    if (write(fd_in, "foo bar\n", 8) != 8) {
        perror("writing to the copy_file_range_source");
        exit(EXIT_FAILURE);
    }
    lseek(fd_in, 0, SEEK_SET);

    if (fstat(fd_in, &stat) == -1) {
        perror("fstat");
        exit(EXIT_FAILURE);
    }

    len = stat.st_size;

    fd_out = open("copy_file_range_dest", O_CREAT | O_WRONLY | O_TRUNC, 0644);
    if (fd_out == -1) {
        perror("open copy_file_range_dest");
        exit(EXIT_FAILURE);
    }

    /* Check copy_file_range called with the correct arguments works. */
    do {
        ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
        if (ret == -1) {
            perror("copy_file_range");
            exit(EXIT_FAILURE);
        }

        len -= ret;
    } while (len > 0);

    /* Check valgrind will produce expected warnings for the
       various wrong arguments. */
    do {
        void *t = 0; VALGRIND_MAKE_MEM_UNDEFINED (&t, sizeof (void *));
        void *z = (void *) -1;

        ret = copy_file_range(fd_in, t, fd_out, NULL, len, 0);
        ret = copy_file_range(fd_in, NULL, fd_out, z, len, 0);
        ret = copy_file_range(- 1, NULL, - 1, NULL, len, 0);
    } while (0);

    close(fd_in);
    close(fd_out);
    unlink("copy_file_range_source");
    unlink("copy_file_range_dest");
    exit(EXIT_SUCCESS);
}