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  {num = XrdPosixMap::Result(xStatus, ecMsg, false);
65  return 0;
66  }
67 
68 // Allocate an array large enough to hold this information
69 //
70  if (!(i = info->GetSize())) {delete info; return 0;}
71  if (i > std::numeric_limits<ptrdiff_t>::max() / sizeof(XrdCl::URL))
72  {delete info; return 0;}
73  uVec = new XrdCl::URL[i];
74 
75 // Now start filling out the array
76 //
77  num = 0;
78  for( it = info->Begin(); it != info->End(); ++it )
79  {if (!netLoc.Set(it->GetAddress().c_str()) && (hName = netLoc.Name()))
80  {std::string hString(hName);
81  uVec[num] = Url;
82  uVec[num].SetHostName(hString);
83  uVec[num].SetPort(netLoc.Port());
84  num++;
85  }
86  }
87 
88 // Make sure we can return something;
89 //
90  delete info;
91  if (!num) {delete [] uVec; return 0;}
92  return uVec;
93 }
94 
95 /******************************************************************************/
96 /* Q u e r y */
97 /******************************************************************************/
98 
99 int XrdPosixAdmin::Query(XrdCl::QueryCode::Code reqCode, void *buff, int bsz)
100 {
101  XrdCl::Buffer reqBuff, *rspBuff = 0;
102 
103 // Make sure we are OK
104 //
105  if (!isOK()) return -1;
106 
107 // Get argument
108 //
109  reqBuff.FromString(Url.GetPathWithParams());
110 
111 // Issue the query
112 //
113  if (!XrdPosixMap::Result(Xrd.Query(reqCode, reqBuff, rspBuff),ecMsg))
114  {uint32_t rspSz = rspBuff->GetSize();
115  char *rspbuff = rspBuff->GetBuffer();
116  if (rspbuff && rspSz)
117  {// if the string is null-terminated decrement the size
118  if ( !(rspbuff[rspSz - 1]) ) --rspSz;
119  if (bsz >= (int)rspSz + 1)
120  {strncpy((char *)buff, rspbuff, rspSz);
121  ((char*)buff)[rspSz] = 0; // make sure it is null-terminated
122  delete rspBuff;
123  return static_cast<int>(rspSz + 1);
124  } else ecMsg.SetErrno(ERANGE,0,"buffer to small to hold result");
125  } else ecMsg.SetErrno(EFAULT,0,"Invalid return results");
126  }
127 
128 // Return error
129 //
130  delete rspBuff;
131  return -1;
132 }
133 
134 /******************************************************************************/
135 /* S t a t */
136 /******************************************************************************/
137 
138 bool XrdPosixAdmin::Stat(mode_t *flags, time_t *mtime)
139 {
140  XrdCl::XRootDStatus xStatus;
141  XrdCl::StatInfo *sInfo = 0;
142 
143 // Make sure admin is ok
144 //
145  if (!isOK()) return false;
146 
147 // Issue the stat and verify that all went well
148 //
149  xStatus = Xrd.Stat(Url.GetPathWithParams(), sInfo);
150  if (!xStatus.IsOK())
151  {XrdPosixMap::Result(xStatus,ecMsg);
152  delete sInfo;
153  return false;
154  }
155 
156 // Return wanted data
157 //
158  if (flags) *flags = XrdPosixMap::Flags2Mode(0, sInfo->GetFlags());
159  if (mtime) *mtime = static_cast<time_t>(sInfo->GetModTime());
160 
161 // Delete our status information and return final result
162 //
163  delete sInfo;
164  return true;
165 }
166 
167 /******************************************************************************/
168 
170 {
171  XrdCl::XRootDStatus xStatus;
172  XrdCl::StatInfo *sInfo = 0;
173 
174 // Make sure admin is ok
175 //
176  if (!isOK()) return false;
177 
178 // Issue the stat and verify that all went well
179 //
180  xStatus = Xrd.Stat(Url.GetPathWithParams(), sInfo);
181  if (!xStatus.IsOK())
182  {XrdPosixMap::Result(xStatus,ecMsg);
183  delete sInfo;
184  return false;
185  }
186 
187 // Return the data
188 //
189  Stat.st_size = static_cast<size_t>(sInfo->GetSize());
190  Stat.st_blocks = Stat.st_size/512 + Stat.st_size%512;
191  Stat.st_ino = static_cast<ino_t>(strtoll(sInfo->GetId().c_str(), 0, 10));
192 #if defined(__mips__) && _MIPS_SIM == _ABIO32
193  // Work around inconsistent type definitions on MIPS
194  // The st_rdev field in struct stat (which is 32 bits) is not type
195  // dev_t (which is 64 bits)
196  dev_t tmp_rdev = Stat.st_rdev;
197  Stat.st_mode = XrdPosixMap::Flags2Mode(&tmp_rdev, sInfo->GetFlags());
198  Stat.st_rdev = tmp_rdev;
199 #else
200  Stat.st_mode = XrdPosixMap::Flags2Mode(&Stat.st_rdev, sInfo->GetFlags());
201 #endif
202  Stat.st_mtime = static_cast<time_t>(sInfo->GetModTime());
203 
204  if (sInfo->ExtendedFormat())
205  {Stat.st_ctime = static_cast<time_t>(sInfo->GetChangeTime());
206  Stat.st_atime = static_cast<time_t>(sInfo->GetAccessTime());
207  } else {
208  Stat.st_ctime = Stat.st_mtime;
209  Stat.st_atime = time(0);
210  }
211 
212 // Delete our status information and return final result
213 //
214  delete sInfo;
215  return true;
216 }
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:311
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)
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:150
Code
XRootD query request codes.
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124