_scratch_mkfs_geom(): Filter out 'k' suffix from fs block size
[xfstests-dev.git] / dmapi / src / suite1 / cmd / downgrade_right.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <lib/hsm.h>
8
9 #include <string.h>
10 #include <getopt.h>
11
12 /*---------------------------------------------------------------------------
13
14 Test program used to test the DMAPI function dm_downgrade_right().  The
15 command line is:
16
17         downgrade_right {-F} [-s sid] token {pathname|handle}
18
19 where:
20 -F
21         when a pathname is specified, -F indicates that its filesystem handle
22         should be used rather than its file object handle.
23 sid
24         is the dm_sessid_t to use rather than the default test session.
25 token
26         is the dm_token_t to use.
27 {pathname|handle}
28         is either a handle, or is the pathname of a file whose handle is
29         to be used.
30
31 ----------------------------------------------------------------------------*/
32
33 #ifndef linux
34 extern  char    *sys_errlist[];
35 #endif
36 extern  int     optind;
37 extern  char    *optarg;
38
39
40 char    *Progname;
41
42
43 static void
44 usage(void)
45 {
46         fprintf(stderr, "usage:\t%s [-F] [-s sid] token {pathname|handle}\n",
47                 Progname);
48         exit(1);
49 }
50
51
52 int
53 main(
54         int     argc,
55         char    **argv)
56 {
57         dm_sessid_t     sid = DM_NO_SESSION;
58         dm_token_t      token;
59         char            *object;
60         void            *hanp;
61         size_t          hlen;
62         int             Fflag = 0;
63         char            *name;
64         int             opt;
65
66         Progname = strrchr(argv[0], '/');
67         if (Progname) {
68                 Progname++;
69         } else {
70                 Progname = argv[0];
71         }
72
73         /* Crack and validate the command line options. */
74
75         while ((opt = getopt(argc, argv, "Fs:")) != EOF) {
76                 switch (opt) {
77                 case 'F':
78                         Fflag++;
79                         break;
80                 case 's':
81                         sid = atol(optarg);
82                         break;
83                 case '?':
84                         usage();
85                 }
86         }
87         if (optind + 2 != argc)
88                 usage();
89         token = atol(argv[optind++]);
90         object = argv[optind];
91
92         if (dm_init_service(&name) == -1)  {
93                 fprintf(stderr, "Can't initialize the DMAPI\n");
94                 exit(1);
95         }
96         if (sid == DM_NO_SESSION)
97                 find_test_session(&sid);
98
99         /* Get the file or filesystem's handle. */
100
101         if (opaque_to_handle(object, &hanp, &hlen)) {
102                 fprintf(stderr, "can't get handle from %s\n", object);
103                 exit(1);
104         }
105         if (Fflag) {
106                 void    *fshanp;
107                 size_t  fshlen;
108
109                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
110                         fprintf(stderr, "can't get filesystem handle from %s\n",
111                                 object);
112                         exit(1);
113                 }
114                 dm_handle_free(hanp, hlen);
115                 hanp = fshanp;
116                 hlen = fshlen;
117         }
118
119         if (dm_downgrade_right(sid, hanp, hlen, token)) {
120                 fprintf(stderr, "dm_downgrade_right failed, %s\n",
121                         strerror(errno));
122                 return(1);
123         }
124
125         dm_handle_free(hanp, hlen);
126         exit(0);
127 }