Loading...
Searching...
No Matches
XrdPosixAdmin.cc
Go to the documentation of this file.
/******************************************************************************/
/* */
/* X r d P o s i x A d m i n . c c */
/* */
/* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <cerrno>
#include <limits>
#include <cstddef>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "XrdNet/XrdNetAddr.hh"
#include "XrdPosix/XrdPosixAdmin.hh"
#include "XrdPosix/XrdPosixMap.hh"
/******************************************************************************/
/* F a n O u t */
/******************************************************************************/
XrdCl::URL *XrdPosixAdmin::FanOut(int &num)
{
XrdCl::XRootDStatus xStatus;
XrdCl::LocationInfo *info = 0;
XrdCl::LocationInfo::Iterator it;
XrdCl::URL *uVec;
XrdNetAddr netLoc;
const char *hName;
unsigned long i;
// Make sure admin is ok
//
if (!isOK()) return 0;
// Issue the deep locate and verify that all went well
//
xStatus = Xrd.DeepLocate(Url.GetPathWithParams(),XrdCl::OpenFlags::PrefName,info);
if (!xStatus.IsOK())
{num = XrdPosixMap::Result(xStatus, ecMsg, false);
return 0;
}
// Allocate an array large enough to hold this information
//
if (!(i = info->GetSize())) {delete info; return 0;}
if (i > std::numeric_limits<ptrdiff_t>::max() / sizeof(XrdCl::URL))
{delete info; return 0;}
uVec = new XrdCl::URL[i];
// Now start filling out the array
//
num = 0;
for( it = info->Begin(); it != info->End(); ++it )
{if (!netLoc.Set(it->GetAddress().c_str()) && (hName = netLoc.Name()))
{std::string hString(hName);
uVec[num] = Url;
uVec[num].SetHostName(hString);
uVec[num].SetPort(netLoc.Port());
num++;
}
}
// Make sure we can return something;
//
delete info;
if (!num) {delete [] uVec; return 0;}
return uVec;
}
/******************************************************************************/
/* Q u e r y */
/******************************************************************************/
int XrdPosixAdmin::Query(XrdCl::QueryCode::Code reqCode, void *buff, int bsz)
{
XrdCl::Buffer reqBuff, *rspBuff = 0;
// Make sure we are OK
//
if (!isOK()) return -1;
// Get argument
//
reqBuff.FromString(Url.GetPathWithParams());
// Issue the query
//
if (!XrdPosixMap::Result(Xrd.Query(reqCode, reqBuff, rspBuff),ecMsg))
{uint32_t rspSz = rspBuff->GetSize();
char *rspbuff = rspBuff->GetBuffer();
if (rspbuff && rspSz)
{// if the string is null-terminated decrement the size
if ( !(rspbuff[rspSz - 1]) ) --rspSz;
if (bsz >= (int)rspSz + 1)
{strncpy((char *)buff, rspbuff, rspSz);
((char*)buff)[rspSz] = 0; // make sure it is null-terminated
delete rspBuff;
return static_cast<int>(rspSz + 1);
} else ecMsg.SetErrno(ERANGE,0,"buffer to small to hold result");
} else ecMsg.SetErrno(EFAULT,0,"Invalid return results");
}
// Return error
//
delete rspBuff;
return -1;
}
/******************************************************************************/
/* S t a t */
/******************************************************************************/
bool XrdPosixAdmin::Stat(mode_t *flags, time_t *mtime)
{
XrdCl::XRootDStatus xStatus;
XrdCl::StatInfo *sInfo = 0;
// Make sure admin is ok
//
if (!isOK()) return false;
// Issue the stat and verify that all went well
//
xStatus = Xrd.Stat(Url.GetPathWithParams(), sInfo);
if (!xStatus.IsOK())
{XrdPosixMap::Result(xStatus,ecMsg);
delete sInfo;
return false;
}
// Return wanted data
//
if (flags) *flags = XrdPosixMap::Flags2Mode(0, sInfo->GetFlags());
if (mtime) *mtime = static_cast<time_t>(sInfo->GetModTime());
// Delete our status information and return final result
//
delete sInfo;
return true;
}
/******************************************************************************/
bool XrdPosixAdmin::Stat(struct stat &Stat)
{
XrdCl::XRootDStatus xStatus;
XrdCl::StatInfo *sInfo = 0;
// Make sure admin is ok
//
if (!isOK()) return false;
// Issue the stat and verify that all went well
//
xStatus = Xrd.Stat(Url.GetPathWithParams(), sInfo);
if (!xStatus.IsOK())
{XrdPosixMap::Result(xStatus,ecMsg);
delete sInfo;
return false;
}
// Return the data
//
Stat.st_size = static_cast<size_t>(sInfo->GetSize());
Stat.st_blocks = Stat.st_size/512 + Stat.st_size%512;
Stat.st_ino = static_cast<ino_t>(strtoll(sInfo->GetId().c_str(), 0, 10));
#if defined(__mips__) && _MIPS_SIM == _ABIO32
// Work around inconsistent type definitions on MIPS
// The st_rdev field in struct stat (which is 32 bits) is not type
// dev_t (which is 64 bits)
dev_t tmp_rdev = Stat.st_rdev;
Stat.st_mode = XrdPosixMap::Flags2Mode(&tmp_rdev, sInfo->GetFlags());
Stat.st_rdev = tmp_rdev;
#else
Stat.st_mode = XrdPosixMap::Flags2Mode(&Stat.st_rdev, sInfo->GetFlags());
#endif
Stat.st_mtime = static_cast<time_t>(sInfo->GetModTime());
if (sInfo->ExtendedFormat())
{Stat.st_ctime = static_cast<time_t>(sInfo->GetChangeTime());
Stat.st_atime = static_cast<time_t>(sInfo->GetAccessTime());
} else {
Stat.st_ctime = Stat.st_mtime;
Stat.st_atime = time(0);
}
// Delete our status information and return final result
//
delete sInfo;
return true;
}
XrdNetAddr.hh
XrdPosixAdmin.hh
XrdPosixMap.hh
stat
#define stat(a, b)
Definition XrdPosix.hh:101
XrdCl::Buffer
Binary blob representation.
Definition XrdClBuffer.hh:34
XrdCl::Buffer::FromString
void FromString(const std::string str)
Fill the buffer from a string.
Definition XrdClBuffer.hh:205
XrdCl::Buffer::GetBuffer
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition XrdClBuffer.hh:72
XrdCl::Buffer::GetSize
uint32_t GetSize() const
Get the size of the message.
Definition XrdClBuffer.hh:132
XrdCl::LocationInfo
Path location info.
Definition XrdClXRootDResponses.hh:44
XrdCl::LocationInfo::GetSize
uint32_t GetSize() const
Get number of locations.
Definition XrdClXRootDResponses.hh:152
XrdCl::LocationInfo::Begin
Iterator Begin()
Get the location begin iterator.
Definition XrdClXRootDResponses.hh:168
XrdCl::LocationInfo::Iterator
LocationList::iterator Iterator
Iterator over locations.
Definition XrdClXRootDResponses.hh:137
XrdCl::LocationInfo::End
Iterator End()
Get the location end iterator.
Definition XrdClXRootDResponses.hh:184
XrdCl::StatInfo
Object stat info.
Definition XrdClXRootDResponses.hh:401
XrdCl::StatInfo::GetChangeTime
uint64_t GetChangeTime() const
Get change time (in seconds since epoch).
Definition XrdClXRootDResponses.cc:330
XrdCl::StatInfo::GetSize
uint64_t GetSize() const
Get size (in bytes).
Definition XrdClXRootDResponses.cc:274
XrdCl::StatInfo::GetFlags
uint32_t GetFlags() const
Get flags.
Definition XrdClXRootDResponses.cc:290
XrdCl::StatInfo::ExtendedFormat
bool ExtendedFormat() const
Has extended stat information.
Definition XrdClXRootDResponses.cc:423
XrdCl::StatInfo::GetId
const std::string & GetId() const
Get id.
Definition XrdClXRootDResponses.cc:266
XrdCl::StatInfo::GetModTime
uint64_t GetModTime() const
Get modification time (in seconds since epoch).
Definition XrdClXRootDResponses.cc:314
XrdCl::StatInfo::GetAccessTime
uint64_t GetAccessTime() const
Get change time (in seconds since epoch).
Definition XrdClXRootDResponses.cc:346
XrdCl::URL
URL representation.
Definition XrdClURL.hh:31
XrdCl::URL::SetPort
void SetPort(int port)
Definition XrdClURL.hh:196
XrdCl::URL::SetHostName
void SetHostName(const std::string &hostName)
Set the host name.
Definition XrdClURL.hh:178
XrdCl::XRootDStatus
Request status.
Definition XrdClXRootDResponses.hh:219
XrdNetAddrInfo::Name
const char * Name(const char *eName=0, const char **eText=0)
Definition XrdNetAddrInfo.cc:309
XrdNetAddr
Definition XrdNetAddr.hh:42
XrdNetAddr::Port
int Port(int pNum=-1)
Definition XrdNetAddr.cc:156
XrdNetAddr::Set
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition XrdNetAddr.cc:216
XrdPosixAdmin::Stat
bool Stat(mode_t *flags=0, time_t *mtime=0)
Definition XrdPosixAdmin.cc:138
XrdPosixAdmin::FanOut
XrdCl::URL * FanOut(int &num)
Definition XrdPosixAdmin.cc:46
XrdPosixAdmin::isOK
bool isOK()
Definition XrdPosixAdmin.hh:55
XrdPosixAdmin::ecMsg
XrdOucECMsg & ecMsg
Definition XrdPosixAdmin.hh:53
XrdPosixAdmin::Query
int Query(XrdCl::QueryCode::Code reqCode, void *buff, int bsz)
Definition XrdPosixAdmin.cc:99
XrdPosixAdmin::Url
XrdCl::URL Url
Definition XrdPosixAdmin.hh:51
XrdPosixMap::Flags2Mode
static mode_t Flags2Mode(dev_t *rdv, uint32_t flags)
Definition XrdPosixMap.cc:62
XrdPosixMap::Result
static int Result(const XrdCl::XRootDStatus &Status, XrdOucECMsg &ecMsg, bool retneg1=false)
Definition XrdPosixMap.cc:188
Xrd
Definition XrdSysTrace.hh:41
XrdCl::OpenFlags::PrefName
@ PrefName
Definition XrdClFileSystem.hh:105
XrdCl::QueryCode::Code
Code
XRootD query request codes.
Definition XrdClFileSystem.hh:53
XrdCl::Status::IsOK
bool IsOK() const
We're fine.
Definition XrdClStatus.hh:124