CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes
RemoteFile Class Reference

#include <RemoteFile.h>

Inheritance diagram for RemoteFile:
File IOChannel Storage IOInput IOOutput IOInput IOOutput

Public Member Functions

 ~RemoteFile (void) override
 

Static Public Member Functions

static std::unique_ptr< Storageget (int localfd, const std::string &name, char **cmd, int mode)
 
static int local (const std::string &tmpdir, std::string &temp)
 

Protected Member Functions

void abort (void) override
 
void close (void) override
 
- Protected Member Functions inherited from File
virtual void attach (IOFD fd)
 
virtual void create (const char *name, bool exclusive=false, int perms=0666)
 
virtual void create (const std::string &name, bool exclusive=false, int perms=0666)
 
 File (void)
 
 File (IOFD fd, bool autoclose=true)
 
 File (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 
 File (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 
void flush (void) override
 
virtual void open (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 
virtual void open (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 
IOOffset position (IOOffset offset, Relative whence=SET) override
 
bool prefetch (const IOPosBuffer *what, IOSize n) override
 
IOSize read (void *into, IOSize n) override
 
IOSize read (void *into, IOSize n, IOOffset pos) override
 
IOSize readv (IOBuffer *into, IOSize length) override
 
void resize (IOOffset size) override
 
virtual void setAutoClose (bool closeit)
 
IOOffset size (void) const override
 
IOSize write (const void *from, IOSize n) override
 
IOSize write (const void *from, IOSize n, IOOffset pos) override
 
IOSize writev (const IOBuffer *from, IOSize length) override
 
 ~File (void) override
 
- Protected Member Functions inherited from IOChannel
bool sysclose (IOFD fd, int *error=nullptr)
 
virtual IOFD fd (void) const
 
virtual void fd (IOFD value)
 
 IOChannel (IOFD fd=-1)
 
virtual bool isBlocking (void) const
 
IOSize read (void *into, IOSize n) override
 
IOSize readv (IOBuffer *into, IOSize buffers) override
 
virtual void setBlocking (bool value)
 
IOSize write (const void *from, IOSize n) override
 
IOSize writev (const IOBuffer *from, IOSize buffers) override
 
 ~IOChannel (void) override
 
- Protected Member Functions inherited from IOInput
int read (void)
 
IOSize read (IOBuffer into)
 
IOSize xread (IOBuffer into)
 
IOSize xread (void *into, IOSize n)
 
IOSize xreadv (IOBuffer *into, IOSize buffers)
 
virtual ~IOInput (void)
 Destruct the stream. A no-op. More...
 
- Protected Member Functions inherited from IOOutput
IOSize write (unsigned char byte)
 
IOSize write (IOBuffer from)
 
IOSize xwrite (const void *from, IOSize n)
 
IOSize xwrite (IOBuffer from)
 
IOSize xwritev (const IOBuffer *from, IOSize buffers)
 
virtual ~IOOutput (void)
 Destruct the stream. A no-op. More...
 
- Protected Member Functions inherited from Storage
virtual bool eof (void) const
 
virtual IOOffset position (void) const
 
IOSize read (IOBuffer into, IOOffset pos)
 
virtual IOSize readv (IOPosBuffer *into, IOSize buffers)
 
virtual void rewind (void)
 
 Storage (void)
 
IOSize write (IOBuffer from, IOOffset pos)
 
virtual IOSize writev (const IOPosBuffer *from, IOSize buffers)
 
 ~Storage (void) override
 

Private Member Functions

 RemoteFile (IOFD fd, const std::string &name)
 
void remove (void)
 

Private Attributes

std::string name_
 

Detailed Description

Definition at line 8 of file RemoteFile.h.

Constructor & Destructor Documentation

RemoteFile::~RemoteFile ( void  )
inlineoverride
RemoteFile::RemoteFile ( IOFD  fd,
const std::string &  name 
)
private

Definition at line 34 of file RemoteFile.cc.

Referenced by get(), and ~RemoteFile().

34 : File(fd), name_(name) {}
File(void)
Definition: File.cc:57
std::string name_
Definition: RemoteFile.h:22
virtual IOFD fd(void) const
Definition: IOChannel.cc:69

Member Function Documentation

void RemoteFile::abort ( void  )
overrideprotectedvirtual

Close the file and ignore all errors.

Reimplemented from File.

Definition at line 43 of file RemoteFile.cc.

References File::abort().

Referenced by ~RemoteFile().

43  {
44  remove();
45  File::abort();
46 }
virtual void abort(void)
Definition: File.cc:254
void RemoteFile::close ( void  )
overrideprotectedvirtual
std::unique_ptr< Storage > RemoteFile::get ( int  localfd,
const std::string &  name,
char **  cmd,
int  mode 
)
static

Definition at line 78 of file RemoteFile.cc.

References close(), join(), IOFlags::OpenCreate, IOFlags::OpenWrite, RemoteFile(), and throwStorageError().

Referenced by Options.Options::__getitem__(), betterConfigParser.BetterConfigParser::__updateDict(), betterConfigParser.BetterConfigParser::getCompares(), betterConfigParser.BetterConfigParser::getGeneral(), betterConfigParser.BetterConfigParser::getResultingSection(), GsiFTPStorageMaker::open(), and HttpStorageMaker::open().

78  {
79  // FIXME: On write, create a temporary local file open for write;
80  // on close, trigger transfer to destination. If opening existing
81  // file for write, may need to first download.
83 
84  pid_t pid = -1;
85  int rc = posix_spawnp(&pid, cmd[0], nullptr, nullptr, cmd, environ);
86 
87  if (rc == -1) {
88  int errsave = errno;
89  ::close(localfd);
90  unlink(name.c_str());
91  throwStorageError("RemoteFile", "Calling RemoteFile::get()", "posix_spawnp()", errsave);
92  }
93 
94  pid_t rcpid;
95  do
96  rcpid = waitpid(pid, &rc, 0);
97  while (rcpid == (pid_t)-1 && errno == EINTR);
98 
99  if (rcpid == (pid_t)-1) {
100  int errsave = errno;
101  ::close(localfd);
102  unlink(name.c_str());
103  throwStorageError("RemoteFile", "Calling RemoteFile::get()", "waitpid()", errsave);
104  }
105 
106  if (WIFEXITED(rc) && WEXITSTATUS(rc) == 0)
107  return std::unique_ptr<Storage>(static_cast<Storage *>(new RemoteFile(localfd, name)));
108  else {
109  ::close(localfd);
110  unlink(name.c_str());
111  cms::Exception ex("RemoteFile");
112  ex << "'" << join(cmd) << "'"
113  << (WIFEXITED(rc)
114  ? " exited with exit code "
115  : WIFSIGNALED(rc) ? " died from signal " : " died for an obscure unknown reason with exit status ")
116  << (WIFEXITED(rc) ? WEXITSTATUS(rc) : WIFSIGNALED(rc) ? WTERMSIG(rc) : rc);
117  ex.addContext("Calling RemoteFile::get()");
118  throw ex;
119  }
120 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
RemoteFile(IOFD fd, const std::string &name)
Definition: RemoteFile.cc:34
void close(void) override
Definition: RemoteFile.cc:38
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
list cmd
Definition: mps_setup.py:244
int RemoteFile::local ( const std::string &  tmpdir,
std::string &  temp 
)
static

Definition at line 48 of file RemoteFile.cc.

References IOChannel::fd(), and throwStorageError().

Referenced by GsiFTPStorageMaker::open(), HttpStorageMaker::open(), and ~RemoteFile().

48  {
49  // Download temporary files to the current directory by default.
50  // This is better for grid jobs as the current directory is
51  // likely to have more space, and is more optimised for
52  // large files, and is cleaned up after the job.
53  if (tmpdir.empty() || tmpdir == ".") {
54  size_t len = pathconf(".", _PC_PATH_MAX);
55  char *buf = (char *)malloc(len);
56  getcwd(buf, len);
57 
58  temp.reserve(len + 32);
59  temp = buf;
60  free(buf);
61  } else {
62  temp.reserve(tmpdir.size() + 32);
63  temp = tmpdir;
64  }
65  if (temp[temp.size() - 1] != '/')
66  temp += '/';
67 
68  temp += "storage-factory-local-XXXXXX";
69  temp.c_str(); // null terminate for mkstemp
70 
71  int fd = mkstemp(&temp[0]);
72  if (fd == -1)
73  throwStorageError("RemoteFile", "Calling RemoteFile::local()", "mkstemp()", errno);
74 
75  return fd;
76 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
void RemoteFile::remove ( void  )
private

Definition at line 36 of file RemoteFile.cc.

References name_.

36 { unlink(name_.c_str()); }
std::string name_
Definition: RemoteFile.h:22

Member Data Documentation

std::string RemoteFile::name_
private

Definition at line 22 of file RemoteFile.h.

Referenced by remove().