TAlienSystem.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TAlienSystem.cxx 31532 2009-12-03 15:51:37Z rdm $
00002 // Author: Andreas Peters   15/05/2006
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TAlienSystem                                                         //
00015 //                                                                      //
00016 //////////////////////////////////////////////////////////////////////////
00017 
00018 #include <stdlib.h>
00019 #include <errno.h>
00020 
00021 #include "Riostream.h"
00022 #include "TAlienSystem.h"
00023 #include "TError.h"
00024 #include "TUrl.h"
00025 #include "TGrid.h"
00026 #include "gapi_dir_operations.h"
00027 #include "gapi_file_operations.h"
00028 #include "gapi_stat.h"
00029 
00030 ClassImp(TAlienSystem)
00031 
00032 //______________________________________________________________________________
00033 TAlienSystem::TAlienSystem(const char *name, const char *title) : TSystem(name, title)
00034 {
00035    // Create a new OS interface.
00036 }
00037 
00038 //______________________________________________________________________________
00039 TAlienSystem::~TAlienSystem()
00040 {
00041    // Delete the OS interface.
00042 }
00043 
00044 //______________________________________________________________________________
00045 Bool_t TAlienSystem::Init()
00046 {
00047    // Initialize the OS interface.
00048   return kTRUE;
00049 }
00050 
00051 //______________________________________________________________________________
00052 int TAlienSystem::MakeDirectory(const char* dirname)
00053 {
00054    // Make a directory. Returns 0 in case of success and
00055    // -1 if the directory could not be created (either already exists or
00056    // illegal path name).
00057 
00058   if (!gGrid)
00059     return -1;
00060 
00061   if (strcmp(gGrid->GetGrid(),"alien")) {
00062     Error("TAlienSystem","You are not connected to AliEn");
00063     return -1;
00064   }
00065 
00066   TUrl url(dirname);
00067   url.CleanRelativePath();
00068   if (strcmp(url.GetProtocol(),"alien")) {
00069     Info("OpenDirectory","Assuming an AliEn URL alien://%s",dirname);
00070     url.SetProtocol("alien",kTRUE);
00071   }
00072   return gapi_mkdir(url.GetUrl(),0);
00073 }
00074 
00075 //______________________________________________________________________________
00076 void *TAlienSystem::OpenDirectory(const char* name)
00077 {
00078    // Open a directory. Returns 0 if directory does not exist.
00079    TUrl url(name);
00080    url.CleanRelativePath();
00081    if (strcmp(url.GetProtocol(),"alien")) {
00082      Info("OpenDirectory","Assuming an AliEn URL alien://%s",name);
00083      url.SetProtocol("alien",kTRUE);
00084    }
00085    return (void*) gapi_opendir(url.GetUrl());
00086 
00087    AbstractMethod("OpenDirectory");
00088    return 0;
00089 }
00090 
00091 //______________________________________________________________________________
00092 void TAlienSystem::FreeDirectory(void* ptr)
00093 {
00094    // Free a directory.
00095    gapi_closedir( (GAPI_DIR*)ptr);
00096    return;
00097    AbstractMethod("FreeDirectory");
00098 }
00099 
00100 //______________________________________________________________________________
00101 const char *TAlienSystem::GetDirEntry(void* ptr)
00102 {
00103    // Get a directory entry. Returns 0 if no more entries.
00104    struct dirent* retdir;
00105    retdir = gapi_readdir( (GAPI_DIR*) ptr);
00106    //   AbstractMethod("GetDirEntry");
00107    if (retdir)
00108      return retdir->d_name;
00109    return 0;
00110 }
00111 
00112 //______________________________________________________________________________
00113 Bool_t TAlienSystem::ChangeDirectory(const char* dirname)
00114 {
00115    // Change directory.
00116    //   AbstractMethod("ChangeDirectory");
00117    //   return kFALSE;
00118 
00119   TUrl url(dirname);
00120   url.CleanRelativePath();
00121   if (strcmp(url.GetProtocol(),"alien")) {
00122     Info("OpenDirectory","Assuming an AliEn URL alien://%s",dirname);
00123     url.SetProtocol("alien",kTRUE);
00124   }
00125   return gapi_chdir(url.GetUrl());
00126   //  return gGrid->Cd(url.GetFile(),kFALSE);
00127 }
00128 
00129 //______________________________________________________________________________
00130 const char *TAlienSystem::WorkingDirectory()
00131 {
00132    // Return working directory.
00133   return gapi_getcwd(fWorkingDirectory,1024);
00134 }
00135 
00136 //______________________________________________________________________________
00137 const char *TAlienSystem::HomeDirectory(const char*)
00138 {
00139    // Return the user's home directory.
00140   if (!gGrid)
00141     return 0;
00142 
00143   if (strcmp(gGrid->GetGrid(),"alien")) {
00144     Error("TAlienSystem","You are not connected to AliEn");
00145     return 0;
00146   }
00147   return (gGrid->GetHomeDirectory());
00148 }
00149 
00150 //______________________________________________________________________________
00151 int TAlienSystem::mkdir(const char *name, Bool_t recursive)
00152 {
00153    // Make a file system directory. Returns 0 in case of success and
00154    // -1 if the directory could not be created (either already exists or
00155    // illegal path name).
00156    // If 'recursive' is true, makes parent directories as needed.
00157 
00158    if (recursive) {
00159       TString dirname = DirName(name);
00160       if (dirname.Length()==0) {
00161          // well we should not have to make the root of the file system!
00162          // (and this avoid infinite recursions!)
00163          return -1;
00164       }
00165       if (AccessPathName(dirname, kFileExists)) {
00166          int res = mkdir(dirname, kTRUE);
00167          if (res) return res;
00168       }
00169       if (!AccessPathName(name, kFileExists)) {
00170          return -1;
00171       }
00172    }
00173 
00174    return MakeDirectory(name);
00175 }
00176 
00177 //______________________________________________________________________________
00178 int TAlienSystem::CopyFile(const char *, const char *, Bool_t)
00179 {
00180    // Copy a file. If overwrite is true and file already exists the
00181    // file will be overwritten. Returns 0 when successful, -1 in case
00182    // of failure, -2 in case the file already exists and overwrite was false.
00183 
00184    AbstractMethod("CopyFile");
00185    return -1;
00186 }
00187 
00188 //______________________________________________________________________________
00189 int TAlienSystem::Rename(const char *oldname, const char *newname)
00190 {
00191    // Rename a file.
00192    return gapi_rename(oldname,newname);
00193    //  AbstractMethod("Rename");
00194    //   return -1;
00195 }
00196 
00197 //______________________________________________________________________________
00198 int TAlienSystem::Link(const char *, const char *)
00199 {
00200    // Create a link from file1 to file2.
00201 
00202    AbstractMethod("Link");
00203    return -1;
00204 }
00205 
00206 //______________________________________________________________________________
00207 int TAlienSystem::Symlink(const char *, const char *)
00208 {
00209    // Create a symbolic link from file1 to file2.
00210 
00211    AbstractMethod("Symlink");
00212    return -1;
00213 }
00214 
00215 //______________________________________________________________________________
00216 int TAlienSystem::Unlink(const char * filename)
00217 {
00218    // Unlink, i.e. remove, a file.
00219 
00220    return gapi_unlink(filename);
00221    //   AbstractMethod("Unlink");
00222    //   return -1;
00223 }
00224 
00225 //______________________________________________________________________________
00226 int TAlienSystem::GetPathInfo(const char *path, Long_t *id, Long_t *size,
00227                          Long_t *flags, Long_t *modtime)
00228 {
00229    // Get info about a file: id, size, flags, modification time.
00230    // Id      is (statbuf.st_dev << 24) + statbuf.st_ino
00231    // Size    is the file size
00232    // Flags   is file type: 0 is regular file, bit 0 set executable,
00233    //                       bit 1 set directory, bit 2 set special file
00234    //                       (socket, fifo, pipe, etc.)
00235    // Modtime is modification time.
00236    // The function returns 0 in case of success and 1 if the file could
00237    // not be stat'ed.
00238 
00239    Long64_t lsize;
00240 
00241    int res = GetPathInfo(path, id, &lsize, flags, modtime);
00242 
00243    if (res == 0 && size) {
00244       if (sizeof(Long_t) == 4 && lsize > kMaxInt) {
00245          Error("GetPathInfo", "file %s > 2 GB, use GetPathInfo() with Long64_t size", path);
00246          *size = kMaxInt;
00247       } else {
00248          *size = (Long_t)lsize;
00249       }
00250    }
00251 
00252    return res;
00253 }
00254 
00255 //______________________________________________________________________________
00256 int TAlienSystem::GetPathInfo(const char *path, Long_t *id, Long64_t *size,
00257                          Long_t *flags, Long_t *modtime)
00258 {
00259    // Get info about a file: id, size, flags, modification time.
00260    // Id      is (statbuf.st_dev << 24) + statbuf.st_ino
00261    // Size    is the file size
00262    // Flags   is file type: 0 is regular file, bit 0 set executable,
00263    //                       bit 1 set directory, bit 2 set special file
00264    //                       (socket, fifo, pipe, etc.)
00265    // Modtime is modification time.
00266    // The function returns 0 in case of success and 1 if the file could
00267    // not be stat'ed.
00268 
00269    FileStat_t buf;
00270 
00271    int res = GetPathInfo(path, buf);
00272 
00273    if (res == 0) {
00274       if (id)
00275          *id = (buf.fDev << 24) + buf.fIno;
00276       if (size)
00277          *size = buf.fSize;
00278       if (modtime)
00279          *modtime = buf.fMtime;
00280       if (flags) {
00281          *flags = 0;
00282          if (buf.fMode & (kS_IXUSR|kS_IXGRP|kS_IXOTH))
00283             *flags |= 1;
00284          if (R_ISDIR(buf.fMode))
00285             *flags |= 2;
00286          if (!R_ISREG(buf.fMode) && !R_ISDIR(buf.fMode))
00287             *flags |= 4;
00288       }
00289    }
00290 
00291    return res;
00292 }
00293 
00294 //______________________________________________________________________________
00295 int TAlienSystem::GetPathInfo(const char *path, FileStat_t &buf)
00296 {
00297    // Get info about a file. Info is returned in the form of a FileStat_t
00298    // structure (see TSystem.h).
00299    // The function returns 0 in case of success and 1 if the file could
00300    // not be stat'ed.
00301 
00302    //   AbstractMethod("GetPathInfo(const char*, FileStat_t&)");
00303   return AlienFilestat(path,buf);
00304 }
00305 
00306 //______________________________________________________________________________
00307 int TAlienSystem::AlienFilestat(const char *fpath, FileStat_t &buf)
00308 {
00309    // Get info about a file. Info is returned in the form of a FileStat_t
00310    // structure (see TSystem.h).
00311    // The function returns 0 in case of success and 1 if the file could
00312    // not be stat'ed.
00313 
00314    TUrl url(fpath);
00315    url.CleanRelativePath();
00316    if (strcmp(url.GetProtocol(),"alien")) {
00317      Info("AlienFilestat","Assuming an AliEn URL alien://%s",fpath);
00318      url.SetProtocol("alien",kTRUE);
00319    }
00320 #if defined(R__SEEK64)
00321    struct stat64 sbuf;
00322    if ((gapi_lstat(url.GetUrl(), (GAPI_STAT*)(&sbuf))) == 0) {
00323 #else
00324    struct stat sbuf;
00325    if ((gapi_lstat(url.GetUrl(), (GAPI_STAT*)(&sbuf))) == 0) {
00326 #endif
00327       buf.fIsLink = S_ISLNK(sbuf.st_mode);
00328       buf.fDev   = sbuf.st_dev;
00329       buf.fIno   = sbuf.st_ino;
00330       buf.fMode  = sbuf.st_mode;
00331       buf.fUid   = sbuf.st_uid;
00332       buf.fGid   = sbuf.st_gid;
00333       buf.fSize  = sbuf.st_size;
00334       buf.fMtime = sbuf.st_mtime;
00335 
00336       return 0;
00337    }
00338    return 1;
00339 }
00340 
00341 //______________________________________________________________________________
00342 int TAlienSystem::GetFsInfo(const char *, Long_t *, Long_t *, Long_t *, Long_t *)
00343 {
00344    // Get info about a file system: fs type, block size, number of blocks,
00345    // number of free blocks.
00346 
00347    AbstractMethod("GetFsInfo");
00348    return 1;
00349 }
00350 
00351 //______________________________________________________________________________
00352 int TAlienSystem::Chmod(const char *file, UInt_t mode)
00353 {
00354    // Set the file permission bits. Returns -1 in case or error, 0 otherwise.
00355    TUrl url(file);
00356    url.CleanRelativePath();
00357    if (strcmp(url.GetProtocol(),"alien")) {
00358      Info("AlienFilestat","Assuming an AliEn URL alien://%s",file);
00359      url.SetProtocol("alien",kTRUE);
00360    }
00361    return gapi_chmod(url.GetUrl(),mode);
00362  }
00363 
00364 //______________________________________________________________________________
00365 int TAlienSystem::Umask(Int_t)
00366 {
00367    // Set the process file creation mode mask.
00368 
00369    AbstractMethod("Umask");
00370    return -1;
00371 }
00372 
00373 //______________________________________________________________________________
00374 int TAlienSystem::Utime(const char *, Long_t, Long_t)
00375 {
00376    // Set the a files modification and access times. If actime = 0 it will be
00377    // set to the modtime. Returns 0 on success and -1 in case of error.
00378 
00379    AbstractMethod("Utime");
00380    return -1;
00381 }
00382 
00383 //______________________________________________________________________________
00384 const char *TAlienSystem::FindFile(const char *, TString&, EAccessMode)
00385 {
00386    // Find location of file in a search path.
00387    // Returns 0 in case file is not found.
00388 
00389    AbstractMethod("Which");
00390    return 0;
00391 }
00392 
00393 //______________________________________________________________________________
00394 Bool_t TAlienSystem::AccessPathName(const char *path, EAccessMode mode)
00395 {
00396    // Returns FALSE if one can access a file using the specified access mode.
00397    // The file name must not contain any special shell characters line ~ or $,
00398    // in those cases first call ExpandPathName().
00399    // Attention, bizarre convention of return value!!
00400 
00401    if (!gGrid)
00402       return -1;
00403 
00404    if (strcmp(gGrid->GetGrid(),"alien")) {
00405       Error("TAlienSystem","You are not connected to AliEn");
00406       return -1;
00407    }
00408 
00409    TString strippath = path ;
00410    // remove trailing '/' 
00411    while (strippath.EndsWith("/")) {strippath.Remove(strippath.Length()-1);}
00412    TUrl url(strippath);
00413    url.CleanRelativePath();
00414 
00415    if (strcmp(url.GetProtocol(),"alien")) {
00416       Info("AccessPathName","Assuming an AliEn URL alien://%s",path);
00417       url.SetProtocol("alien",kTRUE);
00418    }
00419    if(!gapi_access(url.GetUrl(),mode)) {
00420       return kFALSE;
00421    } else {
00422       return kTRUE;
00423    }
00424 }
00425 
00426 
00427 //---- Users & Groups ----------------------------------------------------------
00428 
00429 //______________________________________________________________________________
00430 Int_t TAlienSystem::GetUid(const char * /*user*/)
00431 {
00432    // Returns the user's id. If user = 0, returns current user's id.
00433 
00434    AbstractMethod("GetUid");
00435    return 0;
00436 }
00437 
00438 //______________________________________________________________________________
00439 Int_t TAlienSystem::GetEffectiveUid()
00440 {
00441    // Returns the effective user id. The effective id corresponds to the
00442    // set id bit on the file being executed.
00443 
00444    AbstractMethod("GetEffectiveUid");
00445    return 0;
00446 }
00447 
00448 //______________________________________________________________________________
00449 Int_t TAlienSystem::GetGid(const char * /*group*/)
00450 {
00451    // Returns the group's id. If group = 0, returns current user's group.
00452 
00453    AbstractMethod("GetGid");
00454    return 0;
00455 }
00456 
00457 //______________________________________________________________________________
00458 Int_t TAlienSystem::GetEffectiveGid()
00459 {
00460    // Returns the effective group id. The effective group id corresponds
00461    // to the set id bit on the file being executed.
00462 
00463    AbstractMethod("GetEffectiveGid");
00464    return 0;
00465 }
00466 
00467 //______________________________________________________________________________
00468 UserGroup_t *TAlienSystem::GetUserInfo(Int_t /*uid*/)
00469 {
00470    // Returns all user info in the UserGroup_t structure. The returned
00471    // structure must be deleted by the user. In case of error 0 is returned.
00472 
00473    AbstractMethod("GetUserInfo");
00474    return 0;
00475 }
00476 
00477 //______________________________________________________________________________
00478 UserGroup_t *TAlienSystem::GetUserInfo(const char * /*user*/)
00479 {
00480    // Returns all user info in the UserGroup_t structure. If user = 0, returns
00481    // current user's id info. The returned structure must be deleted by the
00482    // user. In case of error 0 is returned.
00483 
00484    AbstractMethod("GetUserInfo");
00485    return 0;
00486 }
00487 
00488 //______________________________________________________________________________
00489 UserGroup_t *TAlienSystem::GetGroupInfo(Int_t /*gid*/)
00490 {
00491    // Returns all group info in the UserGroup_t structure. The only active
00492    // fields in the UserGroup_t structure for this call are:
00493    //    fGid and fGroup
00494    // The returned structure must be deleted by the user. In case of
00495    // error 0 is returned.
00496 
00497    AbstractMethod("GetGroupInfo");
00498    return 0;
00499 }
00500 
00501 //______________________________________________________________________________
00502 UserGroup_t *TAlienSystem::GetGroupInfo(const char * /*group*/)
00503 {
00504    // Returns all group info in the UserGroup_t structure. The only active
00505    // fields in the UserGroup_t structure for this call are:
00506    //    fGid and fGroup
00507    // If group = 0, returns current user's group. The returned structure
00508    // must be deleted by the user. In case of error 0 is returned.
00509 
00510    AbstractMethod("GetGroupInfo");
00511    return 0;
00512 }

Generated on Tue Jul 5 14:45:30 2011 for ROOT_528-00b_version by  doxygen 1.5.1