1 /* Copyright (C) 2006-2008 MySQL AB
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
15 
16 #include <tap.h>                                /* Includes my_global.h */
17 #include <my_sys.h>
18 #include <my_dir.h>
19 #include "test_file.h"
20 
21 
22 /*
23   Check that file contance correspond to descriptor
24 
25   SYNOPSIS
26     test_file()
27     file                 File to test
28     file_name            Path (and name) of file which is tested
29     size                 size of file
30     buff_size            size of buffer which is enought to check the file
31     desc                 file descriptor to check with
32 
33   RETURN
34     1 file if OK
35     0 error
36 */
37 
test_file(PAGECACHE_FILE file,char * file_name,off_t size,size_t buff_size,struct file_desc * desc)38 int test_file(PAGECACHE_FILE file, char *file_name,
39               off_t size, size_t buff_size, struct file_desc *desc)
40 {
41   unsigned char *buffr= my_malloc(buff_size, MYF(0));
42   off_t pos= 0;
43   size_t byte;
44   int step= 0;
45   int res= 1;                                   /* ok */
46 
47 #ifdef __WIN__
48   /*
49     On Windows, the info returned by stat(), specifically file length
50     is not necessarily current, because this is the behavior of
51     underlying FindFirstFile() function.
52   */
53   WIN32_FILE_ATTRIBUTE_DATA file_attr;
54   LARGE_INTEGER li;
55   if(GetFileAttributesEx(file_name, GetFileExInfoStandard, &file_attr) == 0)
56   {
57     diag("Can't GetFileAttributesEx %s (errno: %lu)\n", file_name,
58       GetLastError());
59     res= 0;
60     goto err;
61   }
62   li.HighPart= file_attr.nFileSizeHigh;
63   li.LowPart=  file_attr.nFileSizeLow;
64   if(li.QuadPart !=  size)
65   {
66     diag("file %s size is %llu (should be %llu)\n",
67       file_name, (ulonglong)size, (ulonglong)li.QuadPart);
68     res= 0;                                       /* failed */
69     /* continue to get more information */
70   }
71 #else
72   MY_STAT stat_buff, *stat;
73   if ((stat= my_stat(file_name, &stat_buff, MYF(0))) == NULL)
74   {
75     diag("Can't stat() %s (errno: %d)\n", file_name, errno);
76     res= 0;
77     goto err;
78   }
79   if (stat->st_size != size)
80   {
81     diag("file %s size is %lu (should be %lu)\n",
82          file_name, (ulong) stat->st_size, (ulong) size);
83     res= 0;                                       /* failed */
84     /* continue to get more information */
85   }
86 #endif
87 
88   /* check content */
89   my_seek(file.file, 0, SEEK_SET, MYF(MY_WME));
90   while (desc[step].length != 0)
91   {
92     if (my_read(file.file, buffr, desc[step].length, MYF(0)) !=
93         desc[step].length)
94     {
95       diag("Can't read %u bytes from %s (file: %d  errno: %d)\n",
96            (uint)desc[step].length, file_name, file.file, errno);
97       res= 0;
98       goto err;
99     }
100     for (byte= 0; byte < desc[step].length; byte++)
101     {
102       if (buffr[byte] != desc[step].content)
103       {
104         diag("content of %s mismatch 0x%x in position %lu instead of 0x%x\n",
105              file_name, (uint) buffr[byte], (ulong) (pos + byte),
106              desc[step].content);
107         res= 0;
108         goto err;
109       }
110     }
111     pos+= desc[step].length;
112     step++;
113   }
114 
115 err:
116   my_free(buffr);
117   return res;
118 }
119