DABC (Data Acquisition Backbone Core)  2.9.9
FileInterface.cxx
Go to the documentation of this file.
1 #include "rfio/FileInterface.h"
2 
3 #include "rawapin.h"
4 
5 #include <cstring>
6 
7 #include "dabc/Url.h"
8 #include "dabc/logging.h"
9 
10 
13  fRemote(0),
14  fOpenedCounter(0),
15  fDataMoverIndx(0)
16 {
17  memset(fDataMoverName, 0, sizeof(fDataMoverName));
18 }
19 
21 {
22  if (fRemote!=0) {
23  DOUT2("Close existing connection to RFIO data mover");
24  rfio_fclose((RFILE*) fRemote);
25  fRemote = 0;
26  }
27 }
28 
29 
30 dabc::FileInterface::Handle rfio::FileInterface::fopen(const char* fname, const char* mode, const char* opt)
31 {
32  // clear possible parameters
33  if (((opt==0) || (*opt==0)) && (fRemote==0)) {
34  return (Handle) rfio_fopen((char*)fname, (char*)mode);
35  }
36 
37  if (fRemote == 0) {
38  bool isany = false;
39 
40  dabc::Url url;
41  url.SetOptions(opt);
42  int rfioCopyMode = 1;
43  std::string rfioLustrePath = "/hera/hades/may14raw";
44  int rfioCopyFrac = 1;
45  int rfioMaxFile = 0;
46  int rfioPathConv = 0;
47  std::string rfioOptions = "wb";
48 
49  if (url.HasOption("rfioCopyMode")) {
50  rfioCopyMode = url.GetOptionInt("rfioCopyMode", rfioCopyMode);
51  isany = true;
52  }
53 
54  if (url.HasOption("rfioCopyFrac")) {
55  rfioCopyFrac = url.GetOptionInt("rfioCopyFrac", rfioCopyFrac);
56  isany = true;
57  }
58 
59  if (url.HasOption("rfioMaxFile")) {
60  rfioMaxFile = url.GetOptionInt("rfioMaxFile", rfioMaxFile);
61  isany = true;
62  }
63 
64  if (url.HasOption("rfioPathConv")) {
65  rfioPathConv = url.GetOptionInt("rfioPathConv", rfioPathConv);
66  isany = true;
67  }
68 
69  if (url.HasOption("rfioLustrePath")) {
70  rfioLustrePath = url.GetOptionStr("rfioLustrePath", rfioLustrePath);
71  isany = true;
72  }
73 
74  if (url.HasOption("rfioOptions")) {
75  rfioOptions = url.GetOptionStr("rfioOptions", rfioOptions);
76  isany = true;
77  }
78 
79  if (isany) {
80 
81  char rfioBase[1024];
82 
83  strncpy(rfioBase, fname, sizeof(rfioBase)-1);
84 
85  char* pcc = (char*) strrchr(rfioBase, ':');
86 
87  if (pcc) {
88  pcc++;
89  *pcc = 0; /* terminates after node name */
90  }
91 
92  strncpy(fDataMoverName, "", sizeof(fDataMoverName)-1);
93  fDataMoverIndx = 0;
94 
95  DOUT1("Try to connect to RFIO mover rfioBase=%s rfioOptions=%s rfioLustrePath=%s rfioCopyMode=%d rfioCopyFrac=%d rfioMaxFile=%d rfioPathConv=%d",
96  rfioBase, rfioOptions.c_str(), rfioLustrePath.c_str(), rfioCopyMode, rfioCopyFrac, rfioMaxFile, rfioPathConv);
97 
98  fRemote = rfio_fopen_gsidaq_dm(rfioBase, (char*) rfioOptions.c_str(),
99  fDataMoverName, &fDataMoverIndx,
100  rfioCopyMode, (char*) rfioLustrePath.c_str(),
101  rfioCopyFrac, rfioMaxFile, rfioPathConv);
102  if (fRemote == 0) {
103  EOUT("Fail to create connection with RFIO, using following arguments"
104  "rfioBase=%s rfioOptions=%s rfioLustrePath=%s rfioCopyMode=%d rfioCopyFrac=%d rfioMaxFile=%d rfioPathConv=%d",
105  rfioBase, rfioOptions.c_str(), rfioLustrePath.c_str(), rfioCopyMode, rfioCopyFrac, rfioMaxFile, rfioPathConv);
106  return 0;
107  }
108 
109  DOUT2("Successfully opened connection to datamover %d %s", fDataMoverIndx, fDataMoverName);
110  }
111  }
112 
113  if (fRemote==0)
114  return (Handle) rfio_fopen((char*)fname, (char*)mode);
115 
116  DOUT3("Calling rfio_fnewfile %s", fname);
117 
118  int rev = rfio_fnewfile((RFILE*)fRemote, (char*) fname);
119 
120  DOUT3("Did call rfio_fnewfile %s rev = %d", fname, rev);
121 
122  if (rev!=0) {
123  EOUT("Fail to create new RFIO file %s via existing datamover %d %s connection", fname, fDataMoverIndx, fDataMoverName);
124  return 0;
125  }
126 
127  fOpenedCounter++;
128 
129  if (fOpenedCounter > 100) EOUT("Too many (%d) files, opened via RFIO connection", fOpenedCounter);
130 
131  return (Handle) fRemote;
132 }
133 
134 
136 {
137  if (strcmp(parname, "RFIO")==0) return 8; // return RFIO version number
138  if (fRemote && strcmp(parname, "DataMoverIndx")==0) return fDataMoverIndx;
139  return 0;
140 }
141 
142 bool rfio::FileInterface::GetFileStrPar(Handle, const char* parname, char* sbuf, int sbuflen)
143 {
144  if (fRemote && strcmp(parname, "DataMoverName")==0)
145  if (strlen(fDataMoverName) < (unsigned) sbuflen) {
146  strncpy(sbuf, fDataMoverName, sbuflen);
147  return true;
148  }
149 
150  return false;
151 }
152 
153 
155 {
156  if ((fRemote!=0) && (f==fRemote)) {
157  rfio_fendfile((RFILE*) fRemote);
158  fOpenedCounter--;
159  if (fOpenedCounter < 0) EOUT("Too many close operations - counter (%d) is negative", fOpenedCounter);
160  return;
161  }
162 
163  if (fRemote!=0) EOUT("Get RFIO::fclose with unexpected argument when fRemote!=0 cnt %d", fOpenedCounter);
164 
165  if (f!=0) rfio_fclose((RFILE*)f);
166 }
167 
168 size_t rfio::FileInterface::fwrite(const void* ptr, size_t sz, size_t nmemb, Handle f)
169 {
170 
171  return ((f==0) || (ptr==0) || (sz==0)) ? 0 : rfio_fwrite((const char*)ptr, sz, nmemb, (RFILE*) f) / sz;
172 }
173 
174 size_t rfio::FileInterface::fread(void* ptr, size_t sz, size_t nmemb, Handle f)
175 {
176  return ((f==0) || (ptr==0) || (sz==0)) ? 0 : rfio_fread((char*) ptr, sz, nmemb, (RFILE*) f) / sz;
177 }
178 
179 bool rfio::FileInterface::fseek(Handle f, long int offset, bool relative)
180 {
181  if (f==0) return false;
182 
183 #ifdef ORIGIN_ADSM
184  int fileid = -1;
185  printf("rfio::FileInterface::fseek not working with original version of ADSM library\n");
186 #else
187  int fileid = rfio_ffileid((RFILE*)f);
188 #endif
189 
190  if (fileid<0) return false;
191 
192  return rfio_lseek(fileid, offset, relative ? SEEK_CUR : SEEK_SET) >= 0;
193 }
194 
195 
197 {
198  return f==0 ? false : (rfio_fendfile((RFILE*)f) > 0);
199 }
200 
202 {
203  return true;
204 
205  // return f==0 ? false : ::fflush((FILE*)f)==0;
206 }
207 
208 dabc::Object* rfio::FileInterface::fmatch(const char* fmask, bool select_files)
209 {
210  return 0;
211 }
void * Handle
File handle descriptor.
Definition: BinaryFile.h:37
Base class for most of the DABC classes.
Definition: Object.h:116
Uniform Resource Locator interpreter.
Definition: Url.h:33
std::string GetOptionStr(const std::string &optname, const std::string &dflt="") const
Definition: Url.cxx:281
bool HasOption(const std::string &optname) const
Definition: Url.h:70
int GetOptionInt(const std::string &optname, int dflt=0) const
Definition: Url.cxx:290
void SetOptions(const std::string &opt)
Method allows to set URL options directly to be able use all Get methods.
Definition: Url.cxx:102
char fDataMoverName[64]
obtained data mover index
Definition: FileInterface.h:15
virtual void fclose(Handle f)
virtual bool fseek(Handle f, long int offset, bool realtive=true)
virtual bool feof(Handle f)
virtual bool GetFileStrPar(Handle h, const char *parname, char *sbuf, int sbuflen)
Method returns file-specific string parameter.
virtual size_t fread(void *ptr, size_t sz, size_t nmemb, Handle f)
virtual int GetFileIntPar(Handle h, const char *parname)
Method returns file-specific int parameter.
FileInterface()
obtained data mover name
virtual size_t fwrite(const void *ptr, size_t sz, size_t nmemb, Handle f)
virtual Handle fopen(const char *fname, const char *mode, const char *opt=0)
virtual dabc::Object * fmatch(const char *fmask, bool select_files=true)
Produce list of files, object must be explicitly destroyed with ref.Destroy call One could decide if ...
virtual bool fflush(Handle f)
#define DOUT2(args ...)
Definition: logging.h:170
#define DOUT3(args ...)
Definition: logging.h:176
#define EOUT(args ...)
Definition: logging.h:150
#define DOUT1(args ...)
Definition: logging.h:162
Event manipulation API.
Definition: api.h:23
RFILE * rfio_fopen(char *pcFile, char *pcOptions)
Definition: rawapin.c:4623
size_t rfio_fwrite(const char *pcBuffer, size_t iSize, size_t iItems, RFILE *pRemFile)
Definition: rawapin.c:4966
int rfio_lseek(int iFileId, int ilocOffset, int ilocSeekMode)
Definition: rawapin.c:4519
int rfio_fclose(RFILE *pRemFile)
Definition: rawapin.c:5035
RFILE * rfio_fopen_gsidaq_dm(char *pcFile, char *pcOptions, char *pcDataMover, int *piDataMover, int iCopyMode, char *pcCopyPath, int iCopyFraction, int iMaxFile, int iPathConvention)
Definition: rawapin.c:4663
int rfio_fendfile(RFILE *pRemFile)
Definition: rawapin.c:5006
int rfio_fnewfile(RFILE *pRemFile, char *pcFile)
Definition: rawapin.c:4859
size_t rfio_fread(char *pcBuffer, size_t iSize, size_t iItems, RFILE *pRemFile)
Definition: rawapin.c:4928
int rfio_ffileid(RFILE *fRemoteFile)
Definition: rawapin.c:4583
#define RFILE
Definition: rawapin.h:71