cmd: add flags argument to create syntax function
[openafs.git] / src / venus / whatfid.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* file         whatfid.c */
11
12
13 #include <afsconfig.h>
14 #include <afs/param.h>
15 #include <afs/stds.h>
16
17 #include <roken.h>
18
19 #include <afs/com_err.h>
20 #include <afs/vice.h>
21 #include <afs/venus.h>
22 #include "afs/prs_fs.h"
23 #include <afs/afsint.h>
24 #include <afs/cellconfig.h>
25 #include <afs/cmd.h>
26
27
28 struct VenusFid {
29     afs_int32 Cell;
30     struct AFSFid Fid;
31 };
32
33
34 char *pn;
35 void PioctlError();
36
37 #include "AFS_component_version_number.c"
38
39 int WhatFidCmd_FileParm;
40 int WhatFidCmd_FollowLinkParm;
41 int
42 WhatFidCmd(struct cmd_syndesc *as, void *arock)
43 {
44     afs_int32 code;
45     struct ViceIoctl blob;
46     struct VenusFid vFid;
47     struct cmd_item *ti;
48     struct VolumeStatus *status;
49     char *name;
50     int follow = 1;
51
52     if (as->parms[1].items)
53         follow = 0;
54     for (ti = as->parms[0].items; ti; ti = ti->next) {
55         /* once per file */
56         blob.out_size = sizeof(struct VenusFid);
57         blob.in_size = 0;
58         blob.out = (char *)&vFid;
59         code = pioctl(ti->data, VIOCGETFID, &blob, follow);
60         if (code) {
61             PioctlError(code, ti->data);
62             continue;
63         }
64         printf("%s: %x:%d.%d.%d\n", ti->data, vFid.Cell, vFid.Fid.Volume,
65                vFid.Fid.Vnode, vFid.Fid.Unique);
66     }
67     return 0;
68 }
69
70
71
72 main(argc, argv)
73      int argc;
74      char **argv;
75 {
76     afs_int32 code;
77     struct cmd_syndesc *ts;
78
79 #ifdef  AFS_AIX32_ENV
80     /*
81      * The following signal action for AIX is necessary so that in case of a
82      * crash (i.e. core is generated) we can include the user's data section
83      * in the core dump. Unfortunately, by default, only a partial core is
84      * generated which, in many cases, isn't too useful.
85      */
86     struct sigaction nsa;
87
88     sigemptyset(&nsa.sa_mask);
89     nsa.sa_handler = SIG_DFL;
90     nsa.sa_flags = SA_FULLDUMP;
91     sigaction(SIGSEGV, &nsa, NULL);
92 #endif
93
94     pn = argv[0];
95
96     ts = cmd_CreateSyntax("initcmd", WhatFidCmd, NULL, 0, "list fid for file(s)");
97     WhatFidCmd_FileParm = cmd_AddParm(ts, "-path", CMD_LIST, 0, "pathnames");
98     WhatFidCmd_FollowLinkParm =
99         cmd_AddParm(ts, "-link", CMD_FLAG, CMD_OPTIONAL,
100                     "do not follow symlinks");
101
102     exit(cmd_Dispatch(argc, argv));
103 }
104
105 void
106 PioctlError(code, filename)
107      int code;
108      char *filename;
109 {                               /*Die */
110
111     if (errno == EINVAL) {
112         if (filename)
113             fprintf(stderr,
114                     "%s: Invalid argument; it is possible that %s is not in AFS.\n",
115                     pn, filename);
116         else
117             fprintf(stderr, "%s: Invalid argument.\n", pn);
118     } else if (errno == ENOENT) {
119         if (filename)
120             fprintf(stderr, "%s: File '%s' doesn't exist\n", pn, filename);
121         else
122             fprintf(stderr, "%s: no such file returned\n", pn);
123     } else if (errno == EROFS)
124         fprintf(stderr,
125                 "%s: You can not change a backup or readonly volume\n", pn);
126     else if (errno == EACCES || errno == EPERM) {
127         if (filename)
128             fprintf(stderr,
129                     "%s: You don't have the required access rights on '%s'\n",
130                     pn, filename);
131         else
132             fprintf(stderr,
133                     "%s: You do not have the required rights to do this operation\n",
134                     pn);
135     } else {
136         if (filename)
137             fprintf(stderr, "%s:'%s'", pn, filename);
138         else
139             fprintf(stderr, "%s", pn);
140         fprintf(stderr, ": %s\n", afs_error_message(errno));
141     }
142 }                               /*Die */