37#include <sys/syscall.h>
72#if defined _IO_ERR_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
74 fp->_flags |= _IO_ERR_SEEN;
75#elif defined __sferror || defined __APPLE__ || defined __DragonFly__ || defined __FreeBSD__ || defined __ANDROID__
81#elif defined __UCLIBC__
82 fp->__modeflags |= __FLAG_ERROR;
86 #error "Unsupported platform! Please report it as a bug."
95#if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
97 fp->_flags |= _IO_EOF_SEEN;
98#elif defined __sferror || defined __APPLE__ || defined __DragonFly__ || defined __ANDROID__
100 fp->_flags |= __SEOF;
104#elif defined __UCLIBC__
105 fp->__modeflags |= __FLAG_EOF;
107 (void)
fseek(fp, 0L, SEEK_END);
123 char unref[2049], filename[2049];
124 char *result = realpath(path,
nullptr);
127 strlcpy(filename, result,
sizeof(filename));
130 bzero(filename,
sizeof(filename));
131 bzero(unref,
sizeof(unref));
133 strlcpy(filename, path,
sizeof(filename));
138 while (readlink(filename, unref,
sizeof(unref)) > 0 && ++i < 10) {
139 strlcpy(filename, unref,
sizeof(filename));
140 bzero(unref,
sizeof(unref));
148 if (errno != EINVAL && errno != ENOENT)
151 size_t len = strlen(filename);
153 errno = ENAMETOOLONG;
156 strlcpy(resolved, filename, rsize);
167 char *myPath, buff[2048];
174 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
175 return Xunix.Access( path, amode);
179 return Xroot.Access(myPath, amode);
194int XrdPosix_Acl(
const char *path,
int cmd,
int nentries,
void *aclbufp)
199 if (res < 0)
return res;
201 ?
Xunix.Acl(
"/tmp", cmd,nentries,aclbufp)
202 :
Xunix.Acl(path, cmd,nentries,aclbufp));
218 if (res < 0)
return res;
238 return (
Xroot.myFD(fildes) ?
Xroot.Close(fildes) :
Xunix.Close(fildes));
251 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Closedir(dirp)
252 :
Xunix.Closedir(dirp));
266 return XrdPosix_Open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
278 int nullfd = fileno(stream);
286 return Xunix.Fclose(stream);
301 if (
Xroot.myFD(fd))
return 0;
303 theArg = va_arg(ap,
void *);
305 return Xunix.Fcntl64(fd, cmd, theArg);
320 return (
Xroot.myFD(fildes) ?
Xroot.Fsync(fildes)
321 :
Xunix.Fsync(fildes));
329#if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
332ssize_t XrdPosix_Fgetxattr (
int fd,
const char *name,
void *value,
size_t size)
334 if (
Xroot.myFD(fd)) {errno = ENOTSUP;
return -1;}
335 return Xunix.Fgetxattr(fd, name, value, size);
351 if (!stream || !
Xroot.myFD(fileno(stream)))
352 return Xunix.Fflush(stream);
354 return Xroot.Fsync(fileno(stream));
362#define ISMODE(x) !strcmp(mode, x)
368 char *myPath, buff[2048];
375 if (res < 0)
return 0;
379 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
380 return Xunix.Fopen64(path, mode);
388 | O_CREAT | O_APPEND;
393 | O_CREAT | O_APPEND;
394 else {errno = EINVAL;
return 0;}
403 if (!(stream = fdopen(fd, mode)))
404 {erc = errno;
Xroot.Close(fd); errno = erc;}
422 int fd = fileno(stream);
424 if (!
Xroot.myFD(fd))
return Xunix.Fread(ptr, size, nitems, stream);
426 bytes =
Xroot.Read(fd, ptr, size*nitems);
430 if (bytes > 0 && size) rc = bytes/size;
431 else if (bytes < 0)
fseterr(stream);
449 if (!
Xroot.myFD(fileno(stream)))
450 return Xunix.Fseek( stream, offset, whence);
452 return (
Xroot.Lseek(fileno(stream), offset, whence) < 0 ? -1 : 0);
467 if (!
Xroot.myFD(fileno(stream)))
468 return Xunix.Fseeko64(stream, offset, whence);
470 return (
Xroot.Lseek(fileno(stream), offset, whence) < 0 ? -1 : 0);
482 if (
Xroot.myFD(fildes)){
483 return(
Xroot.Fstat(fildes, buf));
486 return syscall(SYS_fstat, fildes, buf);
495 if (
Xroot.myFD(fildes)){
496 return(
Xroot.Fstat(fildes, buf));
499 return syscall(SYS_fstat, fildes, buf);
520 if (!(flags & AT_SYMLINK_NOFOLLOW)) {
523 if (res < 0)
return res;
526 strncpy(unref, path, 2048);
528 if (
char *myPath =
XrootPath.URL(unref, buff,
sizeof(buff))) {
529 int ret =
Xroot.Stat(myPath, (
struct stat *)buf);
534 return syscall(SYS_newfstatat,
dirfd, path, buf, flags);
537 return syscall(SYS_fstatat,
dirfd, path, buf, flags);
562 return (
Xroot.myFD(fildes) ?
Xroot.Fsync(fildes)
563 :
Xunix.Fsync(fildes));
578 if (!
Xroot.myFD(fileno(stream)))
return Xunix.Ftell(stream);
580 return static_cast<long>(
Xroot.Lseek(fileno(stream), 0, SEEK_CUR));
595 if (!
Xroot.myFD(fileno(stream)))
return Xunix.Ftello64(stream);
597 return Xroot.Lseek(fileno(stream), 0, SEEK_CUR);
612 return (
Xroot.myFD(fildes) ?
Xroot.Ftruncate (fildes, offset)
613 :
Xunix.Ftruncate64(fildes, offset));
625 size_t bytes, rc = 0;
626 int fd = fileno(stream);
628 if (!
Xroot.myFD(fd))
return Xunix.Fwrite(ptr, size, nitems, stream);
630 bytes =
Xroot.Write(fd, ptr, size*nitems);
634 if (bytes > 0 && size) rc = bytes/size;
645#if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
648ssize_t XrdPosix_Getxattr (
const char *path,
const char *name,
void *value,
size_t size)
650 char *myPath, buff[2048];
654 if (res < 0)
return res;
655 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
656 return Xunix.Getxattr(path, name, value, size);
658 return Xroot.Getxattr(myPath, name, value, size);
667#if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
670ssize_t XrdPosix_Lgetxattr (
const char *path,
const char *name,
void *value,
size_t size)
675 if (res < 0)
return res;
677 if (
XrootPath.URL(unref, 0, 0)) {errno = ENOTSUP;
return -1;}
678 return Xunix.Lgetxattr(path, name, value, size);
694 return (
Xroot.myFD(fildes) ?
Xroot.Lseek (fildes, offset, whence)
695 :
Xunix.Lseek64(fildes, offset, whence));
707 char *myPath, buff[2048];
711 if (!path) {errno = EFAULT;
return -1;}
715 myPath =
XrootPath.URL(path, buff,
sizeof(buff));
717 return Xroot.Stat(myPath, buf);
720 return syscall(SYS_lstat, path, buf);
737 char *myPath, buff[2048];
741 if (res < 0)
return res;
745 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
746 return Xunix.Mkdir(path, mode);
750 return Xroot.Mkdir(myPath, mode);
762 char *myPath, buff[2048];
767 if (res < 0)
return res;
770 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
771 {
if (!(oflag & O_CREAT))
return Xunix.Open64(unref, oflag);
773 mode = va_arg(ap,
int);
775 return Xunix.Open64(unref, oflag, (mode_t)mode);
780 if (!(oflag & O_CREAT))
return Xroot.Open(myPath, oflag);
782 mode = va_arg(ap,
int);
784 return Xroot.Open(myPath, oflag, (mode_t)mode);
796 char *myPath, buff[2048];
801 if (res < 0)
return res;
803 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff)))){
805 if (flag & (O_CREAT)) {
807 mode = va_arg(ap,
int);
811 return (syscall(SYS_openat,
dirfd, path, flag, (mode_t)mode));
819 if (!(flag & O_CREAT))
return Xroot.Open(myPath, flag);
821 mode = va_arg(ap,
int);
823 return Xroot.Open(myPath, flag, (mode_t)mode);
836 char *myPath, buff[2048];
840 if (res < 0)
return NULL;
844 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
845 return Xunix.Opendir(path);
849 return Xroot.Opendir(myPath);
866 if (res < 0)
return res;
869 :
Xunix.Pathconf(unref, name));
884 return (
Xroot.myFD(fildes) ?
Xroot.Pread (fildes, buf, nbyte, offset)
885 :
Xunix.Pread64(fildes, buf, nbyte, offset));
900 return (
Xroot.myFD(fildes) ?
Xroot.Pwrite (fildes, buf, nbyte, offset)
901 :
Xunix.Pwrite64(fildes, buf, nbyte, offset));
916 return (
Xroot.myFD(fildes) ?
Xroot.Read(fildes, buf, nbyte)
917 :
Xunix.Read(fildes, buf, nbyte));
932 return (
Xroot.myFD(fildes) ?
Xroot.Readv(fildes,
iov, iovcnt)
933 :
Xunix.Readv(fildes,
iov, iovcnt));
950 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir(dirp)
951 :
Xunix.Readdir(dirp));
959 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir64(dirp)
960 :
Xunix.Readdir64(dirp));
975 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir_r(dirp,entry,result)
976 :
Xunix.Readdir_r(dirp,entry,result));
984 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir64_r(dirp,entry,result)
985 :
Xunix.Readdir64_r(dirp,entry,result));
997 char *oldPath, buffold[2048], *newPath, buffnew[2048];
1001 if (!oldpath || !newpath) {errno = EFAULT;
return -1;}
1005 if (!(oldPath =
XrootPath.URL(oldpath, buffold,
sizeof(buffold)))
1006 || !(newPath =
XrootPath.URL(newpath, buffnew,
sizeof(buffnew))))
1007 return Xunix.Rename(oldpath, newpath);
1011 return Xroot.Rename(oldPath, newPath);
1026 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Rewinddir(dirp)
1027 :
Xunix.Rewinddir(dirp));
1039 char *myPath, buff[2048];
1043 if (!path) {errno = EFAULT;
return -1;}
1047 if (!(myPath =
XrootPath.URL(path, buff,
sizeof(buff))))
1048 return Xunix.Rmdir(path);
1052 return Xroot.Rmdir(myPath);
1067 (
Xroot.isXrootdDir(dirp) ?
Xroot.Seekdir(dirp, loc)
1068 :
Xunix.Seekdir(dirp, loc));
1084 if (res < 0)
return res;
1087 if (
char *myPath =
XrootPath.URL(unref, buff,
sizeof(buff))) {
1088 int ret =
Xroot.Stat(myPath, buf);
1093 return syscall(SYS_stat, path, buf);
1107 if (path && *path) {
1111 if (!(flags & AT_SYMLINK_NOFOLLOW)) {
1114 if (res < 0)
return res;
1116 strncpy(unref, path, 2048);
1118 if (
char *myPath =
XrootPath.URL(unref, buff,
sizeof(buff))) {
1122 XrdSysStatxHelpers::Stat2Statx(st, *stx);
1126 int ret = syscall(SYS_statx,
dirfd, path, flags, mask, stx);
1148 char *myPath, buff[2048];
1153 if (res < 0)
return res;
1157 return ((myPath =
XrootPath.URL(unref, buff,
sizeof(buff)))
1158 ?
Xroot.Statfs(myPath, buf)
1171 char *myPath, buff[2048];
1175 if (res < 0)
return res;
1179 return ((myPath =
XrootPath.URL(unref, buff,
sizeof(buff)))
1180 ?
Xroot.Statvfs(myPath, buf)
1196 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Telldir(dirp)
1197 :
Xunix.Telldir(dirp));
1209 char *myPath, buff[2048];
1213 if (res < 0)
return res;
1217 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
1218 return Xunix.Truncate64(path, offset);
1222 return Xroot.Truncate(myPath, offset);
1234 char *myPath, buff[2048];
1238 if (res < 0)
return res;
1242 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
1243 return Xunix.Unlink(path);
1247 return Xroot.Unlink(myPath);
1262 return (
Xroot.myFD(fildes) ?
Xroot.Write(fildes, buf, nbyte)
1263 :
Xunix.Write(fildes, buf, nbyte));
1278 return (
Xroot.myFD(fildes) ?
Xroot.Writev(fildes,
iov, iovcnt)
1279 :
Xunix.Writev(fildes,
iov, iovcnt));
1289 return (0 !=
XrootPath.URL(path, 0, 0));
int statvfs64(const char *path, struct statvfs64 *buf)
int statfs64(const char *path, struct statfs64 *buf)
int XrdPosix_Statfs(const char *path, struct statfs *buf)
int XrdPosix_Truncate(const char *path, off_t offset)
ssize_t XrdPosix_Read(int fildes, void *buf, size_t nbyte)
int XrdPosix_Closedir(DIR *dirp)
int XrdPosix_Fsync(int fildes)
ssize_t XrdPosix_Readv(int fildes, const struct iovec *iov, int iovcnt)
static void fseterr(FILE *fp)
int XrdPosix_isMyPath(const char *path)
long long XrdPosix_Ftello(FILE *stream)
int XrdPosix_Open(const char *path, int oflag,...)
void XrdPosix_Rewinddir(DIR *dirp)
ssize_t XrdPosix_Pread(int fildes, void *buf, size_t nbyte, off_t offset)
int XrdPosix_Readdir64_r(DIR *dirp, struct dirent64 *entry, struct dirent64 **result)
int XrdPosix_Close(int fildes)
int XrdPosix_Openat(int dirfd, const char *path, int flag,...)
void XrdPosix_Seekdir(DIR *dirp, long loc)
int XrdPosix_Rmdir(const char *path)
int XrdPosix_Chdir(const char *path)
int XrdPosix_Stat(const char *path, struct stat *buf)
int XrdPosix_Rename(const char *oldpath, const char *newpath)
int XrdPosix_Fcntl(int fd, int cmd,...)
int XrdPosix_Fseek(FILE *stream, long offset, int whence)
long XrdPosix_Ftell(FILE *stream)
static void fseteof(FILE *fp)
int XrdPosix_Readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
int XrdPosix_Mkdir(const char *path, mode_t mode)
static ssize_t XrdResolveLink(const char *path, char *resolved, size_t rsize)
int XrdPosix_Fflush(FILE *stream)
DIR * XrdPosix_Opendir(const char *path)
ssize_t XrdPosix_Writev(int fildes, const struct iovec *iov, int iovcnt)
XrdPosixXrootPath XrootPath
long XrdPosix_Telldir(DIR *dirp)
int XrdPosix_FstatV(int ver, int fildes, struct stat *buf)
ssize_t XrdPosix_Pwrite(int fildes, const void *buf, size_t nbyte, off_t offset)
int XrdPosix_Lstat(const char *path, struct stat *buf)
int XrdPosix_Fstatat(int dirfd, const char *path, struct stat *buf, int flags)
int XrdPosix_Creat(const char *path, mode_t mode)
int XrdPosix_Statx(int dirfd, const char *path, int flags, unsigned int mask, XrdSysStatx *stx)
int XrdPosix_Statvfs(const char *path, struct statvfs *buf)
int XrdPosix_Acl(const char *path, int cmd, int nentries, void *aclbufp)
int XrdPosix_Fstat(int fildes, struct stat *buf)
off_t XrdPosix_Lseek(int fildes, off_t offset, int whence)
ssize_t XrdPosix_Write(int fildes, const void *buf, size_t nbyte)
FILE * XrdPosix_Fopen(const char *path, const char *mode)
size_t XrdPosix_Fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
int XrdPosix_Fclose(FILE *stream)
int XrdPosix_Fdatasync(int fildes)
int XrdPosix_Ftruncate(int fildes, long long offset)
long XrdPosix_Pathconf(const char *path, int name)
int XrdPosix_Unlink(const char *path)
char * XrdPosix_URL(const char *path, char *buff, int blen)
struct dirent64 * XrdPosix_Readdir64(DIR *dirp)
size_t XrdPosix_Fread(void *ptr, size_t size, size_t nitems, FILE *stream)
int XrdPosix_Fseeko(FILE *stream, long long offset, int whence)
int XrdPosix_Access(const char *path, int amode)
struct dirent * XrdPosix_Readdir(DIR *dirp)
POSIX interface to XRootD with some extensions, as noted.
static const int isStream