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);
115 char unref[2049], filename[2049];
119 if (!path) {errno = EFAULT;
return -1;}
120 result = realpath(path, NULL);
122 strncpy(filename, path, 2048);
123 strncpy(unref, path, 2048);
127 int lsize = readlink(filename, unref, 2048);
128 while (lsize > 0 && i<10){
129 strncpy(filename, unref, 2049);
132 lsize = readlink(filename, unref, 2048);
140 strncpy(filename, result, 2048);
143 strncpy(resolved, filename, 2049);
144 return(strlen(filename));
154 char *myPath, buff[2048];
161 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
162 return Xunix.Access( path, amode);
166 return Xroot.Access(myPath, amode);
181int XrdPosix_Acl(
const char *path,
int cmd,
int nentries,
void *aclbufp)
186 if (res < 0)
return res;
188 ?
Xunix.Acl(
"/tmp", cmd,nentries,aclbufp)
189 :
Xunix.Acl(path, cmd,nentries,aclbufp));
205 if (res < 0)
return res;
225 return (
Xroot.myFD(fildes) ?
Xroot.Close(fildes) :
Xunix.Close(fildes));
238 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Closedir(dirp)
239 :
Xunix.Closedir(dirp));
253 return XrdPosix_Open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
265 int nullfd = fileno(stream);
273 return Xunix.Fclose(stream);
288 if (
Xroot.myFD(fd))
return 0;
290 theArg = va_arg(ap,
void *);
292 return Xunix.Fcntl64(fd, cmd, theArg);
307 return (
Xroot.myFD(fildes) ?
Xroot.Fsync(fildes)
308 :
Xunix.Fsync(fildes));
316#if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
319ssize_t XrdPosix_Fgetxattr (
int fd,
const char *name,
void *value,
size_t size)
321 if (
Xroot.myFD(fd)) {errno = ENOTSUP;
return -1;}
322 return Xunix.Fgetxattr(fd, name, value, size);
338 if (!stream || !
Xroot.myFD(fileno(stream)))
339 return Xunix.Fflush(stream);
341 return Xroot.Fsync(fileno(stream));
349#define ISMODE(x) !strcmp(mode, x)
355 char *myPath, buff[2048];
362 if (res < 0)
return 0;
366 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
367 return Xunix.Fopen64(path, mode);
375 | O_CREAT | O_APPEND;
380 | O_CREAT | O_APPEND;
381 else {errno = EINVAL;
return 0;}
390 if (!(stream = fdopen(fd, mode)))
391 {erc = errno;
Xroot.Close(fd); errno = erc;}
409 int fd = fileno(stream);
411 if (!
Xroot.myFD(fd))
return Xunix.Fread(ptr, size, nitems, stream);
413 bytes =
Xroot.Read(fd, ptr, size*nitems);
417 if (bytes > 0 && size) rc = bytes/size;
418 else if (bytes < 0)
fseterr(stream);
436 if (!
Xroot.myFD(fileno(stream)))
437 return Xunix.Fseek( stream, offset, whence);
439 return (
Xroot.Lseek(fileno(stream), offset, whence) < 0 ? -1 : 0);
454 if (!
Xroot.myFD(fileno(stream)))
455 return Xunix.Fseeko64(stream, offset, whence);
457 return (
Xroot.Lseek(fileno(stream), offset, whence) < 0 ? -1 : 0);
469 if (
Xroot.myFD(fildes)){
470 return(
Xroot.Fstat(fildes, buf));
473 return syscall(SYS_fstat, fildes, buf);
482 if (
Xroot.myFD(fildes)){
483 return(
Xroot.Fstat(fildes, buf));
486 return syscall(SYS_fstat, fildes, buf);
507 if (!(flags & AT_SYMLINK_NOFOLLOW)) {
510 if (res < 0)
return res;
513 strncpy(unref, path, 2048);
515 if (
char *myPath =
XrootPath.URL(unref, buff,
sizeof(buff))) {
516 int ret =
Xroot.Stat(myPath, (
struct stat *)buf);
521 return syscall(SYS_newfstatat,
dirfd, path, buf, flags);
524 return syscall(SYS_fstatat,
dirfd, path, buf, flags);
549 return (
Xroot.myFD(fildes) ?
Xroot.Fsync(fildes)
550 :
Xunix.Fsync(fildes));
565 if (!
Xroot.myFD(fileno(stream)))
return Xunix.Ftell(stream);
567 return static_cast<long>(
Xroot.Lseek(fileno(stream), 0, SEEK_CUR));
582 if (!
Xroot.myFD(fileno(stream)))
return Xunix.Ftello64(stream);
584 return Xroot.Lseek(fileno(stream), 0, SEEK_CUR);
599 return (
Xroot.myFD(fildes) ?
Xroot.Ftruncate (fildes, offset)
600 :
Xunix.Ftruncate64(fildes, offset));
612 size_t bytes, rc = 0;
613 int fd = fileno(stream);
615 if (!
Xroot.myFD(fd))
return Xunix.Fwrite(ptr, size, nitems, stream);
617 bytes =
Xroot.Write(fd, ptr, size*nitems);
621 if (bytes > 0 && size) rc = bytes/size;
632#if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
635ssize_t XrdPosix_Getxattr (
const char *path,
const char *name,
void *value,
size_t size)
637 char *myPath, buff[2048];
641 if (res < 0)
return res;
642 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
643 return Xunix.Getxattr(path, name, value, size);
645 return Xroot.Getxattr(myPath, name, value, size);
654#if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
657ssize_t XrdPosix_Lgetxattr (
const char *path,
const char *name,
void *value,
size_t size)
662 if (res < 0)
return res;
664 if (
XrootPath.URL(unref, 0, 0)) {errno = ENOTSUP;
return -1;}
665 return Xunix.Lgetxattr(path, name, value, size);
681 return (
Xroot.myFD(fildes) ?
Xroot.Lseek (fildes, offset, whence)
682 :
Xunix.Lseek64(fildes, offset, whence));
694 char *myPath, buff[2048];
698 if (!path) {errno = EFAULT;
return -1;}
702 myPath =
XrootPath.URL(path, buff,
sizeof(buff));
704 return Xroot.Stat(myPath, buf);
707 return syscall(SYS_lstat, path, buf);
724 char *myPath, buff[2048];
728 if (res < 0)
return res;
732 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
733 return Xunix.Mkdir(path, mode);
737 return Xroot.Mkdir(myPath, mode);
749 char *myPath, buff[2048];
754 if (res < 0)
return res;
757 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
758 {
if (!(oflag & O_CREAT))
return Xunix.Open64(unref, oflag);
760 mode = va_arg(ap,
int);
762 return Xunix.Open64(unref, oflag, (mode_t)mode);
767 if (!(oflag & O_CREAT))
return Xroot.Open(myPath, oflag);
769 mode = va_arg(ap,
int);
771 return Xroot.Open(myPath, oflag, (mode_t)mode);
783 char *myPath, buff[2048];
788 if (res < 0)
return res;
790 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff)))){
792 if (flag & (O_CREAT)) {
794 mode = va_arg(ap,
int);
798 return (syscall(SYS_openat,
dirfd, path, flag, (mode_t)mode));
806 if (!(flag & O_CREAT))
return Xroot.Open(myPath, flag);
808 mode = va_arg(ap,
int);
810 return Xroot.Open(myPath, flag, (mode_t)mode);
823 char *myPath, buff[2048];
827 if (res < 0)
return NULL;
831 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
832 return Xunix.Opendir(path);
836 return Xroot.Opendir(myPath);
853 if (res < 0)
return res;
856 :
Xunix.Pathconf(unref, name));
871 return (
Xroot.myFD(fildes) ?
Xroot.Pread (fildes, buf, nbyte, offset)
872 :
Xunix.Pread64(fildes, buf, nbyte, offset));
887 return (
Xroot.myFD(fildes) ?
Xroot.Pwrite (fildes, buf, nbyte, offset)
888 :
Xunix.Pwrite64(fildes, buf, nbyte, offset));
903 return (
Xroot.myFD(fildes) ?
Xroot.Read(fildes, buf, nbyte)
904 :
Xunix.Read(fildes, buf, nbyte));
919 return (
Xroot.myFD(fildes) ?
Xroot.Readv(fildes,
iov, iovcnt)
920 :
Xunix.Readv(fildes,
iov, iovcnt));
937 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir(dirp)
938 :
Xunix.Readdir(dirp));
946 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir64(dirp)
947 :
Xunix.Readdir64(dirp));
962 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir_r(dirp,entry,result)
963 :
Xunix.Readdir_r(dirp,entry,result));
971 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Readdir64_r(dirp,entry,result)
972 :
Xunix.Readdir64_r(dirp,entry,result));
984 char *oldPath, buffold[2048], *newPath, buffnew[2048];
988 if (!oldpath || !newpath) {errno = EFAULT;
return -1;}
992 if (!(oldPath =
XrootPath.URL(oldpath, buffold,
sizeof(buffold)))
993 || !(newPath =
XrootPath.URL(newpath, buffnew,
sizeof(buffnew))))
994 return Xunix.Rename(oldpath, newpath);
998 return Xroot.Rename(oldPath, newPath);
1013 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Rewinddir(dirp)
1014 :
Xunix.Rewinddir(dirp));
1026 char *myPath, buff[2048];
1030 if (!path) {errno = EFAULT;
return -1;}
1034 if (!(myPath =
XrootPath.URL(path, buff,
sizeof(buff))))
1035 return Xunix.Rmdir(path);
1039 return Xroot.Rmdir(myPath);
1054 (
Xroot.isXrootdDir(dirp) ?
Xroot.Seekdir(dirp, loc)
1055 :
Xunix.Seekdir(dirp, loc));
1071 if (res < 0)
return res;
1074 if (
char *myPath =
XrootPath.URL(unref, buff,
sizeof(buff))) {
1075 int ret =
Xroot.Stat(myPath, buf);
1080 return syscall(SYS_stat, path, buf);
1094 if (path && *path) {
1098 if (!(flags & AT_SYMLINK_NOFOLLOW)) {
1101 if (res < 0)
return res;
1103 strncpy(unref, path, 2048);
1105 if (
char *myPath =
XrootPath.URL(unref, buff,
sizeof(buff))) {
1113 int ret = syscall(SYS_statx,
dirfd, path, flags, mask, stx);
1135 char *myPath, buff[2048];
1140 if (res < 0)
return res;
1144 return ((myPath =
XrootPath.URL(unref, buff,
sizeof(buff)))
1145 ?
Xroot.Statfs(myPath, buf)
1158 char *myPath, buff[2048];
1162 if (res < 0)
return res;
1166 return ((myPath =
XrootPath.URL(unref, buff,
sizeof(buff)))
1167 ?
Xroot.Statvfs(myPath, buf)
1183 return (
Xroot.isXrootdDir(dirp) ?
Xroot.Telldir(dirp)
1184 :
Xunix.Telldir(dirp));
1196 char *myPath, buff[2048];
1200 if (res < 0)
return res;
1204 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
1205 return Xunix.Truncate64(path, offset);
1209 return Xroot.Truncate(myPath, offset);
1221 char *myPath, buff[2048];
1225 if (res < 0)
return res;
1229 if (!(myPath =
XrootPath.URL(unref, buff,
sizeof(buff))))
1230 return Xunix.Unlink(path);
1234 return Xroot.Unlink(myPath);
1249 return (
Xroot.myFD(fildes) ?
Xroot.Write(fildes, buf, nbyte)
1250 :
Xunix.Write(fildes, buf, nbyte));
1265 return (
Xroot.myFD(fildes) ?
Xroot.Writev(fildes,
iov, iovcnt)
1266 :
Xunix.Writev(fildes,
iov, iovcnt));
1276 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)
static ssize_t XrdResolveLink(const char *path, char *resolved)
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)
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
static void Stat2Statx(const struct stat &stat, XrdSysStatx &statx)