Loading...
Searching...
No Matches
XrdSysFAttrSun.icc
Go to the documentation of this file.
/******************************************************************************/
/* */
/* X r d S y s F A t t r S u n . i c c */
/* */
/* (c) 2010 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 <dirent.h>
#include <cerrno>
#include <fcntl.h>
#include <cstring>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "XrdSys/XrdSysFAttr.hh"
/******************************************************************************/
/* P l a t f o r m D e p e n d e n c i e s */
/******************************************************************************/
#ifndef ENOATTR
#define ENOATTR ENODATA
#endif
/******************************************************************************/
/* X r d S y s F A t t r : : D e l */
/******************************************************************************/
int XrdSysFAttr::Del(const char *Aname, const char *Path, int fd)
{
int ec = 0;
// Open the directory associated with the path or file descriptor
//
fd = (fd < 0 ? attropen(Path, ".", O_RDONLY)
: openat( fd, ".", O_RDONLY|O_XATTR));
// If the preceding open failed, diagnose the problem.
//
if (fd < 0) return Diagnose("open to remove", Aname, Path, errno);
// Now unlink the attribute file
//
if (unlinkat(fd, Aname, 0) < 0) ec = Diagnose("remove", Aname, Path, errno);
// All done
//
close(fd);
return ec;
}
/******************************************************************************/
/* X r d S y s F A t t r : : G e t */
/******************************************************************************/
int XrdSysFAttr::Get(const char *Aname, void *Aval, int Avsz,
const char *Path, int fd)
{
int ec = 0;
// Open the directory associated with the path or file descriptor
//
fd = (fd < 0 ? attropen(Path, Aname, O_RDONLY)
: openat( fd, Aname, O_RDONLY|O_XATTR));
// If the preceding open failed, diagnose the problem.
//
if (fd < 0) return Diagnose("open to get", Aname, Path, errno);
}
// If a size was passed, then read the variable data. Otherwise, the caller
// wants to know the size of the attribute data. So, return that.
//
if (Avsz) {if ((ec = read(fd, Aval, Avsz)) < 0) ec = -errno;}
else {struct stat Stat;
ec = (fstat(fd, &Stat) ? -errno : Stat.st_size);
}
// Close the underlying directory and diagnose any errors and return result.
//
close(fd);
if (ec < 0) ec = Diagnose("get", Aname, Path, -ec);
return ec;
}
/******************************************************************************/
/* X r d S y s F A t t r : : L i s t */
/******************************************************************************/
#if SOLARIS_VERSION < 11
#if !defined(__XOPEN_OR_POSIX)
#define dirfd(x) x->dd_fd
#else
#define dirfd(x) x->d_fd
#endif
#endif
int XrdSysFAttr::List(AList **aPL, const char *Path, int fd, int getSz)
{
AList *aNew;
DIR *dP;
struct dirent *dEnt;
struct stat Stat;
int rc, maxSz = 0;
// Open the directory associated with the path or file descriptor
//
*aPL = 0;
fd = (fd < 0 ? attropen(Path, ".", O_RDONLY)
: openat( fd, ".", O_RDONLY|O_XATTR));
// If the preceding open failed, diagnose the problem. We ignore ENOATTR.
//
if (fd < 0)
{if (errno == ENOATTR) return 0;
return Diagnose("open to list", "*", Path, errno);
}
// Now open the attribute directory
//
if (!(dP = fdopendir(fd)))
{rc = errno; close(fd);
if (rc == ENOATTR) return 0;
return Diagnose("open to list", "*", Path, rc);
}
// Now list the directory entries (less dot and dot-dot) as attributes.
// If the size is wanted, we do this here to avoid multiple opens.
//
while((dEnt = readdir(dP)))
{if ( (strcmp(".", dEnt->d_name) && strcmp("..", dEnt->d_name))
&& (aNew = getEnt(Path, fd, dEnt->d_name, *aPL, 0)))
{if (getSz)
{if (fstatat(dirfd(dP), dEnt->d_name, &Stat, 0))
{Diagnose("stat", dEnt->d_name, Path, errno); continue;}
aNew->Vlen = Stat.st_size;
if (maxSz < aNew->Vlen) maxSz = aNew->Vlen;
}
*aPL = aNew;
}
}
// All done
//
closedir(dP); close(fd);
return maxSz;
}
/******************************************************************************/
/* X r d S y s F A t t r : : S e t */
/******************************************************************************/
int XrdSysFAttr::Set(const char *Aname, const void *Aval, int Avsz,
const char *Path, int fd, int isNew)
{
static const mode_t aMode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;
int oFlag = O_CREAT|O_RDWR|O_TRUNC|O_XATTR | (isNew ? O_EXCL : 0);
int ec = 0;
// Open the directory associated with the path or file descriptor
//
fd = (fd < 0 ? attropen(Path, Aname, oFlag, aMode)
: openat( fd, Aname, oFlag, aMode));
// If the preceding open failed, diagnose the problem.
//
if (fd < 0) return Diagnose("open to set", Aname, Path, errno);
// Write the data, the caller should have made sure the file was truncated.
//
if (write(fd, Aval, Avsz) < 0) ec = Diagnose("set", Aname, Path, errno);
// All done, close the file and return result
//
close(fd);
return ec;
}
ENOATTR
#define ENOATTR
Definition XProtocol.hh:1343
Stat
struct stat Stat
Definition XrdCks.cc:49
close
#define close(a)
Definition XrdPosix.hh:48
fstat
#define fstat(a, b)
Definition XrdPosix.hh:62
write
#define write(a, b, c)
Definition XrdPosix.hh:115
closedir
#define closedir(a)
Definition XrdPosix.hh:50
stat
#define stat(a, b)
Definition XrdPosix.hh:101
readdir
#define readdir(a)
Definition XrdPosix.hh:86
read
#define read(a, b, c)
Definition XrdPosix.hh:82
Path
XrdOucString Path
Definition XrdSecpwdSrvAdmin.cc:160
ec
ec
Definition XrdSysFAttrSun.icc:100
dirfd
#define dirfd(x)
Definition XrdSysFAttrSun.icc:116
XrdSysFAttr.hh