#include "TGFALFile.h"
#include "TROOT.h"
#include "TUrl.h"
extern "C" {
#include <gfal_api.h>
}
ClassImp(TGFALFile)
ClassImp(TGFALSystem)
TGFALFile::TGFALFile(const char *url, Option_t *option, const char *ftitle,
                     Int_t compress)
         : TFile(url, "NET", ftitle, compress)
{
   
   
   
   
   
   
   
   
   fStatCached = kFALSE;
   fOption = option;
   fOption.ToUpper();
   if (fOption == "NEW")
      fOption = "CREATE";
   Bool_t create   = (fOption == "CREATE") ? kTRUE : kFALSE;
   Bool_t recreate = (fOption == "RECREATE") ? kTRUE : kFALSE;
   Bool_t update   = (fOption == "UPDATE") ? kTRUE : kFALSE;
   Bool_t read     = (fOption == "READ") ? kTRUE : kFALSE;
   if (!create && !recreate && !update && !read) {
      read    = kTRUE;
      fOption = "READ";
   }
   TString stmp;
   char *fname;
   if ((fname = gSystem->ExpandPathName(fUrl.GetFile()))) {
      stmp = fname;
      delete [] fname;
      fname = (char *)stmp.Data();
   } else {
      Error("TGFALFile", "error expanding path %s", fUrl.GetFile());
      goto zombie;
   }
   if (recreate) {
      if (::gfal_access(fname, kFileExists) == 0)
         ::gfal_unlink(fname);
      recreate = kFALSE;
      create   = kTRUE;
      fOption  = "CREATE";
   }
   if (create && ::gfal_access(fname, kFileExists) == 0) {
      Error("TGFALFile", "file %s already exists", fname);
      goto zombie;
   }
   if (update) {
      if (::gfal_access(fname, kFileExists) != 0) {
         update = kFALSE;
         create = kTRUE;
      }
      if (update && ::gfal_access(fname, kWritePermission) != 0) {
         Error("TGFALFile", "no write permission, could not open file %s", fname);
         goto zombie;
      }
   }
   if (read) {
#ifdef GFAL_ACCESS_FIXED
      if (::gfal_access(fname, kFileExists) != 0) {
         Error("TGFALFile", "file %s does not exist", fname);
         goto zombie;
      }
      if (::gfal_access(fname, kReadPermission) != 0) {
         Error("TGFALFile", "no read permission, could not open file %s", fname);
         goto zombie;
      }
#endif
   }
   
   fRealName = fname;
   if (create || update) {
#ifndef WIN32
      fD = SysOpen(fname, O_RDWR | O_CREAT, 0644);
#else
      fD = SysOpen(fname, O_RDWR | O_CREAT | O_BINARY, S_IREAD | S_IWRITE);
#endif
      if (fD == -1) {
         SysError("TGFALFile", "file %s can not be opened", fname);
         goto zombie;
      }
      fWritable = kTRUE;
   } else {
#ifndef WIN32
      fD = SysOpen(fname, O_RDONLY, 0644);
#else
      fD = SysOpen(fname, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
#endif
      if (fD == -1) {
         SysError("TGFALFile", "file %s can not be opened for reading", fname);
         goto zombie;
      }
      fWritable = kFALSE;
   }
   Init(create);
   return;
zombie:
   
   MakeZombie();
   gDirectory = gROOT;
}
TGFALFile::~TGFALFile()
{
   
   Close();
}
Int_t TGFALFile::SysOpen(const char *pathname, Int_t flags, UInt_t mode)
{
   
   Int_t ret = ::gfal_open64(pathname, flags, (Int_t) mode);
   return ret;
}
Int_t TGFALFile::SysClose(Int_t fd)
{
   
   Int_t ret = ::gfal_close(fd);
   return ret;
}
Int_t TGFALFile::SysRead(Int_t fd, void *buf, Int_t len)
{
   
   fOffset += len;
   Int_t ret = ::gfal_read(fd, buf, len);
   return ret;
}
Int_t TGFALFile::SysWrite(Int_t fd, const void *buf, Int_t len)
{
   
   fOffset += len;
   Int_t ret = ::gfal_write(fd, buf, len);
   return ret;
}
Long64_t TGFALFile::SysSeek(Int_t fd, Long64_t offset, Int_t whence)
{
   
   
   
   if (whence == SEEK_SET && offset == fOffset) return offset;
   Long64_t ret = ::gfal_lseek64(fd, offset, whence);
   if (ret >= 0)
      fOffset = ret;
   return ret;
}
Int_t TGFALFile::SysStat(Int_t , Long_t *id, Long64_t *size, Long_t *flags,
                         Long_t *modtime)
{
   
   
   struct stat64 &statbuf = fStatBuffer;
   if (fOption != "READ" || !fStatCached) {
      
      
      if (::gfal_stat64(fRealName, &statbuf) >= 0)
         fStatCached = kTRUE;
   }
   if (fStatCached) {
      if (id)
         *id = (statbuf.st_dev << 24) + statbuf.st_ino;
      if (size)
         *size = statbuf.st_size;
      if (modtime)
         *modtime = statbuf.st_mtime;
      if (flags) {
         *flags = 0;
         if (statbuf.st_mode & ((S_IEXEC)|(S_IEXEC>>3)|(S_IEXEC>>6)))
            *flags |= 1;
         if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
            *flags |= 2;
         if ((statbuf.st_mode & S_IFMT) != S_IFREG &&
             (statbuf.st_mode & S_IFMT) != S_IFDIR)
            *flags |= 4;
      }
      return 0;
   }
   return 1;
}
Bool_t TGFALFile::ReadBuffer(char *buf, Int_t len)
{
   
   
   Int_t st;
   if ((st = ReadBufferViaCache(buf, len))) {
      if (st == 2)
         return kTRUE;
      return kFALSE;
   }
   return TFile::ReadBuffer(buf, len);
}
Bool_t TGFALFile::WriteBuffer(const char *buf, Int_t len)
{
   
   
   if (!IsOpen() || !fWritable) return kTRUE;
   Int_t st;
   if ((st = WriteBufferViaCache(buf, len))) {
      if (st == 2)
         return kTRUE;
      return kFALSE;
   }
   return TFile::WriteBuffer(buf, len);
}
TGFALSystem::TGFALSystem() : TSystem("-gfal", "GFAL Helper System")
{
   
   
   SetName("gfal");
   fDirp = 0;
}
Int_t TGFALSystem::MakeDirectory(const char *dir)
{
   
   TUrl url(dir);
   Int_t ret = ::gfal_mkdir(url.GetFile(), 0755);
   return ret;
}
void *TGFALSystem::OpenDirectory(const char *dir)
{
   
   
   if (fDirp) {
      Error("OpenDirectory", "invalid directory pointer (should never happen)");
      fDirp = 0;
   }
   TUrl url(dir);
   struct stat64 finfo;
   if (::gfal_stat64(url.GetFile(), &finfo) < 0)
      return 0;
   if ((finfo.st_mode & S_IFMT) != S_IFDIR)
      return 0;
   fDirp = (void*) ::gfal_opendir(url.GetFile());
   return fDirp;
}
void TGFALSystem::FreeDirectory(void *dirp)
{
   
   if (dirp != fDirp) {
      Error("FreeDirectory", "invalid directory pointer (should never happen)");
      return;
   }
   if (dirp)
      ::gfal_closedir((DIR*)dirp);
   fDirp = 0;
}
const char *TGFALSystem::GetDirEntry(void *dirp)
{
   
   if (dirp != fDirp) {
      Error("GetDirEntry", "invalid directory pointer (should never happen)");
      return 0;
   }
   struct dirent64 *dp;
   if (dirp) {
      dp = ::gfal_readdir64((DIR*)dirp);
      if (!dp)
         return 0;
      return dp->d_name;
   }
   return 0;
}
Int_t TGFALSystem::GetPathInfo(const char *path, FileStat_t &buf)
{
   
   
   
   
   TUrl url(path);
   struct stat64 sbuf;
   if (path && ::gfal_stat64(url.GetFile(), &sbuf) >= 0) {
      buf.fDev    = sbuf.st_dev;
      buf.fIno    = sbuf.st_ino;
      buf.fMode   = sbuf.st_mode;
      buf.fUid    = sbuf.st_uid;
      buf.fGid    = sbuf.st_gid;
      buf.fSize   = sbuf.st_size;
      buf.fMtime  = sbuf.st_mtime;
      buf.fIsLink = kFALSE;
      return 0;
   }
   return 1;
}
Bool_t TGFALSystem::AccessPathName(const char *path, EAccessMode mode)
{
   
   
   
   TUrl url(path);
   if (::gfal_access(url.GetFile(), mode) == 0)
      return kFALSE;
   return kTRUE;
}
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.