XRootD
XrdPosixMap.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d P o s i x M a p . 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 <sys/stat.h>
33 
34 #include "XrdOuc/XrdOucECMsg.hh"
35 #include "XProtocol/XProtocol.hh"
36 #include "XrdPosix/XrdPosixMap.hh"
37 #include "XrdSfs/XrdSfsFlags.hh"
38 #include "XrdSys/XrdSysHeaders.hh"
39 
40 #ifndef EAUTH
41 #define EAUTH EBADE
42 #endif
43 
44 #ifndef ENOSR
45 #define ENOSR ENOSPC
46 #endif
47 
48 #ifndef ECHRNG
49 #define ECHRNG EINVAL
50 #endif
51 
52 /******************************************************************************/
53 /* S t a t i c M e m b e r s */
54 /******************************************************************************/
55 
56 bool XrdPosixMap::Debug = false;
57 
58 /******************************************************************************/
59 /* F l a g s 2 M o d e */
60 /******************************************************************************/
61 
62 mode_t XrdPosixMap::Flags2Mode(dev_t *rdv, uint32_t flags)
63 {
64  mode_t newflags = 0;
65 
66 // Map the xroot flags to unix flags
67 //
68  if (flags & XrdCl::StatInfo::XBitSet) newflags |= S_IXUSR;
69  if (flags & XrdCl::StatInfo::IsReadable) newflags |= S_IRUSR;
70  if (flags & XrdCl::StatInfo::IsWritable) newflags |= S_IWUSR;
71  if (flags & XrdCl::StatInfo::Other) newflags |= S_IFBLK;
72  else if (flags & XrdCl::StatInfo::IsDir) newflags |= S_IFDIR;
73  else newflags |= S_IFREG;
74  if (flags & XrdCl::StatInfo::POSCPending) newflags |= XRDSFS_POSCPEND;
75  if (rdv)
76  {*rdv = 0;
77  if (flags & XrdCl::StatInfo::Offline) *rdv |= XRDSFS_OFFLINE;
78  if (flags & XrdCl::StatInfo::BackUpExists) *rdv |= XRDSFS_HASBKUP;
79  }
80 
81  return newflags;
82 }
83 
84 /******************************************************************************/
85 /* S t a t F i l l */
86 /******************************************************************************/
87 
89 {
90  auto statInfo = dirEnt.GetStatInfo();
91  if (!statInfo) return EIO;
92 
93  memset(&buf, '\0', sizeof(buf));
94  buf.st_mode = XrdPosixMap::Flags2Mode(nullptr, statInfo->GetFlags());
95  // Since the UID/GID isn't known by the client, when these are translated by
96  // XrdXrootdProtocol::StatGen back to xrootd protocol flags, they will get zero'd
97  // out if only the user access mode is set (e.g., S_IRUSR). Therefor, upgrade the
98  // access mode in the mapping to "other" as well (e.g., S_ROTH). This way, the
99  // computed mode is the same for both the origin and the cache
100  if (buf.st_mode & S_IRUSR) buf.st_mode |= S_IROTH;
101  if (buf.st_mode & S_IWUSR) buf.st_mode |= S_IWOTH;
102  if (buf.st_mode & S_IXUSR) buf.st_mode |= S_IXOTH;
103  buf.st_mtime = static_cast<time_t>(statInfo->GetModTime());
104  buf.st_ctime = buf.st_mtime;
105  buf.st_size = static_cast<size_t>(statInfo->GetSize());
106  buf.st_ino = static_cast<ino_t>(strtoll(statInfo->GetId().c_str(), 0, 10));
107  buf.st_blocks = buf.st_size/512 + buf.st_size%512;
108  // If the device is zero'd out, then the listing later is translated to being offline
109  buf.st_dev = 1;
110 
111  if (statInfo->ExtendedFormat())
112  {buf.st_ctime = static_cast<time_t>(statInfo->GetChangeTime());
113  buf.st_atime = static_cast<time_t>(statInfo->GetAccessTime());
114  }
115 
116  return 0;
117 }
118 
119 /******************************************************************************/
120 /* Private: m a p C o d e */
121 /******************************************************************************/
122 
123 int XrdPosixMap::mapCode(int rc)
124 {
125  switch(rc)
126  {case XrdCl::errRetry: return EAGAIN; // Cl:001
127  case XrdCl::errInvalidOp: return EOPNOTSUPP; // Cl:003
128  case XrdCl::errConfig: return ENOEXEC; // Cl:006
129  case XrdCl::errInvalidArgs: return EINVAL; // Cl:009
130  case XrdCl::errInProgress: return EINPROGRESS; // Cl:010
131  case XrdCl::errNotSupported: return ENOTSUP; // Cl:013
132  case XrdCl::errDataError: return EDOM; // Cl:014
133  case XrdCl::errNotImplemented: return ENOSYS; // Cl:015
134  case XrdCl::errNoMoreReplicas: return ENOSR; // Cl:016
135  case XrdCl::errInvalidAddr: return EHOSTUNREACH; // Cl:101
136  case XrdCl::errSocketError: return ENOTSOCK; // Cl:102
137  case XrdCl::errSocketTimeout: return ETIMEDOUT; // Cl:103
138  case XrdCl::errSocketDisconnected: return ENOTCONN; // Cl:104
139  case XrdCl::errStreamDisconnect: return ECONNRESET; // Cl:107
140  case XrdCl::errConnectionError: return ECONNREFUSED; // Cl:108
141  case XrdCl::errInvalidSession: return ECHRNG; // Cl:109
142  case XrdCl::errTlsError: return ENETRESET; // Cl:110
143  case XrdCl::errInvalidMessage: return EPROTO; // Cl:201
144  case XrdCl::errHandShakeFailed: return EPROTO; // Cl:202
145  case XrdCl::errLoginFailed: return ECONNABORTED; // Cl:203
146  case XrdCl::errAuthFailed: return EAUTH; // Cl:204
147  case XrdCl::errQueryNotSupported: return ENOTSUP; // Cl:205
148  case XrdCl::errOperationExpired: return ESTALE; // Cl:206
149  case XrdCl::errOperationInterrupted: return EINTR; // Cl:207
150  case XrdCl::errNoMoreFreeSIDs: return ENOSR; // Cl:301
151  case XrdCl::errInvalidRedirectURL: return ESPIPE; // Cl:302
152  case XrdCl::errInvalidResponse: return EBADMSG; // Cl:303
153  case XrdCl::errNotFound: return EIDRM; // Cl:304
154  case XrdCl::errCheckSumError: return EILSEQ; // Cl:305
155  case XrdCl::errRedirectLimit: return ELOOP; // Cl:306
156  default: break;
157  }
158  return ENOMSG;
159 }
160 
161 /******************************************************************************/
162 /* M o d e 2 A c c e s s */
163 /******************************************************************************/
164 
167 
168 // Map the mode
169 //
170  if (mode & S_IRUSR) XMode |= XrdCl::Access::UR;
171  if (mode & S_IWUSR) XMode |= XrdCl::Access::UW;
172  if (mode & S_IXUSR) XMode |= XrdCl::Access::UX;
173  if (mode & S_IRGRP) XMode |= XrdCl::Access::GR;
174  if (mode & S_IWGRP) XMode |= XrdCl::Access::GW;
175  if (mode & S_IXGRP) XMode |= XrdCl::Access::GX;
176  if (mode & S_IROTH) XMode |= XrdCl::Access::OR;
177  if (mode & S_IXOTH) XMode |= XrdCl::Access::OX;
178  return XMode;
179 }
180 
181 /******************************************************************************/
182 /* R e s u l t */
183 /******************************************************************************/
184 
186  XrdOucECMsg& ecMsg, bool retneg1)
187 {
188  int eNum;
189 
190 // If all went well, return success
191 //
192  if (Status.IsOK()) return 0;
193 
194 // If this is an xrootd error then get the xrootd generated error
195 //
196  if (Status.code == XrdCl::errErrorResponse)
197  {ecMsg = Status.GetErrorMessage();
198  eNum = XProtocol::toErrno(Status.errNo);
199  } else {
200  ecMsg = Status.ToStr();
201  eNum = (Status.errNo ? Status.errNo : mapCode(Status.code));
202  }
203 
204 // Trace this if need be (we supress this for as we really need more info to
205 // make this messae useful like the opteration and path).
206 //
207 // if (eNum != ENOENT && !ecMsg.hasMsg() && Debug)
208 // std::cerr <<"XrdPosix: " <<eText <<std::endl;
209 
210 // Return
211 //
212  ecMsg = errno = eNum;
213  return (retneg1 ? -1 : -eNum);
214 }
#define EAUTH
Definition: XProtocol.hh:1351
#define ECHRNG
Definition: XrdPosixMap.cc:49
#define ENOSR
Definition: XrdPosixMap.cc:45
int stat(const char *path, struct stat *buf)
static const dev_t XRDSFS_HASBKUP
Definition: XrdSfsFlags.hh:102
#define XRDSFS_POSCPEND
Definition: XrdSfsFlags.hh:89
static const dev_t XRDSFS_OFFLINE
Definition: XrdSfsFlags.hh:100
static int toErrno(int xerr)
Definition: XProtocol.hh:1411
StatInfo * GetStatInfo()
Get the stat info object.
@ IsReadable
Read access is allowed.
@ IsDir
This is a directory.
@ Other
Neither a file nor a directory.
@ BackUpExists
Back up copy exists.
@ XBitSet
Executable/searchable bit set.
@ Offline
File is not online (ie. on disk)
@ IsWritable
Write access is allowed.
const std::string & GetErrorMessage() const
Get error message.
std::string ToStr() const
Convert to string.
static mode_t Flags2Mode(dev_t *rdv, uint32_t flags)
Definition: XrdPosixMap.cc:62
static int Entry2Buf(const XrdCl::DirectoryList::ListEntry &dirEnt, struct stat &buf)
Definition: XrdPosixMap.cc:88
static int Result(const XrdCl::XRootDStatus &Status, XrdOucECMsg &ecMsg, bool retneg1=false)
Definition: XrdPosixMap.cc:185
static XrdCl::Access::Mode Mode2Access(mode_t mode)
Definition: XrdPosixMap.cc:165
const uint16_t errQueryNotSupported
Definition: XrdClStatus.hh:89
const uint16_t errInvalidAddr
Definition: XrdClStatus.hh:71
const uint16_t errStreamDisconnect
Definition: XrdClStatus.hh:77
const uint16_t errRedirectLimit
Definition: XrdClStatus.hh:102
const uint16_t errErrorResponse
Definition: XrdClStatus.hh:105
const uint16_t errTlsError
Definition: XrdClStatus.hh:80
const uint16_t errOperationExpired
Definition: XrdClStatus.hh:90
const uint16_t errNotImplemented
Operation is not implemented.
Definition: XrdClStatus.hh:64
const uint16_t errLoginFailed
Definition: XrdClStatus.hh:87
const uint16_t errNoMoreFreeSIDs
Definition: XrdClStatus.hh:97
const uint16_t errInProgress
Definition: XrdClStatus.hh:59
const uint16_t errNotFound
Definition: XrdClStatus.hh:100
const uint16_t errSocketTimeout
Definition: XrdClStatus.hh:73
const uint16_t errDataError
data is corrupted
Definition: XrdClStatus.hh:63
const uint16_t errInvalidOp
Definition: XrdClStatus.hh:51
const uint16_t errHandShakeFailed
Definition: XrdClStatus.hh:86
const uint16_t errConfig
System misconfigured.
Definition: XrdClStatus.hh:55
const uint16_t errInvalidResponse
Definition: XrdClStatus.hh:99
const uint16_t errInvalidArgs
Definition: XrdClStatus.hh:58
const uint16_t errInvalidRedirectURL
Definition: XrdClStatus.hh:98
const uint16_t errConnectionError
Definition: XrdClStatus.hh:78
const uint16_t errNotSupported
Definition: XrdClStatus.hh:62
const uint16_t errSocketError
Definition: XrdClStatus.hh:72
const uint16_t errRetry
Try again for whatever reason.
Definition: XrdClStatus.hh:49
const uint16_t errCheckSumError
Definition: XrdClStatus.hh:101
const uint16_t errOperationInterrupted
Definition: XrdClStatus.hh:91
const uint16_t errNoMoreReplicas
No more replicas to try.
Definition: XrdClStatus.hh:65
const uint16_t errInvalidSession
Definition: XrdClStatus.hh:79
const uint16_t errSocketDisconnected
Definition: XrdClStatus.hh:74
const uint16_t errAuthFailed
Definition: XrdClStatus.hh:88
const uint16_t errInvalidMessage
Definition: XrdClStatus.hh:85
thread_local XrdOucECMsg ecMsg
Mode
Access mode.
@ OX
world executable/browsable
@ UR
owner readable
@ GR
group readable
@ UW
owner writable
@ GX
group executable/browsable
@ GW
group writable
@ UX
owner executable/browsable
@ OR
world readable
uint16_t code
Error type, or additional hints on what to do.
Definition: XrdClStatus.hh:147
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124
uint32_t errNo
Errno, if any.
Definition: XrdClStatus.hh:148