XRootD
XrdCks.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d C k s . c c */
4 /* */
5 /* (c) 2022 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <cstdio>
32 #include <cstdlib>
33 #include <cstring>
34 #include <iostream>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <cerrno>
40 
41 #include "XrdCks/XrdCksXAttr.hh"
42 #include "XrdOuc/XrdOucXAttr.hh"
43 
44 /******************************************************************************/
45 /* G l o b a l D a t a */
46 /******************************************************************************/
47 
49  struct stat Stat;
50  const char *csCmd;
51 
52 /******************************************************************************/
53 /* D i s p l a y */
54 /******************************************************************************/
55 
56 void Display()
57 {
58  const char *stale;
59  char csBuff[512];
60 
61 // Check if the checksum is stale
62 //
63  if (xCS.Attr.Cks.fmTime != Stat.st_mtime) stale = " stale";
64  else stale = "";
65 
66 // Get displayable checksum
67 //
68  xCS.Attr.Cks.Get(csBuff, sizeof(csBuff));
69 
70 // Display the information
71 //
72  std::cout <<xCS.Attr.Cks.Name<<' '<<csBuff<<stale<<std::endl;
73 }
74 
75 /******************************************************************************/
76 /* U n a b l e */
77 /******************************************************************************/
78 
79 void Unable(int rc)
80 {
81  char eBuff[256];
82  if (rc < 0) rc = -rc;
83  snprintf(eBuff, sizeof(eBuff), "%s", strerror(rc));
84  *eBuff = tolower(*eBuff);
85  std::cerr<<"xrdcks: Unable to "<<csCmd<<" checksum; "<<eBuff<<std::endl;
86  exit(5);
87 }
88 
89 /******************************************************************************/
90 /* U s a g e */
91 /******************************************************************************/
92 
93 void Usage(int rc)
94 {
95  std::cerr <<"Usage: xrdcks <path> <cksname> [<cksval>|delete]\n"
96  " xrdcks -h\n\n"
97  "Where: <path> - path to the target file\n"
98  " <cksname> - checksum name (e.g. adler32)\n"
99  " <cksval> - ASCII hex of value (even number of digits)"
100  <<std::endl;
101  exit(rc);
102 }
103 
104 /******************************************************************************/
105 /* m a i n */
106 /******************************************************************************/
107 
108 int main(int argc, char *argv[])
109 {
110  char *csName, *csPath, *csVal;
111  int rc;
112 
113 // Make sure the right number of arguments are here
114 //
115  if (argc <= 2)
116  {if (argc > 1 && !strcmp(argv[1], "-h")) Usage(0);
117  Usage(1);
118  }
119 
120 // Verify the name
121 //
122  csName = argv[2];
123  if (!xCS.Attr.Cks.Set(csName))
124  {std::cerr <<"xrdsetcks: checksum name '"<<csName<<"' is invalid"<<std::endl;
125  exit(3);
126  }
127 
128 // Determine what we should be doing
129 //
130  if (argc < 3) csCmd = "query";
131  else {csVal = argv[3];
132  if (!strcmp("delete", csVal)) csCmd = "delete";
133  else {csCmd = "set";
134  if (strncmp("0x", csVal, 2)) csVal += 2;
135  if (!xCS.Attr.Cks.Set(csVal, strlen(csVal)))
136  {std::cerr <<"xrdcks: checksum value is invalid"
137  <<std::endl;
138  exit(3);
139  }
140  }
141  }
142 
143 // Verify the path
144 //
145  csPath = argv[1];
146  if (stat(csPath, &Stat)) Unable(errno);
147 
148 // Handle query request
149 //
150  if (*csCmd == 'q')
151  {if ((rc = xCS.Get(csPath))) Unable(rc);
152  if (strcmp(xCS.Attr.Cks.Name, csName)) Unable(EILSEQ);
153  Display();
154  exit(0);
155  }
156 
157 // Handle delete
158 //
159  if (*csCmd == 'd')
160  {if ((rc = xCS.Del(csPath))) Unable(rc);
161  exit(0);
162  }
163 
164 // Handle the set
165 //
166  xCS.Attr.Cks.fmTime = static_cast<long long>(Stat.st_mtime);
167  xCS.Attr.Cks.csTime = 0;
168  if ((rc = xCS.Set(csPath))) Unable(rc);
169  exit(0);
170 }
int main(int argc, char *argv[])
Definition: XrdCks.cc:108
void Unable(int rc)
Definition: XrdCks.cc:79
const char * csCmd
Definition: XrdCks.cc:50
void Display()
Definition: XrdCks.cc:56
struct stat Stat
Definition: XrdCks.cc:49
void Usage(int rc)
Definition: XrdCks.cc:93
XrdOucXAttr< XrdCksXAttr > xCS
Definition: XrdCks.cc:48
int stat(const char *path, struct stat *buf)