Whamcloud - gitweb
LU-6142 tests: Fix style issues for chownmany.c
[fs/lustre-release.git] / lustre / tests / sendfile.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <time.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/sendfile.h>
40 #include <sys/stat.h>
41 #include <sys/socket.h>
42
43 #include <lustre/lustreapi.h>
44
45 #define syserr(str) { perror(str); exit(-1); }
46
47 int main(int argc, char *argv[])
48 {
49         char *sfile, *tfile;
50         struct stat stbuf;
51         int size;
52         unsigned long bufsize = 1024 * 1024;
53         int infd, outfd;
54         int sd[2];
55         int rc;
56         char *buf;
57         char cmd[1024];
58         loff_t pos;
59
60         if (argc < 3) {
61                 fprintf(stderr, "%s <source file> <dest file>\n", argv[0]);
62                 exit(-1);
63         }
64
65         sfile = argv[1];
66         tfile = argv[2];
67
68         if (stat(sfile, &stbuf) < 0) {
69                 if (errno == ENOENT) {
70                         /* assume doing non-object file testing */
71                         infd = open(sfile, O_LOV_DELAY_CREATE|O_CREAT|O_RDWR,
72                                     0644);
73                         if (infd < 0)
74                                 syserr("open source file:");
75
76                         size = random() % (1 * 1024 * 1024) + 1024;
77                         if (ftruncate(infd, (off_t)size) < 0)
78                                 syserr("truncate file error:");
79                 } else {
80                         syserr("stat file: ");
81                 }
82         } else if (S_ISREG(stbuf.st_mode)) {
83                 size = (int)stbuf.st_size;
84                 infd = open(sfile, O_RDONLY, 0644);
85                 if (infd < 0)
86                         syserr("Open an existing file error:");
87         } else {
88                 fprintf(stderr, "%s is not a regular file\n", sfile);
89                 exit(-1);
90         }
91
92         outfd = open(tfile, O_WRONLY|O_TRUNC|O_CREAT, 0666);
93         if (outfd < 0)
94                 syserr("open dest file:");
95
96         rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, sd);
97         if (rc < 0)
98                 syserr("socketpair");
99
100         rc = fcntl(sd[0], F_SETFL, O_NONBLOCK);
101         if (rc < 0)
102                 syserr("fcntl");
103
104         rc = setsockopt(sd[0], SOL_SOCKET, SO_SNDBUF,
105                         &bufsize, sizeof(bufsize));
106         if (rc)
107                 syserr("setsockopt");
108
109         srandom(time(NULL));
110
111         pos = 0;
112         while (size > 0) {
113                 int rc2;
114                 size_t seg_size;
115
116                 seg_size = random() % bufsize + 1;
117                 if (seg_size > size)
118                         seg_size = size;
119
120                 while (seg_size) {
121                         rc = sendfile(sd[0], infd, &pos, seg_size);
122                         if (rc < 0)
123                                 syserr("sendfile:");
124
125                         seg_size -= rc;
126                         size -= rc;
127                         if (size == 0)
128                                 close(sd[0]);
129
130                         buf = malloc(rc);
131                         if (read(sd[1], buf, rc) < 0)
132                                 syserr("read from socket:");
133
134                         rc2 = write(outfd, buf, rc);
135                         if (rc2 != rc)
136                                 syserr("write dest file error:");
137                         free(buf);
138                 }
139         }
140         close(sd[1]), close(infd), close(outfd);
141
142         snprintf(cmd, sizeof(cmd), "cmp %s %s\n", sfile, tfile);
143         return system(cmd);
144 }