Whamcloud - gitweb
LU-1606 api: Rename liblustreapi.h -> lustreapi.h
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/sendfile.h>
42 #include <sys/stat.h>
43 #include <sys/socket.h>
44
45 #include <lustre/lustreapi.h>
46
47 #define syserr(str) { perror(str); exit(-1); }
48
49 int main(int argc, char *argv[])
50 {
51         char *sfile, *tfile;
52         struct stat stbuf;
53         int size;
54         unsigned long bufsize = 1024 * 1024;
55         int infd, outfd;
56         int sd[2];
57         int rc;
58         char *buf;
59         char cmd[1024];
60         loff_t pos;
61
62         if (argc < 3) {
63                 fprintf(stderr, "%s <source file> <dest file>\n", argv[0]);
64                 exit(-1);
65         }
66
67         sfile = argv[1];
68         tfile = argv[2];
69
70         if (stat(sfile, &stbuf) < 0) {
71                 if (errno == ENOENT) {
72                         /* assume doing non-object file testing */
73                         infd = open(sfile, O_LOV_DELAY_CREATE|O_CREAT|O_RDWR,
74                                     0644);
75                         if (infd < 0)
76                                 syserr("open source file:");
77
78                         size = random() % (1 * 1024 * 1024) + 1024;
79                         if (ftruncate(infd, (off_t)size) < 0)
80                                 syserr("truncate file error:");
81                 } else {
82                         syserr("stat file: ");
83                 }
84         } else if (S_ISREG(stbuf.st_mode)) {
85                 size = (int)stbuf.st_size;
86                 infd = open(sfile, O_RDONLY, 0644);
87                 if (infd < 0)
88                         syserr("Open an existing file error:");
89         } else {
90                 fprintf(stderr, "%s is not a regular file\n", sfile);
91                 exit(-1);
92         }
93
94         outfd = open(tfile, O_WRONLY|O_TRUNC|O_CREAT, 0666);
95         if (outfd < 0)
96                 syserr("open dest file:");
97
98         rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, sd);
99         if (rc < 0)
100                 syserr("socketpair");
101
102         rc = fcntl(sd[0], F_SETFL, O_NONBLOCK);
103         if (rc < 0)
104                 syserr("fcntl");
105
106         rc = setsockopt(sd[0], SOL_SOCKET, SO_SNDBUF,
107                         &bufsize, sizeof(bufsize));
108         if (rc)
109                 syserr("setsockopt");
110
111         srandom(time(NULL));
112
113         pos = 0;
114         while (size > 0) {
115                 int rc2;
116                 size_t seg_size;
117
118                 seg_size = random() % bufsize + 1;
119                 if (seg_size > size)
120                         seg_size = size;
121
122                 while (seg_size) {
123                         rc = sendfile(sd[0], infd, &pos, seg_size);
124                         if (rc < 0)
125                                 syserr("sendfile:");
126
127                         seg_size -= rc;
128                         size -= rc;
129                         if (size == 0)
130                                 close(sd[0]);
131
132                         buf = malloc(rc);
133                         if (read(sd[1], buf, rc) < 0)
134                                 syserr("read from socket:");
135
136                         rc2 = write(outfd, buf, rc);
137                         if (rc2 != rc)
138                                 syserr("write dest file error:");
139                         free(buf);
140                 }
141         }
142         close(sd[1]), close(infd), close(outfd);
143
144         sprintf(cmd, "cmp %s %s\n", sfile, tfile);
145         return system(cmd);
146 }