XRootD
XrdPosixAdmin.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d P o s i x A d m i n . c c */
4 /* */
5 /* (c) 2013 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 <cerrno>
32 #include <limits>
33 #include <cstddef>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 
38 #include "XrdNet/XrdNetAddr.hh"
40 #include "XrdPosix/XrdPosixMap.hh"
41 
42 /******************************************************************************/
43 /* F a n O u t */
44 /******************************************************************************/
45 
47 {
48  XrdCl::XRootDStatus xStatus;
49  XrdCl::LocationInfo *info = 0;
51  XrdCl::URL *uVec;
52  XrdNetAddr netLoc;
53  const char *hName;
54  unsigned long i;
55 
56 // Make sure admin is ok
57 //
58  if (!isOK()) return 0;
59 
60 // Issue the deep locate and verify that all went well
61 //
62  xStatus = Xrd.DeepLocate(Url.GetPathWithParams(),XrdCl::OpenFlags::None,info);
63  if (!xStatus.IsOK())
64  {if (ecMutex) ecMutex->lock();
65  num = XrdPosixMap::Result(xStatus, ecMsg, false);
66  if (ecMutex) ecMutex->unlock();
67  return 0;
68  }
69 
70 // Allocate an array large enough to hold this information
71 //
72  if (!(i = info->GetSize())) {delete info; return 0;}
73  if (i > std::numeric_limits<ptrdiff_t>::max() / sizeof(XrdCl::URL))
74  {delete info; return 0;}
75  uVec = new XrdCl::URL[i];
76 
77 // Now start filling out the array
78 //
79  num = 0;
80  for( it = info->Begin(); it != info->End(); ++it )
81  {if (!netLoc.Set(it->GetAddress().c_str()) && (hName = netLoc.Name()))
82  {std::string hString(hName);
83  uVec[num] = Url;
84  uVec[num].SetHostName(hString);
85  uVec[num].SetPort(netLoc.Port());
86  num++;
87  }
88  }
89 
90 // Make sure we can return something;
91 //
92  delete info;
93  if (!num) {delete [] uVec; return 0;}
94  return uVec;
95 }
96 
97 /******************************************************************************/
98 /* Q u e r y */
99 /******************************************************************************/
100 
101 int XrdPosixAdmin::Query(XrdCl::QueryCode::Code reqCode, void *buff, int bsz)
102 {
103  XrdCl::Buffer reqBuff, *rspBuff = 0;
104 
105 // Make sure we are OK
106 //
107  if (!isOK()) return -1;
108 
109 // Get argument
110 //
111  reqBuff.FromString(Url.GetPathWithParams());
112 
113 // Issue the query
114 //
115  auto st = Xrd.Query(reqCode, reqBuff, rspBuff);
116  int rc;
117  {
118  if (ecMutex) ecMutex->lock();
119  rc = XrdPosixMap::Result(st, ecMsg);
120  if (ecMutex) ecMutex->unlock();
121  }
122  if (!rc)
123  {uint32_t rspSz = rspBuff->GetSize();
124  char *rspbuff = rspBuff->GetBuffer();
125  if (rspbuff && rspSz)
126  {// if the string is null-terminated decrement the size
127  if ( !(rspbuff[rspSz - 1]) ) --rspSz;
128  if (bsz >= (int)rspSz + 1)
129  {strncpy((char *)buff, rspbuff, rspSz);
130  ((char*)buff)[rspSz] = 0; // make sure it is null-terminated
131  delete rspBuff;
132  return static_cast<int>(rspSz + 1);
133  } else
134  {if (ecMutex) ecMutex->lock();
135  ecMsg.SetErrno(ERANGE,0,"buffer to small to hold result");
136  if (ecMutex) ecMutex->unlock();
137  }
138  } else
139  {if (ecMutex) ecMutex->lock();
140  ecMsg.SetErrno(EFAULT,0,"Invalid return results");
141  if (ecMutex) ecMutex->unlock();
142  }
143  }
144 
145 // Return error
146 //
147  delete rspBuff;
148  return -1;
149 }
150 
151 /******************************************************************************/
152 /* S t a t */
153 /******************************************************************************/
154 
155 bool XrdPosixAdmin::Stat(mode_t *flags, time_t *mtime)
156 {
157  XrdCl::XRootDStatus xStatus;
158  XrdCl::StatInfo *sInfo = 0;
159 
160 // Make sure admin is ok
161 //
162  if (!isOK()) return false;
163 
164 // Issue the stat and verify that all went well
165 //
166  xStatus = Xrd.Stat(Url.GetPathWithParams(), sInfo);
167  if (!xStatus.IsOK())
168  {if (ecMutex) ecMutex->lock();
169  XrdPosixMap::Result(xStatus,ecMsg);
170  if (ecMutex) ecMutex->unlock();
171  delete sInfo;
172  return false;
173  }
174 
175 // Return wanted data
176 //
177  if (flags) *flags = XrdPosixMap::Flags2Mode(0, sInfo->GetFlags());
178  if (mtime) *mtime = static_cast<time_t>(sInfo->GetModTime());
179 
180 // Delete our status information and return final result
181 //
182  delete sInfo;
183  return true;
184 }
185 
186 /******************************************************************************/
187 
189 {
190  XrdCl::XRootDStatus xStatus;
191  XrdCl::StatInfo *sInfo = 0;
192 
193 // Make sure admin is ok
194 //
195  if (!isOK()) return false;
196 
197 // Issue the stat and verify that all went well
198 //
199  xStatus = Xrd.Stat(Url.GetPathWithParams(), sInfo);
200  if (!xStatus.IsOK())
201  {if (ecMutex) ecMutex->lock();
202  XrdPosixMap::Result(xStatus,ecMsg);
203  if (ecMutex) ecMutex->unlock();
204  delete sInfo;
205  return false;
206  }
207 
208 // Return the data
209 //
210  Stat.st_size = static_cast<size_t>(sInfo->GetSize());
211  Stat.st_blocks = Stat.st_size/512 + Stat.st_size%512;
212  Stat.st_ino = static_cast<ino_t>(strtoll(sInfo->GetId().c_str(), 0, 10));
213 #if defined(__mips__) && _MIPS_SIM == _ABIO32
214  // Work around inconsistent type definitions on MIPS
215  // The st_rdev field in struct stat (which is 32 bits) is not type
216  // dev_t (which is 64 bits)
217  dev_t tmp_rdev = Stat.st_rdev;
218  Stat.st_mode = XrdPosixMap::Flags2Mode(&tmp_rdev, sInfo->GetFlags());
219  Stat.st_rdev = tmp_rdev;
220 #else
221  Stat.st_mode = XrdPosixMap::Flags2Mode(&Stat.st_rdev, sInfo->GetFlags());
222 #endif
223  Stat.st_mtime = static_cast<time_t>(sInfo->GetModTime());
224 
225  if (sInfo->ExtendedFormat())
226  {Stat.st_ctime = static_cast<time_t>(sInfo->GetChangeTime());
227  Stat.st_atime = static_cast<time_t>(sInfo->GetAccessTime());
228  } else {
229  Stat.st_ctime = Stat.st_mtime;
230  Stat.st_atime = time(0);
231  }
232 
233 // Delete our status information and return final result
234 //
235  delete sInfo;
236  return true;
237 }
struct stat Stat
Definition: XrdCks.cc:49
int stat(const char *path, struct stat *buf)
Binary blob representation.
Definition: XrdClBuffer.hh:34
void FromString(const std::string str)
Fill the buffer from a string.
Definition: XrdClBuffer.hh:205
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
uint32_t GetSize() const
Get the size of the message.
Definition: XrdClBuffer.hh:132
Path location info.
uint32_t GetSize() const
Get number of locations.
Iterator Begin()
Get the location begin iterator.
LocationList::iterator Iterator
Iterator over locations.
Iterator End()
Get the location end iterator.
Object stat info.
uint64_t GetChangeTime() const
Get change time (in seconds since epoch)
uint64_t GetSize() const
Get size (in bytes)
uint32_t GetFlags() const
Get flags.
bool ExtendedFormat() const
Has extended stat information.
const std::string & GetId() const
Get id.
uint64_t GetModTime() const
Get modification time (in seconds since epoch)
uint64_t GetAccessTime() const
Get change time (in seconds since epoch)
URL representation.
Definition: XrdClURL.hh:31
void SetPort(int port)
Definition: XrdClURL.hh:196
std::string GetPathWithParams() const
Get the path with params.
Definition: XrdClURL.cc:318
void SetHostName(const std::string &hostName)
Set the host name.
Definition: XrdClURL.hh:178
const char * Name(const char *eName=0, const char **eText=0)
int Port(int pNum=-1)
Definition: XrdNetAddr.cc:156
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:216
int SetErrno(int ecc, int retval=-1, const char *alt=0)
Definition: XrdOucECMsg.cc:144
bool Stat(mode_t *flags=0, time_t *mtime=0)
std::mutex * ecMutex
XrdCl::URL * FanOut(int &num)
XrdOucECMsg & ecMsg
int Query(XrdCl::QueryCode::Code reqCode, void *buff, int bsz)
XrdCl::URL Url
static mode_t Flags2Mode(dev_t *rdv, uint32_t flags)
Definition: XrdPosixMap.cc:62
static int Result(const XrdCl::XRootDStatus &Status, XrdOucECMsg &ecMsg, bool retneg1=false)
Definition: XrdPosixMap.cc:188
Code
XRootD query request codes.
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124