test 194, test tricky mapping/conversion around holes
[xfstests-dev.git] / dmapi / src / suite2 / src / mmap.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 /*
19  *      This routine simulates
20  *      
21  *      cp      file1 file2
22  *
23  *
24  *      It is a demo program which does the copy by memory mapping each of the
25  *      files and then doing a byte at a time memory copy.
26  *
27  */
28 #ifdef linux
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE
31 #endif
32 #endif
33
34 #include <unistd.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <sys/mman.h>
39 #include <sys/fcntl.h>
40 #include <sys/stat.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45
46 char * Progname;
47 off_t   len;                    /* length of file 1 */
48 off_t   offset = 0;
49 int     print_flags_set = 1;
50
51 typedef struct  fflag {
52         int     arg;            /* != 0 if ars specified */
53         int     value;          /* flags value */
54 } fflag_t;
55
56
57 typedef enum ftype {    /* flag bit types */
58         FL_MAP, FL_PROT, FL_OPEN, FL_MAX
59 } ftype_t;
60
61 typedef struct  mfile   {
62         fflag_t flags[FL_MAX];
63         char    *path;
64         int     fd;
65         struct  stat    st;
66 #ifdef linux
67         void *p;
68 #else
69         addr_t  p;
70 #endif
71 } mfile_t;
72
73
74 #define FLAG(symbol,type) { # symbol , symbol, type }
75 #define MAP_NONE        0
76
77 static  struct  {
78         char    *name;
79         int     value;
80         ftype_t type;
81 } Flags[] = {
82         FLAG(O_RDONLY, FL_OPEN),
83         FLAG(O_WRONLY, FL_OPEN),
84         FLAG(O_RDWR, FL_OPEN),
85         FLAG(O_NDELAY, FL_OPEN),
86         FLAG(O_NONBLOCK, FL_OPEN),
87         FLAG(O_APPEND, FL_OPEN),
88         FLAG(O_SYNC, FL_OPEN),
89         FLAG(O_TRUNC, FL_OPEN),
90         FLAG(O_CREAT, FL_OPEN),
91         FLAG(O_DIRECT, FL_OPEN),
92         FLAG(PROT_NONE, FL_PROT),
93         FLAG(PROT_READ, FL_PROT),
94         FLAG(PROT_WRITE, FL_PROT),
95         FLAG(PROT_EXEC, FL_PROT),
96 #ifdef __sgi
97         FLAG(PROT_EXECUTE, FL_PROT),
98 #endif
99         FLAG(MAP_SHARED, FL_MAP),
100         FLAG(MAP_PRIVATE, FL_MAP),
101         FLAG(MAP_FIXED, FL_MAP),
102 #ifdef __sgi
103         FLAG(MAP_RENAME, FL_MAP),
104         FLAG(MAP_AUTOGROW, FL_MAP),
105         FLAG(MAP_LOCAL, FL_MAP),
106         FLAG(MAP_AUTORESRV, FL_MAP),
107 #endif
108         FLAG(MAP_NONE, FL_MAP),
109 };
110
111 int     num_Flags = sizeof(Flags)/sizeof(Flags[0]);
112
113
114 mfile_t *ifile, *ofile;
115 mfile_t *hfile; /* Hack job */
116 static int      hack = 0;
117         
118 static  mfile_t *new_mfile(void);
119 static int mfile_opt(char * s, mfile_t * f);
120 static  void print_flags(char *s, mfile_t *f);
121 static void Usage(void);
122
123 main(int argc, char * argv[])
124 {
125         int     opt;
126
127         if ((Progname = strrchr(argv[0], '/')) == NULL)
128                 Progname = argv[0];
129         else
130                 Progname++;
131
132         ifile = new_mfile();
133         ofile = new_mfile();
134         hfile = new_mfile();
135         if (ifile == NULL || ofile == NULL || hfile == NULL) {
136                 fprintf(stderr,"%s: malloc failure.\n", Progname);
137                 exit (1);
138         }
139
140         /* Set default flags */
141         ifile->flags[FL_MAP].value = MAP_PRIVATE;
142         ifile->flags[FL_PROT].value = PROT_READ;
143         ifile->flags[FL_OPEN].value = O_RDONLY;
144         ofile->flags[FL_MAP].value = MAP_SHARED;
145 #ifdef __sgi
146         ofile->flags[FL_MAP].value = MAP_AUTOGROW;
147 #endif
148         ofile->flags[FL_PROT].value = PROT_WRITE;
149         ofile->flags[FL_OPEN].value = O_RDWR|O_CREAT;
150
151         while ((opt = getopt(argc, argv, "i:o:h:d")) != EOF) {
152                 switch(opt) {
153                 case 'i':
154                         if (mfile_opt(optarg, ifile) != 0) {
155                                 fprintf(stderr, "%s: Invalid -i option %s\n",
156                                         Progname, optarg);
157                                 Usage();
158                         }
159                         break;
160
161                 case 'o':
162                         if (mfile_opt(optarg, ofile) != 0) {
163                                 fprintf(stderr, "%s: Invalid -o option %s\n",
164                                         Progname, optarg);
165                                 Usage();
166                         }
167                         break;
168
169                 case 'h':
170                         if (mfile_opt(optarg, hfile) != 0) {
171                                 fprintf(stderr, "%s: Invalid -h option %s\n",
172                                         Progname, optarg);
173                                 Usage();
174                         }
175                         hack = 1;
176                         break;
177
178                 case 'd':
179                         print_flags_set ^= 1;
180                         break;
181                 case '?':
182                         Usage();
183                 }
184         }
185
186         if (optind+1 > argc)
187                 Usage();
188
189         ifile->path = argv[optind++];
190         ofile->path = argv[optind++];
191
192         if (optind != argc)     /* Extra args on command line */
193                 Usage();
194
195         if (stat(ifile->path, &(ifile->st)) < 0) {
196                 fprintf(stderr,"%s: stat of %s failed.\n",
197                                                 Progname, ifile->path);
198                 perror(ifile->path);
199                 exit(2);
200         }
201
202         len = ifile->st.st_size;
203
204         ifile->fd = open(ifile->path, ifile->flags[FL_OPEN].value);
205         if (ifile->fd < 0) {
206                 fprintf(stderr,"%s: cannot open %s\n", Progname, ifile->path);
207                 perror(ifile->path);
208                 exit(2);
209         }
210
211
212         ofile->fd = open(ofile->path, ofile->flags[FL_OPEN].value, 0644);
213         if (ofile->fd < 0) {
214                 fprintf(stderr,"%s: cannot open %s\n", Progname, ofile->path);
215                 perror(ofile->path);
216                 exit(3);
217         }
218
219         if (print_flags_set) {
220                 print_flags("Input ", ifile);
221                 print_flags("Output", ofile);
222                 if (hack)
223                         print_flags("Hack  ", hfile);
224         }
225
226
227         ifile->p = mmap(NULL, len, ifile->flags[FL_PROT].value,
228                                 ifile->flags[FL_MAP].value, ifile->fd, 0);
229         if (ifile->p == MAP_FAILED) {
230                 fprintf(stderr,"%s: cannot mmap %s\n", Progname, ifile->path);
231                 perror(ifile->path);
232                 exit(2);
233         }
234
235         ofile->p = mmap(NULL, len, ofile->flags[FL_PROT].value,
236                                 ofile->flags[FL_MAP].value , ofile->fd, 0);
237                 if (ofile->p == MAP_FAILED) {
238                 fprintf(stderr,"%s: cannot mmap %s\n", Progname, ofile->path);
239                 perror(ofile->path);
240                 exit(3);
241         }
242
243         if (hack) {
244                 int     error;
245
246                 error = mprotect(ofile->p, len, hfile->flags[FL_PROT].value);
247                 if (error) {
248                         fprintf(stderr,"%s: mprotect call failed.\n", Progname);
249                         perror("mprotect");
250                         exit(3);
251                 }
252         }
253         
254         bcopy(ifile->p, ofile->p, len);
255
256         printf("%s complete.\n", Progname);
257         return 0;
258 }
259
260 static mfile_t *
261 new_mfile(void)
262 {
263         mfile_t *ptr = (mfile_t *)malloc(sizeof(*ptr));
264         if (ptr)
265                 bzero(ptr, sizeof *ptr);
266
267         return  ptr;
268 }
269
270
271 static  int
272 mfile_opt(char * s, mfile_t *f)
273 {
274         int     i;
275         ftype_t type;
276
277         for (i = 0; i < num_Flags; i++) {
278                 if(!strcasecmp(Flags[i].name, s)) {
279
280                         /* Zero value if this is 1st arg of this type */
281
282                         type = Flags[i].type;
283                         if (f->flags[type].arg++ == 0) 
284                                 f->flags[type].value = 0;
285                         f->flags[type].value |= Flags[i].value;
286                         return 0;
287                 }
288         }
289         return -1;      /* error - string not found */
290 }
291
292 static void
293 Usage(void)
294 {
295         int     i;
296
297         fprintf(stderr, 
298                 "Usage: %s [-d] [-i flag] [-i flag] [-o flag] ... file1 file2\n",
299                 Progname);
300         fprintf(stderr, "Valid flag values are:\n");
301
302         for (i = 0; i < num_Flags; i++) {
303                 fprintf(stderr,"%15s",Flags[i].name);
304                 if ((i+1)%4 == 0 || i == num_Flags-1)
305                         fprintf(stderr,"\n");
306                 else
307                         fprintf(stderr,",");
308         }
309         exit(1);
310 }
311
312 static  void
313 print_flags(char *s, mfile_t *f)
314 {
315         int             i;
316         ftype_t         type;
317
318         printf("DEBUG - %s flags:\n", s);
319         for (i = 0; i < num_Flags; i++) {
320                 type = Flags[i].type;
321                 if (type == FL_OPEN && Flags[i].value == O_RDONLY && 
322                         ((f->flags[type].value) & 3) == 0) 
323                                 /* Hack to print out O_RDONLY */
324                                 printf("\t%s\n", Flags[i].name);
325                 else if ((Flags[i].value & (f->flags[type].value)) != 0)
326                         printf("\t%s\n", Flags[i].name);
327         }
328 }