4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/sendfile.h>
40 #include <sys/socket.h>
42 #include <lustre/lustreapi.h>
44 #define syserr(str) { perror(str); exit(-1); }
46 int main(int argc, char *argv[])
51 unsigned long bufsize = 1024 * 1024;
60 fprintf(stderr, "%s <source file> <dest file>\n", argv[0]);
67 if (stat(sfile, &stbuf) < 0) {
68 if (errno == ENOENT) {
69 /* assume doing non-object file testing */
71 O_LOV_DELAY_CREATE | O_CREAT | O_RDWR,
74 syserr("open source file:");
76 size = random() % (1 * 1024 * 1024) + 1024;
77 if (ftruncate(infd, (off_t)size) < 0)
78 syserr("truncate file error:");
80 syserr("stat file: ");
82 } else if (S_ISREG(stbuf.st_mode)) {
83 size = (int)stbuf.st_size;
84 infd = open(sfile, O_RDONLY, 0644);
86 syserr("Open an existing file error:");
88 fprintf(stderr, "%s is not a regular file\n", sfile);
92 outfd = open(tfile, O_WRONLY | O_TRUNC | O_CREAT, 0666);
94 syserr("open dest file:");
96 rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, sd);
100 rc = fcntl(sd[0], F_SETFL, O_NONBLOCK);
104 rc = setsockopt(sd[0], SOL_SOCKET, SO_SNDBUF,
105 &bufsize, sizeof(bufsize));
107 syserr("setsockopt");
116 seg_size = random() % bufsize + 1;
121 rc = sendfile(sd[0], infd, &pos, seg_size);
131 if (read(sd[1], buf, rc) < 0)
132 syserr("read from socket:");
134 rc2 = write(outfd, buf, rc);
136 syserr("write dest file error:");
140 close(sd[1]), close(infd), close(outfd);
142 snprintf(cmd, sizeof(cmd), "cmp %s %s\n", sfile, tfile);