11 dabc::FileInterface(), fSession(0), fMaxFilesPerSession(10), fSessionConnectRetries(5), fIsClosing(false), fSessionFileCount(0), fUseDaysubfolders(false)
13 ,fUseFileSystemDemon(false), fServernameFSD(
"lxcopytool01.gsi.de"),fPortFSD(7625),fSessionFSD(0)
16 DOUT3(
"tsm::FileInterface::FileInterface() ctor starts...");
17 api_msg_set_level(API_MSG_ERROR);
18 DOUT0(
"tsm::FileInterface::FileInterface() ctor set api message level to %d",
20 tsm_init (DSM_MULTITHREAD);
21 DOUT3(
"tsm::FileInterface::FileInterface() ctor leaving...");
26 DOUT3(
"ltsm::FileInterface::DTOR ... ");
28 tsm_cleanup (DSM_MULTITHREAD);
32 const char* mode,
const char* opt)
35 DOUT0(
"ltsm::FileInterface::fopen with options %s",opt);
44 fMaxFilesPerSession = url.
GetOptionInt(
"ltsmMaxSessionFiles", fMaxFilesPerSession);
45 DOUT0(
"tsm::FileInterface::fopen uses %d max session files from url options.",fMaxFilesPerSession);
49 DOUT0(
"tsm::FileInterface::fopen uses %d max session files from DEFAULTS.",fMaxFilesPerSession);
54 fUseDaysubfolders = url.
GetOptionBool(
"ltsmDaysubfolders", fUseDaysubfolders);
55 DOUT0(
"tsm::FileInterface::fopen uses day prefix in path: %d from url options.",fUseDaysubfolders);
59 DOUT0(
"tsm::FileInterface::fopen uses uses day prefix in path: %d from DEFAULTS.",fUseDaysubfolders);
65 if(fSessionFileCount >= fMaxFilesPerSession)
71 std::string fileName=fname;
79 gettimeofday(&tv, NULL);
80 strftime(buf, 128,
"%y/%j/", localtime_r(&tv.tv_sec, &tm_res));
81 DOUT0(
"ltsm::FileInterface uses year day path %s",buf);
82 std::string insertpath(buf);
84 size_t slash = fileName.rfind(
"/");
85 if (slash == std::string::npos)
87 fileName.insert(slash+1,insertpath);
89 DOUT0(
"ltsm::FileInterface uses new filename %s",fileName.c_str());
94 if (fUseFileSystemDemon)
98 if(!OpenTSMSession(opt))
return 0;
106 if(!OpenTSMSession(opt))
return 0;
113 "This file was created by DABC ltsm interface at %s",
119 fDescription = url.
GetOptionStr(
"ltsmDescription", fDescription);
122 if (strstr(mode,
"w") != 0)
126 if (fUseFileSystemDemon)
128 rc = fsd_fopen(fFsname.c_str(), (
char*) fileName.c_str(),
129 (
char*) fDescription.c_str(), fSessionFSD);
133 "Fail to open LTSM file for writing to FSD (returncode:%d), using following arguments"
134 "File=%s, FSD Servername=%s, Node=%s, Pass=%s, Owner=%s,Fsname=%s Description=%s",rc,
135 fileName.c_str(), fServernameFSD.c_str(), fNode.c_str(),
136 fPassword.c_str(), fOwner.c_str(), fFsname.c_str(),
137 fDescription.c_str());
143 fCurrentFile = fileName.c_str();
145 DOUT0(
"Opened LTSM file (session count %d) for writing to FSD: "
146 "File=%s, FSD Servername=%s, Description=%s", fSessionFileCount, fname,
147 fServernameFSD.c_str(), fDescription.c_str());
153 rc = tsm_fopen(fFsname.c_str(), (
char*) fileName.c_str(),
154 (
char*) fDescription.c_str(), fSession);
158 "Fail to open LTSM file for writing (returncode:%d), using following arguments"
159 "File=%s, Servername=%s, Node=%s, Pass=%s, Owner=%s,Fsname=%s Description=%s",rc,
160 fileName.c_str(), fServername.c_str(), fNode.c_str(),
161 fPassword.c_str(), fOwner.c_str(), fFsname.c_str(),
162 fDescription.c_str());
168 fCurrentFile = fileName.c_str();
170 DOUT0(
"Opened LTSM file (session count %d) for writing: "
171 "File=%s, Servername=%s, Description=%s", fSessionFileCount, fileName.c_str(),
172 fServername.c_str(), fDescription.c_str());
173 return fSession->tsm_file;
177 else if (strstr(mode,
"r") != 0)
179 EOUT(
"tsm_fread is not yet supported!");
189 if (strcmp(parname,
"RFIO") == 0)
191 if (strcmp(parname,
"DataMoverIndx") == 0)
193 std::string suffix = fNode.substr(fNode.size() - 2);
194 return std::stoi(suffix);
204 if (strcmp(parname,
"DataMoverName") == 0)
206 strncpy(sbuf, fNode.c_str(), sbuflen);
210 if (strcmp(parname,
"RealFileName")==0)
212 strncpy(sbuf,fCurrentFile.c_str(),sbuflen);
220 DOUT3(
"ltsm::FileInterface::fclose with handle 0x%x...", f);
226 "ltsm::FileInterface::fclose is called during closing - ignored!");
233 if (fUseFileSystemDemon)
235 struct fsd_session_t* theHandleFSD = (
struct fsd_session_t*) f;
236 if (theHandleFSD != fSessionFSD)
240 "Inconsistent file handles (0x%x != 0x%x) when closing LTSM file: "
241 "File=%s, FSD Servername=%s, Node=%s, Fsname=%s .... try to close most recent file in session",
242 theHandleFSD, fSessionFSD, fCurrentFile.c_str(),
243 fServernameFSD.c_str(), fNode.c_str(), fFsname.c_str());
245 rc = fsd_fclose(fSessionFSD);
248 EOUT(
"Failedto close LTSM file on FSD server: "
249 "File=%s, FSD Servername=%s, Node=%s, Fsname=%s",
250 fCurrentFile.c_str(), fServernameFSD.c_str(), fNode.c_str(),
258 struct tsm_file_t* theHandle = (
struct tsm_file_t*) f;
259 if (theHandle != fSession->tsm_file)
263 "Inconsistent file handles (0x%x != 0x%x) when closing LTSM file: "
264 "File=%s, Servername=%s, Node=%s, Fsname=%s .... try to close most recent file in session",
265 theHandle, fSession->tsm_file, fCurrentFile.c_str(),
266 fServername.c_str(), fNode.c_str(), fFsname.c_str());
270 rc = tsm_fclose(fSession);
274 EOUT(
"Failedto close LTSM file: "
275 "File=%s, Servername=%s, Node=%s, Fsname=%s",
276 fCurrentFile.c_str(), fServername.c_str(), fNode.c_str(),
281 DOUT0(
"ltsm::FileInterface has closed file %s", fCurrentFile.c_str());
283 DOUT3(
"ltsm::FileInterface::fclose END ");
290 if ((f == 0) || (ptr == 0) || (sz == 0))
296 "ltsm::FileInterface::fwrite is called during closing - ignored!");
303 if (fUseFileSystemDemon)
305 struct fsd_session_t* theHandleFSD = (
struct fsd_session_t*) f;
306 if (theHandleFSD != fSessionFSD)
310 "Inconsistent file handles (0x%x != 0x%x) when writing to LTSM: "
311 "File=%s, FSD Servername=%s, Node=%s, Fsname=%s .... something is wrong!",
312 theHandleFSD, fSessionFSD, fCurrentFile.c_str(),
313 fServernameFSD.c_str(), fNode.c_str(), fFsname.c_str());
316 rc = fsd_fwrite(ptr, sz, nmemb, fSessionFSD);
321 struct tsm_file_t* theHandle = (
struct tsm_file_t*) f;
322 if (theHandle != fSession->tsm_file)
326 "Inconsistent tsm_file_t handles (0x%x != 0x%x) when writing to LTSM: "
327 "File=%s, Servername=%s, Node=%s, Fsname=%s .... something is wrong!",
328 theHandle, fSession->tsm_file, fCurrentFile.c_str(),
329 fServername.c_str(), fNode.c_str(), fFsname.c_str());
332 rc = tsm_fwrite(ptr, sz, nmemb, fSession);
336 EOUT(
"tsm_fwrite failed, handle:0x%x, size:%d, nmemb:%d, rc=%d", f, sz, nmemb,rc);
340 if (rc !=
int(sz * nmemb))
342 EOUT(
"tsm_fwrite size mismatch, wrote %d bytes from requested %d bytes",
351 EOUT(
"ltsm::FileInterface::fread is not yet supported!");
392 DOUT3(
"ltsm::FileInterface::OpenTSMSession ... ");
396 fCurrentFile =
"none";
397 fServername =
"lxltsm01-tsm-server";
398 fNode =
"LTSM_TEST01";
399 fPassword =
"LTSM_TEST01";
401 fFsname = DEFAULT_FSNAME;
403 DOUT1(
"ltsm::FileInterface::fOpenTSMSession before options with options %s", opt);
407 fServername = url.
GetOptionStr(
"ltsmServer", fServername);
426 if (url.
HasOption(
"ltsmSessionConnectRetries"))
428 fMaxFilesPerSession = url.
GetOptionInt(
"ltsmMaxSessionFiles", fSessionConnectRetries);
429 DOUT0(
"tsm::FileInterface::OpenTSMSession uses %d session connect retries from url options.",fSessionConnectRetries);
433 DOUT0(
"tsm::FileInterface::OpenTSMSession uses %d session connect retries from DEFAULTS.", fSessionConnectRetries);
439 fUseFileSystemDemon = url.
GetOptionBool(
"ltsmUseFSD", fUseFileSystemDemon);
440 DOUT0(
"tsm::FileInterface::OpenTSMSession will use %s from url options.",fUseFileSystemDemon ?
"file system demon" :
"direct TSM connection");
444 DOUT0(
"tsm::FileInterface::OpenTSMSession will use %s from defaults.",fUseFileSystemDemon ?
"file system demon" :
"direct TSM connection");
447 if (fUseFileSystemDemon)
451 fServernameFSD = url.
GetOptionStr(
"ltsmFSDServerName", fServernameFSD);
452 DOUT0(
"tsm::FileInterface::OpenTSMSession with FSD server %s from url options.",fServernameFSD.c_str());
456 DOUT0(
"tsm::FileInterface::OpenTSMSession with FSD server %s from defaults.",fServernameFSD.c_str());
460 fPortFSD = url.
GetOptionInt(
"ltsmFSDServerPort", fPortFSD);
461 DOUT0(
"tsm::FileInterface::OpenTSMSession with FSD server port %d from url options.",fPortFSD);
465 DOUT0(
"tsm::FileInterface::OpenTSMSession with FSD server port %d from defaults.",fPortFSD);
472 "Prepare open LTSM file for writing - "
473 "Servername=%s, Node=%s, Pass=%s, Owner=%s,Fsname=%s Description=%s",
474 fServername.c_str(), fNode.c_str(), fPassword.c_str(),
475 fOwner.c_str(), fFsname.c_str(), fDescription.c_str());
479 struct fsd_login_t fsdlogin;
481 strncpy(fsdlogin.node, fNode.c_str(), DSM_MAX_NODE_LENGTH);
482 strncpy(fsdlogin.password, fPassword.c_str(), DSM_MAX_VERIFIER_LENGTH);
483 strncpy(fsdlogin.hostname, fServernameFSD.c_str(), DSM_MAX_NODE_LENGTH);
484 fsdlogin.port=fPortFSD;
486 fSessionFSD = (
struct fsd_session_t*) malloc(
sizeof(
struct fsd_session_t));
488 struct login_t tsmlogin;
489 login_init(&tsmlogin, fServername.c_str(), fNode.c_str(),
490 fPassword.c_str(), fOwner.c_str(), LINUX_PLATFORM,
491 fFsname.c_str(), DEFAULT_FSTYPE);
492 fSession = (
struct session_t*) malloc(
sizeof(
struct session_t));
494 EOUT(
"Memory allocation error");
500 while (connectcount++ <fSessionConnectRetries)
503 if (fUseFileSystemDemon)
505 memset(fSessionFSD, 0,
sizeof(
struct fsd_session_t));
506 rc = fsd_fconnect(&fsdlogin, fSessionFSD);
511 memset(fSession, 0,
sizeof(
struct session_t));
512 rc = tsm_fconnect(&tsmlogin, fSession);
520 EOUT(
"Fail to connect LTSM session (returncode:%d) using following arguments"
521 "Servername=%s, Node=%s, Pass=%s, Owner=%s,Fsname=%s , retry again %d time...",rc,
522 fServername.c_str(), fNode.c_str(), fPassword.c_str(),
523 fOwner.c_str(), fFsname.c_str(), connectcount);
530 EOUT(
"Finally FAILED to connect LTSM session (returncode%d))after %d retries!! - using following arguments"
531 "Servername=%s, Node=%s, Pass=%s, Owner=%s,Fsname=%s", rc, connectcount,
532 fServername.c_str(), fNode.c_str(), fPassword.c_str(),
533 fOwner.c_str(), fFsname.c_str());
535 if (fUseFileSystemDemon)
549 DOUT0(
"Successfully conncted LTSM session with parameter: "
550 "Servername=%s, Node=%s, Pass=%s, Owner=%s,Fsname=%s",
551 fServername.c_str(), fNode.c_str(), fPassword.c_str(),
552 fOwner.c_str(), fFsname.c_str());
555 if (fUseFileSystemDemon)
556 DOUT0(
"Using FSD at server %s ,port %d", fServernameFSD.c_str(), fPortFSD);
566 if (fUseFileSystemDemon)
568 if(fSessionFSD==0)
return false;
569 fsd_fdisconnect(fSessionFSD);
576 if(fSession==0)
return false;
577 tsm_fdisconnect(fSession);
Class for holding GMT time with precision of nanoseconds.
std::string AsJSString(int ndecimal=3) const
convert string into sec.frac format, can be interpret directly in JavaScript ISO 8601 standard is use...
void * Handle
File handle descriptor.
void Sleep(double tmout, const char *prefix=0)
Sleep for specified time, keep thread event loop running See Manager::Sleep() method for more details...
Base class for most of the DABC classes.
Uniform Resource Locator interpreter.
std::string GetOptionStr(const std::string &optname, const std::string &dflt="") const
bool GetOptionBool(const std::string &optname, bool dflt=false) const
bool HasOption(const std::string &optname) const
int GetOptionInt(const std::string &optname, int dflt=0) const
void SetOptions(const std::string &opt)
Method allows to set URL options directly to be able use all Get methods.
virtual bool fflush(Handle f)
bool CloseTSMSession()
Shut down current TSM session.
virtual int GetFileIntPar(Handle h, const char *parname)
Method returns file-specific int parameter.
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 void fclose(Handle f)
virtual bool feof(Handle f)
virtual bool fseek(Handle f, long int offset, bool realtive=true)
bool OpenTSMSession(const char *options)
Re-open session with parameters specified in dabc options string.
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 size_t fwrite(const void *ptr, size_t sz, size_t nmemb, Handle f)
std::string format(const char *fmt,...)