Logo ROOT  
Reference Guide
TAlienSystem.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Andreas Peters 15/05/2006
3
4/*************************************************************************
5 * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//////////////////////////////////////////////////////////////////////////
13// //
14// TAlienSystem //
15// //
16//////////////////////////////////////////////////////////////////////////
17
18#include <stdlib.h>
19#include <errno.h>
20
21#include "Riostream.h"
22#include "TAlienSystem.h"
23#include "TError.h"
24#include "TUrl.h"
25#include "TGrid.h"
26#include "gapi_dir_operations.h"
27#include "gapi_file_operations.h"
28#include "gapi_stat.h"
29
31
32////////////////////////////////////////////////////////////////////////////////
33/// Create a new OS interface.
34
35TAlienSystem::TAlienSystem(const char *name, const char *title) : TSystem(name, title)
36{
37 fWorkingDirectory[0] = '\0';
38}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Delete the OS interface.
42
44{
45}
46
47////////////////////////////////////////////////////////////////////////////////
48/// Initialize the OS interface.
49
51{
52 return kTRUE;
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Make a directory. Returns 0 in case of success and
57/// -1 if the directory could not be created (either already exists or
58/// illegal path name).
59
60int TAlienSystem::MakeDirectory(const char* dirname)
61{
62 if (!gGrid)
63 return -1;
64
65 if (strcmp(gGrid->GetGrid(),"alien")) {
66 Error("TAlienSystem","You are not connected to AliEn");
67 return -1;
68 }
69
70 TUrl url(dirname);
72 if (strcmp(url.GetProtocol(),"alien")) {
73 Info("OpenDirectory","Assuming an AliEn URL alien://%s",dirname);
74 url.SetProtocol("alien",kTRUE);
75 }
76 return gapi_mkdir(url.GetUrl(),0);
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Open a directory. Returns 0 if directory does not exist.
81
83{
84 TUrl url(name);
86 if (strcmp(url.GetProtocol(),"alien")) {
87 Info("OpenDirectory","Assuming an AliEn URL alien://%s",name);
88 url.SetProtocol("alien",kTRUE);
89 }
90 return (void*) gapi_opendir(url.GetUrl());
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Free a directory.
95
97{
98 gapi_closedir( (GAPI_DIR*)ptr);
99 return;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Get a directory entry. Returns 0 if no more entries.
104
105const char *TAlienSystem::GetDirEntry(void* ptr)
106{
107 struct dirent* retdir;
108 retdir = gapi_readdir( (GAPI_DIR*) ptr);
109 // AbstractMethod("GetDirEntry");
110 if (retdir)
111 return retdir->d_name;
112 return 0;
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Change directory.
117/// AbstractMethod("ChangeDirectory");
118/// return kFALSE;
119
121{
122 TUrl url(dirname);
123 url.CleanRelativePath();
124 if (strcmp(url.GetProtocol(),"alien")) {
125 Info("OpenDirectory","Assuming an AliEn URL alien://%s",dirname);
126 url.SetProtocol("alien",kTRUE);
127 }
128 return gapi_chdir(url.GetUrl());
129 // return gGrid->Cd(url.GetFile(),kFALSE);
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/// Return working directory.
134
136{
137 return gapi_getcwd(fWorkingDirectory,1024);
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Return the user's home directory.
142
143const char *TAlienSystem::HomeDirectory(const char*)
144{
145 if (!gGrid)
146 return 0;
147
148 if (strcmp(gGrid->GetGrid(),"alien")) {
149 Error("TAlienSystem","You are not connected to AliEn");
150 return 0;
151 }
152 return (gGrid->GetHomeDirectory());
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// Make a file system directory. Returns 0 in case of success and
157/// -1 if the directory could not be created (either already exists or
158/// illegal path name).
159/// If 'recursive' is true, makes parent directories as needed.
160
161int TAlienSystem::mkdir(const char *name, Bool_t recursive)
162{
163 if (recursive) {
164 TString dirname = DirName(name);
165 if (dirname.Length()==0) {
166 // well we should not have to make the root of the file system!
167 // (and this avoid infinite recursions!)
168 return -1;
169 }
170 if (AccessPathName(dirname, kFileExists)) {
171 int res = mkdir(dirname, kTRUE);
172 if (res) return res;
173 }
175 return -1;
176 }
177 }
178
179 return MakeDirectory(name);
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Copy a file. If overwrite is true and file already exists the
184/// file will be overwritten. Returns 0 when successful, -1 in case
185/// of failure, -2 in case the file already exists and overwrite was false.
186
187int TAlienSystem::CopyFile(const char *, const char *, Bool_t)
188{
189 AbstractMethod("CopyFile");
190 return -1;
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Rename a file.
195
196int TAlienSystem::Rename(const char *oldname, const char *newname)
197{
198 return gapi_rename(oldname,newname);
199 // AbstractMethod("Rename");
200 // return -1;
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// Create a link from file1 to file2.
205
206int TAlienSystem::Link(const char *, const char *)
207{
208 AbstractMethod("Link");
209 return -1;
210}
211
212////////////////////////////////////////////////////////////////////////////////
213/// Create a symbolic link from file1 to file2.
214
215int TAlienSystem::Symlink(const char *, const char *)
216{
217 AbstractMethod("Symlink");
218 return -1;
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Unlink, i.e. remove, a file.
223
224int TAlienSystem::Unlink(const char * filename)
225{
226 return gapi_unlink(filename);
227 // AbstractMethod("Unlink");
228 // return -1;
229}
230
231////////////////////////////////////////////////////////////////////////////////
232/// Get info about a file: id, size, flags, modification time.
233/// Id is (statbuf.st_dev << 24) + statbuf.st_ino
234/// Size is the file size
235/// Flags is file type: 0 is regular file, bit 0 set executable,
236/// bit 1 set directory, bit 2 set special file
237/// (socket, fifo, pipe, etc.)
238/// Modtime is modification time.
239/// The function returns 0 in case of success and 1 if the file could
240/// not be stat'ed.
241
242int TAlienSystem::GetPathInfo(const char *path, Long_t *id, Long_t *size,
243 Long_t *flags, Long_t *modtime)
244{
245 Long64_t lsize;
246
247 int res = GetPathInfo(path, id, &lsize, flags, modtime);
248
249 if (res == 0 && size) {
250 if (sizeof(Long_t) == 4 && lsize > kMaxInt) {
251 Error("GetPathInfo", "file %s > 2 GB, use GetPathInfo() with Long64_t size", path);
252 *size = kMaxInt;
253 } else {
254 *size = (Long_t)lsize;
255 }
256 }
257
258 return res;
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Get info about a file: id, size, flags, modification time.
263/// Id is (statbuf.st_dev << 24) + statbuf.st_ino
264/// Size is the file size
265/// Flags is file type: 0 is regular file, bit 0 set executable,
266/// bit 1 set directory, bit 2 set special file
267/// (socket, fifo, pipe, etc.)
268/// Modtime is modification time.
269/// The function returns 0 in case of success and 1 if the file could
270/// not be stat'ed.
271
272int TAlienSystem::GetPathInfo(const char *path, Long_t *id, Long64_t *size,
273 Long_t *flags, Long_t *modtime)
274{
275 FileStat_t buf;
276
277 int res = GetPathInfo(path, buf);
278
279 if (res == 0) {
280 if (id)
281 *id = (buf.fDev << 24) + buf.fIno;
282 if (size)
283 *size = buf.fSize;
284 if (modtime)
285 *modtime = buf.fMtime;
286 if (flags) {
287 *flags = 0;
288 if (buf.fMode & (kS_IXUSR|kS_IXGRP|kS_IXOTH))
289 *flags |= 1;
290 if (R_ISDIR(buf.fMode))
291 *flags |= 2;
292 if (!R_ISREG(buf.fMode) && !R_ISDIR(buf.fMode))
293 *flags |= 4;
294 }
295 }
296
297 return res;
298}
299
300////////////////////////////////////////////////////////////////////////////////
301/// Get info about a file. Info is returned in the form of a FileStat_t
302/// structure (see TSystem.h).
303/// The function returns 0 in case of success and 1 if the file could
304/// not be stat'ed.
305
306int TAlienSystem::GetPathInfo(const char *path, FileStat_t &buf)
307{
308 // AbstractMethod("GetPathInfo(const char*, FileStat_t&)");
309 return AlienFilestat(path,buf);
310}
311
312////////////////////////////////////////////////////////////////////////////////
313/// Get info about a file. Info is returned in the form of a FileStat_t
314/// structure (see TSystem.h).
315/// The function returns 0 in case of success and 1 if the file could
316/// not be stat'ed.
317
318int TAlienSystem::AlienFilestat(const char *fpath, FileStat_t &buf)
319{
320 TUrl url(fpath);
321 url.CleanRelativePath();
322 if (strcmp(url.GetProtocol(),"alien")) {
323 Info("AlienFilestat","Assuming an AliEn URL alien://%s",fpath);
324 url.SetProtocol("alien",kTRUE);
325 }
326#if defined(R__SEEK64)
327 struct stat64 sbuf;
328 if ((gapi_lstat(url.GetUrl(), (GAPI_STAT*)(&sbuf))) == 0) {
329#else
330 struct stat sbuf;
331 if ((gapi_lstat(url.GetUrl(), (GAPI_STAT*)(&sbuf))) == 0) {
332#endif
333 buf.fIsLink = S_ISLNK(sbuf.st_mode);
334 buf.fDev = sbuf.st_dev;
335 buf.fIno = sbuf.st_ino;
336 buf.fMode = sbuf.st_mode;
337 buf.fUid = sbuf.st_uid;
338 buf.fGid = sbuf.st_gid;
339 buf.fSize = sbuf.st_size;
340 buf.fMtime = sbuf.st_mtime;
341
342 return 0;
343 }
344 return 1;
345}
346
347////////////////////////////////////////////////////////////////////////////////
348/// Get info about a file system: fs type, block size, number of blocks,
349/// number of free blocks.
350
351int TAlienSystem::GetFsInfo(const char *, Long_t *, Long_t *, Long_t *, Long_t *)
352{
353 AbstractMethod("GetFsInfo");
354 return 1;
355}
356
357////////////////////////////////////////////////////////////////////////////////
358/// Set the file permission bits. Returns -1 in case or error, 0 otherwise.
359
360int TAlienSystem::Chmod(const char *file, UInt_t mode)
361{
362 TUrl url(file);
363 url.CleanRelativePath();
364 if (strcmp(url.GetProtocol(),"alien")) {
365 Info("AlienFilestat","Assuming an AliEn URL alien://%s",file);
366 url.SetProtocol("alien",kTRUE);
367 }
368 return gapi_chmod(url.GetUrl(),mode);
369 }
370
371////////////////////////////////////////////////////////////////////////////////
372/// Set the process file creation mode mask.
373
375{
376 AbstractMethod("Umask");
377 return -1;
378}
379
380////////////////////////////////////////////////////////////////////////////////
381/// Set the a files modification and access times. If actime = 0 it will be
382/// set to the modtime. Returns 0 on success and -1 in case of error.
383
385{
386 AbstractMethod("Utime");
387 return -1;
388}
389
390////////////////////////////////////////////////////////////////////////////////
391/// Find location of file in a search path.
392/// Returns 0 in case file is not found.
393
394const char *TAlienSystem::FindFile(const char *, TString&, EAccessMode)
395{
396 AbstractMethod("Which");
397 return 0;
398}
399
400////////////////////////////////////////////////////////////////////////////////
401/// Returns FALSE if one can access a file using the specified access mode.
402/// The file name must not contain any special shell characters line ~ or $,
403/// in those cases first call ExpandPathName().
404/// Attention, bizarre convention of return value!!
405
407{
408 if (!gGrid)
409 return -1;
410
411 if (strcmp(gGrid->GetGrid(),"alien")) {
412 Error("TAlienSystem","You are not connected to AliEn");
413 return -1;
414 }
415
416 TString strippath = path ;
417 // remove trailing '/'
418 while (strippath.EndsWith("/")) {strippath.Remove(strippath.Length()-1);}
419 TUrl url(strippath);
420 url.CleanRelativePath();
421
422 if (strcmp(url.GetProtocol(),"alien")) {
423 Info("AccessPathName","Assuming an AliEn URL alien://%s",path);
424 url.SetProtocol("alien",kTRUE);
425 }
426 if(!gapi_access(url.GetUrl(),mode)) {
427 return kFALSE;
428 } else {
429 return kTRUE;
430 }
431}
432
433
434//---- Users & Groups ----------------------------------------------------------
435
436////////////////////////////////////////////////////////////////////////////////
437/// Returns the user's id. If user = 0, returns current user's id.
438
439Int_t TAlienSystem::GetUid(const char * /*user*/)
440{
441 AbstractMethod("GetUid");
442 return 0;
443}
444
445////////////////////////////////////////////////////////////////////////////////
446/// Returns the effective user id. The effective id corresponds to the
447/// set id bit on the file being executed.
448
450{
451 AbstractMethod("GetEffectiveUid");
452 return 0;
453}
454
455////////////////////////////////////////////////////////////////////////////////
456/// Returns the group's id. If group = 0, returns current user's group.
457
458Int_t TAlienSystem::GetGid(const char * /*group*/)
459{
460 AbstractMethod("GetGid");
461 return 0;
462}
463
464////////////////////////////////////////////////////////////////////////////////
465/// Returns the effective group id. The effective group id corresponds
466/// to the set id bit on the file being executed.
467
469{
470 AbstractMethod("GetEffectiveGid");
471 return 0;
472}
473
474////////////////////////////////////////////////////////////////////////////////
475/// Returns all user info in the UserGroup_t structure. The returned
476/// structure must be deleted by the user. In case of error 0 is returned.
477
479{
480 AbstractMethod("GetUserInfo");
481 return 0;
482}
483
484////////////////////////////////////////////////////////////////////////////////
485/// Returns all user info in the UserGroup_t structure. If user = 0, returns
486/// current user's id info. The returned structure must be deleted by the
487/// user. In case of error 0 is returned.
488
490{
491 AbstractMethod("GetUserInfo");
492 return 0;
493}
494
495////////////////////////////////////////////////////////////////////////////////
496/// Returns all group info in the UserGroup_t structure. The only active
497/// fields in the UserGroup_t structure for this call are:
498/// fGid and fGroup
499/// The returned structure must be deleted by the user. In case of
500/// error 0 is returned.
501
503{
504 AbstractMethod("GetGroupInfo");
505 return 0;
506}
507
508////////////////////////////////////////////////////////////////////////////////
509/// Returns all group info in the UserGroup_t structure. The only active
510/// fields in the UserGroup_t structure for this call are:
511/// fGid and fGroup
512/// If group = 0, returns current user's group. The returned structure
513/// must be deleted by the user. In case of error 0 is returned.
514
516{
517 AbstractMethod("GetGroupInfo");
518 return 0;
519}
const Int_t kMaxInt
Definition: RtypesCore.h:101
const Bool_t kFALSE
Definition: RtypesCore.h:90
long Long_t
Definition: RtypesCore.h:52
long long Long64_t
Definition: RtypesCore.h:71
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
char name[80]
Definition: TGX11.cxx:109
R__EXTERN TGrid * gGrid
Definition: TGrid.h:128
EAccessMode
Definition: TSystem.h:42
@ kFileExists
Definition: TSystem.h:43
Bool_t R_ISREG(Int_t mode)
Definition: TSystem.h:117
Bool_t R_ISDIR(Int_t mode)
Definition: TSystem.h:114
@ kS_IXOTH
Definition: TSystem.h:111
@ kS_IXUSR
Definition: TSystem.h:103
@ kS_IXGRP
Definition: TSystem.h:107
virtual Bool_t Init()
Initialize the OS interface.
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
virtual UserGroup_t * GetGroupInfo(Int_t gid)
Returns all group info in the UserGroup_t structure.
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
virtual const char * WorkingDirectory()
Return working directory.
virtual const char * FindFile(const char *search, TString &file, EAccessMode mode=kFileExists)
Find location of file in a search path.
virtual Int_t GetUid(const char *user=0)
Returns the user's id. If user = 0, returns current user's id.
virtual int GetFsInfo(const char *path, Long_t *id, Long_t *bsize, Long_t *blocks, Long_t *bfree)
Get info about a file system: fs type, block size, number of blocks, number of free blocks.
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
char fWorkingDirectory[1024]
Definition: TAlienSystem.h:32
virtual Int_t GetGid(const char *group=0)
Returns the group's id. If group = 0, returns current user's group.
virtual int Symlink(const char *from, const char *to)
Create a symbolic link from file1 to file2.
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
virtual int Utime(const char *file, Long_t modtime, Long_t actime)
Set the a files modification and access times.
virtual int Umask(Int_t mask)
Set the process file creation mode mask.
virtual Int_t GetEffectiveUid()
Returns the effective user id.
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
virtual const char * HomeDirectory(const char *userName=0)
Return the user's home directory.
virtual int Link(const char *from, const char *to)
Create a link from file1 to file2.
virtual void FreeDirectory(void *dirp)
Free a directory.
virtual ~TAlienSystem()
Delete the OS interface.
virtual int MakeDirectory(const char *name)
Make a directory.
virtual int Chmod(const char *file, UInt_t mode)
Set the file permission bits. Returns -1 in case or error, 0 otherwise.
virtual int CopyFile(const char *from, const char *to, Bool_t overwrite=kFALSE)
Copy a file.
TAlienSystem(const char *name="Generic", const char *title="Generic System")
Create a new OS interface.
virtual Int_t GetEffectiveGid()
Returns the effective group id.
virtual int AlienFilestat(const char *fpath, FileStat_t &buf)
Get info about a file.
virtual Bool_t ChangeDirectory(const char *path)
Change directory.
virtual int Rename(const char *from, const char *to)
Rename a file.
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
virtual UserGroup_t * GetUserInfo(Int_t uid)
Returns all user info in the UserGroup_t structure.
const char * GetGrid() const
Definition: TGrid.h:60
virtual const char * GetHomeDirectory()
Definition: TGrid.h:88
void AbstractMethod(const char *method) const
Use this method to implement an "abstract" method that you don't want to leave purely abstract.
Definition: TObject.cxx:933
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:865
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2177
TString & Remove(Ssiz_t pos)
Definition: TString.h:668
Abstract base class defining a generic interface to the underlying Operating System.
Definition: TSystem.h:265
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1002
This class represents a WWW compatible URL.
Definition: TUrl.h:35
void CleanRelativePath()
Recompute the path removing all relative directory jumps via '..'.
Definition: TUrl.cxx:685
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:387
void SetProtocol(const char *proto, Bool_t setDefaultPort=kFALSE)
Set protocol and, optionally, change the port accordingly.
Definition: TUrl.cxx:520
const char * GetProtocol() const
Definition: TUrl.h:66
Definition: file.py:1
Int_t fMode
Definition: TSystem.h:126
Long64_t fSize
Definition: TSystem.h:129
Long_t fDev
Definition: TSystem.h:124
Int_t fGid
Definition: TSystem.h:128
Long_t fMtime
Definition: TSystem.h:130
Long_t fIno
Definition: TSystem.h:125
Bool_t fIsLink
Definition: TSystem.h:131
Int_t fUid
Definition: TSystem.h:127