c5f945a83191772f04adc1583785c9658721f321
[xfstests-dev.git] / dmapi / src / suite1 / cmd / probe_hole.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 <lib/hsm.h>
20
21 #include <getopt.h>
22 #include <string.h>
23
24
25 /*---------------------------------------------------------------------------
26
27 Test program used to test the DMAPI function dm_probe_hole().  The
28 command line is:
29
30         probe_hole [-o offset] [-l length] [-s sid] pathname
31
32 where pathname is the name of a file, offset is the offset of the start of
33 the proposed punch, and length is the length of the punch.  sid is the
34 session ID whose events you you are interested in.
35
36 ----------------------------------------------------------------------------*/
37
38 #ifndef linux
39 extern  char    *sys_errlist[];
40 #endif
41 extern  int     optind;
42 extern  char    *optarg;
43
44
45 char    *Progname;
46
47
48 static void
49 usage(void)
50 {
51         fprintf(stderr, "usage:\t%s [-o offset] [-l length] "
52                 "[-s sid] pathname\n", Progname);
53         exit(1);
54 }
55
56
57 int
58 main(
59         int     argc, 
60         char    **argv)
61 {
62         dm_sessid_t     sid = DM_NO_SESSION;
63         char            *pathname = NULL;
64         dm_off_t        offset = 0;
65         dm_size_t       length = 0;
66         dm_off_t        roffp;
67         dm_size_t       rlenp;
68         void            *hanp;
69         size_t          hlen;
70         char            *name;
71         int             opt;
72
73         Progname = strrchr(argv[0], '/');
74         if (Progname) {
75                 Progname++;
76         } else {
77                 Progname = argv[0];
78         }
79
80         /* Crack and validate the command line options. */
81
82         while ((opt = getopt(argc, argv, "o:l:s:")) != EOF) {
83                 switch (opt) {
84                 case 'o':
85                         offset = atol(optarg);
86                         break;
87                 case 'l':
88                         length = atol(optarg);
89                         break;
90                 case 's':
91                         sid = atol(optarg);
92                         break;
93                 case '?':
94                         usage();
95                 }
96         }
97         if (optind + 1 != argc)
98                 usage();
99         pathname = argv[optind];
100
101         if (dm_init_service(&name) == -1)  {
102                 fprintf(stderr, "Can't initialize the DMAPI\n");
103                 exit(1);
104         }
105         if (sid == DM_NO_SESSION)
106                 find_test_session(&sid);
107
108         /* Get the file's handle. */
109
110         if (dm_path_to_handle(pathname, &hanp, &hlen)) {
111                 fprintf(stderr, "can't get handle for file %s\n", pathname);
112                 exit(1);
113         }
114
115         if (dm_probe_hole(sid, hanp, hlen, DM_NO_TOKEN, offset, length,
116             &roffp, &rlenp)) {
117                 fprintf(stderr, "dm_probe_hole failed, %s\n",
118                         strerror(errno));
119                 exit(1);
120         }
121         fprintf(stdout, "roffp is %lld, rlenp is %llu\n",
122                 (long long) roffp, (unsigned long long) rlenp);
123         dm_handle_free(hanp, hlen);
124         exit(0);
125 }