f8b28683e0a7d261ca60804e390a6d1477577601
[xfstests-dev.git] / dmapi / src / suite2 / src / region_test.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 #include <sys/types.h>
20
21 #include <lib/hsm.h>
22 #include <lib/errtest.h>
23
24 #include <getopt.h>
25 #include <string.h>
26
27
28 /*---------------------------------------------------------------------------
29
30 Manual test for the DMAPI functions dm_set_region() and dm_get_region().
31
32 The command line is:
33
34         region_test [-Rv] [-s sid] [-l len] [-o offset] ls_path directory 
35
36 where 
37    -R
38         reuse the existing test file
39    pathname
40       is the name of a file
41    ls_path
42       is the path to a specific copy of ls, important only for its size
43    sid
44       is the session ID whose events you you are interested in.
45 ----------------------------------------------------------------------------*/
46
47 #define NELEM   1
48
49 #ifndef linux
50 extern  char    *sys_errlist[];
51 #endif
52 extern  int     optind;
53 extern  char    *optarg;
54
55
56 char    *Progname;
57
58
59 static void
60 usage(void)
61 {
62         fprintf(stderr, "usage:\t%s [-Rv] [-s sid] [-l len] [-o offset] ls_path pathname\n", Progname);
63         exit(1);
64 }
65
66
67 int
68 main(
69         int     argc, 
70         char    **argv)
71 {
72         dm_sessid_t     sid = DM_NO_SESSION;
73         char            *dir_name = NULL;
74         char            *ls_path = NULL;
75         char            command[1024];
76         char            test_file[128];
77         int             opt;
78         int             Vflag = 0;
79         dm_region_t     region = { 0, 0, 0 };
80         char            *name;
81         int             reuse_file = 0;
82         void            *hanp;
83         size_t           hlen;
84         dm_boolean_t    exactflag;
85
86         Progname = strrchr(argv[0], '/');
87         if (Progname) {
88                 Progname++;
89         } else {
90                 Progname = argv[0];
91         }
92
93         /* Crack and validate the command line options. */
94
95         while ((opt = getopt(argc, argv, "vo:l:s:R")) != EOF) {
96                 switch (opt) {
97                 case 'v':
98                         Vflag++;
99                         break;
100                 case 'R':
101                         reuse_file++;
102                         break;
103                 case 'o':
104                         region.rg_offset = atol(optarg);
105                         break;
106                 case 'l':
107                         region.rg_size = atol(optarg);
108                         break;
109                 case 's':
110                         sid = atol(optarg);
111                         break;
112                 case '?':
113                         usage();
114                 }
115         }
116         if (optind + 2 > argc)
117                 usage();
118         ls_path = argv[optind];
119         dir_name = argv[optind+1];
120
121         if (dm_init_service(&name) == -1)  {
122                 fprintf(stderr, "Can't initialize the DMAPI\n");
123                 exit(1);
124         }
125         if (sid == DM_NO_SESSION)
126                 find_test_session(&sid);
127
128         sprintf(test_file, "%s/DMAPI_test_file", dir_name);
129         if( !reuse_file ){
130                 sprintf(command, "cp %s %s\n", ls_path, test_file); 
131                 system(command);
132         }
133
134         if (dm_path_to_handle(test_file, &hanp, &hlen)) {
135                 fprintf(stderr, "can't get handle for file %s\n", test_file);
136                 exit(1);
137         }
138
139         region.rg_flags = DM_REGION_READ | DM_REGION_WRITE | DM_REGION_TRUNCATE;
140         if( dm_set_region( sid, hanp, hlen, DM_NO_TOKEN, NELEM,
141                           &region, &exactflag ) ){
142                 fprintf(stderr, "dm_set_region failed, err=%d\n", errno);
143                 dm_handle_free(hanp,hlen);
144                 exit(1);
145         }
146         if( exactflag == DM_FALSE )
147                 fprintf(stderr, "exact flag was false\n");
148
149         dm_handle_free(hanp, hlen);
150         exit(0);
151 }