common/rc: factor out _scratch_xfs_[get|set]_sb_field
[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 #include <stdlib.h>
45
46
47 char * Progname;
48 off_t   len;                    /* length of file 1 */
49 off_t   offset = 0;
50 int     print_flags_set = 1;
51
52 typedef struct  fflag {
53         int     arg;            /* != 0 if ars specified */
54         int     value;          /* flags value */
55 } fflag_t;
56
57
58 typedef enum ftype {    /* flag bit types */
59         FL_MAP, FL_PROT, FL_OPEN, FL_MAX
60 } ftype_t;
61
62 typedef struct  mfile   {
63         fflag_t flags[FL_MAX];
64         char    *path;
65         int     fd;
66         struct  stat    st;
67 #ifdef linux
68         void *p;
69 #else
70         addr_t  p;
71 #endif
72 } mfile_t;
73
74
75 #define FLAG(symbol,type) { # symbol , symbol, type }
76 #define MAP_NONE        0
77
78 static  struct  {
79         char    *name;
80         int     value;
81         ftype_t type;
82 } Flags[] = {
83         FLAG(O_RDONLY, FL_OPEN),
84         FLAG(O_WRONLY, FL_OPEN),
85         FLAG(O_RDWR, FL_OPEN),
86         FLAG(O_NDELAY, FL_OPEN),
87         FLAG(O_NONBLOCK, FL_OPEN),
88         FLAG(O_APPEND, FL_OPEN),
89         FLAG(O_SYNC, FL_OPEN),
90         FLAG(O_TRUNC, FL_OPEN),
91         FLAG(O_CREAT, FL_OPEN),
92         FLAG(O_DIRECT, FL_OPEN),
93         FLAG(PROT_NONE, FL_PROT),
94         FLAG(PROT_READ, FL_PROT),
95         FLAG(PROT_WRITE, FL_PROT),
96         FLAG(PROT_EXEC, FL_PROT),
97         FLAG(MAP_SHARED, FL_MAP),
98         FLAG(MAP_PRIVATE, FL_MAP),
99         FLAG(MAP_FIXED, FL_MAP),
100         FLAG(MAP_NONE, FL_MAP),
101 };
102
103 int     num_Flags = sizeof(Flags)/sizeof(Flags[0]);
104
105
106 mfile_t *ifile, *ofile;
107 mfile_t *hfile; /* Hack job */
108 static int      hack = 0;
109         
110 static  mfile_t *new_mfile(void);
111 static int mfile_opt(char * s, mfile_t * f);
112 static  void print_flags(char *s, mfile_t *f);
113 static void Usage(void);
114
115 int
116 main(int argc, char * argv[])
117 {
118         int     opt;
119
120         Progname = strrchr(argv[0], '/');
121         if (Progname == NULL)
122                 Progname = argv[0];
123         else
124                 Progname++;
125
126         ifile = new_mfile();
127         ofile = new_mfile();
128         hfile = new_mfile();
129         if (ifile == NULL || ofile == NULL || hfile == NULL) {
130                 fprintf(stderr,"%s: malloc failure.\n", Progname);
131                 exit (1);
132         }
133
134         /* Set default flags */
135         ifile->flags[FL_MAP].value = MAP_PRIVATE;
136         ifile->flags[FL_PROT].value = PROT_READ;
137         ifile->flags[FL_OPEN].value = O_RDONLY;
138         ofile->flags[FL_MAP].value = MAP_SHARED;
139         ofile->flags[FL_PROT].value = PROT_WRITE;
140         ofile->flags[FL_OPEN].value = O_RDWR|O_CREAT;
141
142         while ((opt = getopt(argc, argv, "i:o:h:d")) != EOF) {
143                 switch(opt) {
144                 case 'i':
145                         if (mfile_opt(optarg, ifile) != 0) {
146                                 fprintf(stderr, "%s: Invalid -i option %s\n",
147                                         Progname, optarg);
148                                 Usage();
149                         }
150                         break;
151
152                 case 'o':
153                         if (mfile_opt(optarg, ofile) != 0) {
154                                 fprintf(stderr, "%s: Invalid -o option %s\n",
155                                         Progname, optarg);
156                                 Usage();
157                         }
158                         break;
159
160                 case 'h':
161                         if (mfile_opt(optarg, hfile) != 0) {
162                                 fprintf(stderr, "%s: Invalid -h option %s\n",
163                                         Progname, optarg);
164                                 Usage();
165                         }
166                         hack = 1;
167                         break;
168
169                 case 'd':
170                         print_flags_set ^= 1;
171                         break;
172                 case '?':
173                         Usage();
174                 }
175         }
176
177         if (optind+1 > argc)
178                 Usage();
179
180         ifile->path = argv[optind++];
181         ofile->path = argv[optind++];
182
183         if (optind != argc)     /* Extra args on command line */
184                 Usage();
185
186         if (stat(ifile->path, &(ifile->st)) < 0) {
187                 fprintf(stderr,"%s: stat of %s failed.\n",
188                                                 Progname, ifile->path);
189                 perror(ifile->path);
190                 exit(2);
191         }
192
193         len = ifile->st.st_size;
194
195         ifile->fd = open(ifile->path, ifile->flags[FL_OPEN].value);
196         if (ifile->fd < 0) {
197                 fprintf(stderr,"%s: cannot open %s\n", Progname, ifile->path);
198                 perror(ifile->path);
199                 exit(2);
200         }
201
202
203         ofile->fd = open(ofile->path, ofile->flags[FL_OPEN].value, 0644);
204         if (ofile->fd < 0) {
205                 fprintf(stderr,"%s: cannot open %s\n", Progname, ofile->path);
206                 perror(ofile->path);
207                 exit(3);
208         }
209
210         if (print_flags_set) {
211                 print_flags("Input ", ifile);
212                 print_flags("Output", ofile);
213                 if (hack)
214                         print_flags("Hack  ", hfile);
215         }
216
217
218         ifile->p = mmap(NULL, len, ifile->flags[FL_PROT].value,
219                                 ifile->flags[FL_MAP].value, ifile->fd, 0);
220         if (ifile->p == MAP_FAILED) {
221                 fprintf(stderr,"%s: cannot mmap %s\n", Progname, ifile->path);
222                 perror(ifile->path);
223                 exit(2);
224         }
225
226         ofile->p = mmap(NULL, len, ofile->flags[FL_PROT].value,
227                                 ofile->flags[FL_MAP].value , ofile->fd, 0);
228                 if (ofile->p == MAP_FAILED) {
229                 fprintf(stderr,"%s: cannot mmap %s\n", Progname, ofile->path);
230                 perror(ofile->path);
231                 exit(3);
232         }
233
234         if (hack) {
235                 int     error;
236
237                 error = mprotect(ofile->p, len, hfile->flags[FL_PROT].value);
238                 if (error) {
239                         fprintf(stderr,"%s: mprotect call failed.\n", Progname);
240                         perror("mprotect");
241                         exit(3);
242                 }
243         }
244         
245         bcopy(ifile->p, ofile->p, len);
246
247         printf("%s complete.\n", Progname);
248         return 0;
249 }
250
251 static mfile_t *
252 new_mfile(void)
253 {
254         mfile_t *ptr = (mfile_t *)malloc(sizeof(*ptr));
255         if (ptr)
256                 bzero(ptr, sizeof *ptr);
257
258         return  ptr;
259 }
260
261
262 static  int
263 mfile_opt(char * s, mfile_t *f)
264 {
265         int     i;
266         ftype_t type;
267
268         for (i = 0; i < num_Flags; i++) {
269                 if(!strcasecmp(Flags[i].name, s)) {
270
271                         /* Zero value if this is 1st arg of this type */
272
273                         type = Flags[i].type;
274                         if (f->flags[type].arg++ == 0) 
275                                 f->flags[type].value = 0;
276                         f->flags[type].value |= Flags[i].value;
277                         return 0;
278                 }
279         }
280         return -1;      /* error - string not found */
281 }
282
283 static void
284 Usage(void)
285 {
286         int     i;
287
288         fprintf(stderr, 
289                 "Usage: %s [-d] [-i flag] [-i flag] [-o flag] ... file1 file2\n",
290                 Progname);
291         fprintf(stderr, "Valid flag values are:\n");
292
293         for (i = 0; i < num_Flags; i++) {
294                 fprintf(stderr,"%15s",Flags[i].name);
295                 if ((i+1)%4 == 0 || i == num_Flags-1)
296                         fprintf(stderr,"\n");
297                 else
298                         fprintf(stderr,",");
299         }
300         exit(1);
301 }
302
303 static  void
304 print_flags(char *s, mfile_t *f)
305 {
306         int             i;
307         ftype_t         type;
308
309         printf("DEBUG - %s flags:\n", s);
310         for (i = 0; i < num_Flags; i++) {
311                 type = Flags[i].type;
312                 if (type == FL_OPEN && Flags[i].value == O_RDONLY && 
313                         ((f->flags[type].value) & 3) == 0) 
314                                 /* Hack to print out O_RDONLY */
315                                 printf("\t%s\n", Flags[i].name);
316                 else if ((Flags[i].value & (f->flags[type].value)) != 0)
317                         printf("\t%s\n", Flags[i].name);
318         }
319 }