1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "loader/dbufio.h"
40 #include <stdio.h>
41 #include <fcntl.h>
42 #include <toku_assert.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <string.h>
46 
47 enum { N  = 5 };
48 enum { M = 10 };
test1(size_t chars_per_file,size_t UU (bytes_per_read))49 static void test1 (size_t chars_per_file, size_t UU(bytes_per_read)) {
50     int fds[N];
51     char fnames[N][100];
52     size_t n_read[N];
53     int UU(n_live)=N;
54     for (int i=0; i<N; i++) {
55 	snprintf(fnames[i], 100, "dbufio-test-destroy-file%d.data", i);
56 	unlink(fnames[i]);
57 	fds[i] = open(fnames[i], O_CREAT|O_RDWR, S_IRWXU);
58 	//printf("fds[%d]=%d is %s\n", i, fds[i], fnames[i]);
59 	assert(fds[i]>=0);
60 	n_read[i]=0;
61 	for (size_t j=0; j<chars_per_file; j++) {
62 	    unsigned char c = (i+j)%256;
63 	    int r = toku_os_write(fds[i], &c, 1);
64 	    if (r!=0) {
65                 int er = get_maybe_error_errno();
66                 printf("fds[%d]=%d r=%d errno=%d (%s)\n", i, fds[i], r, er, strerror(er));
67             }
68 	    assert(r==0);
69 	}
70 	{
71 	    int r = lseek(fds[i], 0, SEEK_SET);
72 	    assert(r==0);
73 	}
74 
75     }
76     DBUFIO_FILESET bfs;
77     {
78 	int r = create_dbufio_fileset(&bfs, N, fds, M, false);
79 	assert(r==0);
80     }
81 
82     { int r = panic_dbufio_fileset(bfs, EIO); assert(r == 0); }
83 
84     {
85 	int r = destroy_dbufio_fileset(bfs);
86 	assert(r==0);
87     }
88     for (int i=0; i<N; i++) {
89 	{
90 	    int r = unlink(fnames[i]);
91 	    assert(r==0);
92 	}
93 	{
94 	    int r = close(fds[i]);
95 	    assert(r==0);
96 	}
97 	assert(n_read[i]==0);
98     }
99 }
100 
101 
102 
main(int argc,char * argv[])103 int main (int argc __attribute__((__unused__)), char *argv[]__attribute__((__unused__))) {
104 //    test1(0, 1);
105 //    test1(1, 1);
106 //    test1(15, 1);
107 //    test1(100, 1);
108     test1(30, 3); // 3 and M are relatively prime.  But 3 divides the file size.
109     return 0;
110 }
111