KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVTarArchive.cpp
Go to the documentation of this file.
1 /*
2 $Id: KVClassFactory.cpp,v 1.19 2009/01/21 08:04:20 franklan Exp $
3 $Revision: 1.19 $
4 $Date: 2009/01/21 08:04:20 $
5 */
6 
7 //Created by KVClassFactory on Wed Jun 3 08:40:52 2009
8 //Author: John Frankland,,,
9 
10 #include "KVTarArchive.h"
11 #include "TSystem.h"
12 #include "TSystemDirectory.h"
13 #include "TList.h"
14 
16 
17 
18 
19 
20 
24 {
25  // Default constructor
26  init();
27 }
28 
29 
30 
33 
34 KVTarArchive::KVTarArchive(const Char_t* dirname, const Char_t* path)
35 {
36  // Open directory/archive with name 'dirname'[.tgz] in directory 'path'
37 
38  init();
39  CheckDirectory(dirname, path);
40 }
41 
42 
43 
49 
51 {
52  // Destructor
53  // If the directory was extracted from an archive file, we delete the extracted
54  // directory and all files in it. This is so that the next time we need files from
55  // the archive, we reopen the archive (in case the archive has been updated).
56 
58 }
59 
60 
61 
64 
66 {
67  // Default initialisations for ctors
68  fOK = kFALSE;
69  fTGZ = kFALSE;
70  fFullpath = "";
71 }
72 
73 
74 
88 
89 void KVTarArchive::CheckDirectory(const Char_t* dirname, const Char_t* path)
90 {
91  // We look for the directory 'dirname' in the directory 'path', i.e. we look for
92  // path/dirname
93  //
94  // If not found, we look for the following compressed archive:
95  //
96  // path/dirname.tgz
97  //
98  // and if found, uncompress it (with 'tar -zxf').
99  //
100  // Sets fOK to kTRUE if directory/archive file exists.Full path to directory returned in 'fullpath'.
101  //
102  // Sets fOK to kFALSE if directory does not exist and no archive file found.
103 
104  fTGZ = kFALSE;
105  fOK = kFALSE;
106  KVString search = dirname;
107  if (!SearchFile(search.Data(), fFullpath, 1, path)) {
108  // directory not found; look for .tgz
109  search += ".tgz";
110  if (SearchFile(search.Data(), fFullpath, 1, path)) {
111  // uncompress in directory where tgz is found
112  TString pwd = gSystem->pwd();
114  TString cmd;
115  cmd.Form("tar -zxf %s", gSystem->BaseName(fFullpath.Data()));
116  Info("CheckDirectory", "Opening archive file %s", fFullpath.Data());
117  if (gSystem->Exec(cmd.Data())) {
118  Error("CheckDirectory", "Problem executing %s", cmd.Data());
119  fFullpath = "";
120  }
121  else {
122  fOK = kTRUE;
123  // strip '.tgz' to leave just full path to directory
125  fTGZ = kTRUE;
126  }
127  gSystem->cd(pwd.Data());
128  }
129  }
130  else {
131  fOK = kTRUE;
132  Info("CheckDirectory", "Found directory %s", fFullpath.Data());
133  }
134 }
135 
136 
137 
142 
144 {
145  // Delete all files in directory 'dirpath' (give full path) then delete the directory itself.
146  // This is to get round the fact that gSystem->Unlink(dirname) does not work if the
147  // directory contains any files.
148 
149  Info("DeleteDirectory", "Deleting %s", dirpath);
150  TString pwd = gSystem->WorkingDirectory();//keep working directory
151  TSystemDirectory dir("dir", dirpath);
152  // get list of files (and directories)
153  TList* files = dir.GetListOfFiles();
154  TIter nxtFile(files);
155  TSystemFile* fil = 0;
156  // delete all files (and directories)
157  while ((fil = (TSystemFile*)nxtFile())) {
158  if (fil->IsDirectory()) {
159  // recursively remove subdirectories if necessary
160  if (strcmp(fil->GetName(), "..") && strcmp(fil->GetName(), ".")) {
161  DeleteDirectory(fil->GetTitle());
162  }
163  }
164  else {
165  if (gSystem->Unlink(Form("%s/%s", fil->GetTitle(), fil->GetName())) == -1) {
166  Info("DeleteDirectory", "Cannot delete file %s in %s", fil->GetName(), fil->GetTitle());
167  }
168  }
169  }
170  delete files;
171  gSystem->cd(pwd.Data());//change back to working directory
172  // delete directory
173  if (gSystem->Unlink(dirpath) == 0)
174  Info("DeleteDirectory", "Directory %s deleted", dirpath);
175  else
176  Info("DeleteDirectory", "Cannot delete directory %s", dirpath);
177 }
178 
179 
Bool_t SearchFile(const Char_t *name, TString &fullpath, int ndirs,...)
Search for file in an arbitrary number of locations, return kTRUE if file found and put full path to ...
Definition: KVBase.cpp:486
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
const Bool_t kTRUE
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
Handles directories stored in .tgz archive files.
Definition: KVTarArchive.h:21
void init()
Default initialisations for ctors.
void CheckDirectory(const Char_t *dirname, const Char_t *path)
virtual ~KVTarArchive()
KVTarArchive()
Default constructor.
Bool_t fOK
set to kTRUE if directory/archive is found
Definition: KVTarArchive.h:25
Bool_t fTGZ
set to kTRUE if directory is extracted from '.tgz' archive
Definition: KVTarArchive.h:26
void DeleteDirectory(const Char_t *dirpath)
KVString fFullpath
full path to directory if found/extracted
Definition: KVTarArchive.h:27
virtual const char * GetName() const
virtual const char * GetTitle() const
virtual void Error(const char *method, const char *msgfmt,...) const
virtual void Info(const char *method, const char *msgfmt,...) const
Ssiz_t Length() const
const char * Data() const
void Form(const char *fmt,...)
TString & Remove(EStripType s, char c)
virtual TList * GetListOfFiles() const
virtual Bool_t IsDirectory(const char *dir=0) const
Bool_t cd(const char *path)
virtual const char * DirName(const char *pathname)
const char * pwd()
virtual Int_t Exec(const char *shellcmd)
virtual const char * BaseName(const char *pathname)
virtual const char * WorkingDirectory()
virtual int Unlink(const char *name)