CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_commit.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
6 #include "cmd_commit.h"
7 
8 #include <errno.h>
9 
10 #include <ctime>
11 #include <string>
12 #include <vector>
13 
14 #include "publish/cmd_util.h"
15 #include "publish/except.h"
16 #include "publish/repository.h"
17 #include "publish/settings.h"
18 #include "util/logging.h"
19 #include "util/posix.h"
20 #include "util/string.h"
21 #include "whitelist.h"
22 
23 namespace publish {
24 
25 int CmdCommit::Main(const Options &options) {
26  // Repository name and lease path are submitted as a single argument
27  // for historical reasons
28  std::string fqrn;
29 
30  if (!options.plain_args().empty()) {
31  std::vector<std::string> tokens =
32  SplitStringBounded(2, options.plain_args()[0].value_str, '/');
33  fqrn = tokens[0];
34  }
35 
36  SettingsBuilder builder;
37  std::string session_dir = Env::GetEnterSessionDir();
38  builder.SetConfigPath(session_dir);
39 
41  try {
42  settings = builder.CreateSettingsPublisher(fqrn, true /* needs_managed */);
43  } catch (const EPublish &e) {
45  LogCvmfs(kLogCvmfs, kLogStderr | kLogSyslogErr, "CernVM-FS error: %s",
46  e.msg().c_str());
47  return 1;
48  }
49  throw;
50  }
51 
52  if (!SwitchCredentials(settings->owner_uid(), settings->owner_gid(),
53  false /* temporarily */))
54  {
55  throw EPublish("No write permission to repository");
56  }
57 
58  FileSystemInfo fs_info = GetFileSystemInfo("/cvmfs");
59  if (fs_info.type == kFsTypeAutofs)
60  throw EPublish("Autofs on /cvmfs has to be disabled");
61 
62  UniquePtr<Publisher> publisher;
63  try {
64  publisher = new Publisher(*settings);
65  if (publisher->whitelist()->IsExpired()) {
66  throw EPublish("Repository whitelist for $name is expired",
68  }
69  } catch (const EPublish &e) {
72  {
73  LogCvmfs(kLogCvmfs, kLogStderr | kLogSyslogErr, "%s", e.msg().c_str());
74  return EINVAL;
75  }
76  }
77 
78  double whitelist_valid_s =
79  difftime(publisher->whitelist()->expires(), time(NULL));
80  if (whitelist_valid_s < (12 * 60 * 60)) {
82  "Warning: Repository whitelist stays valid for less than 12 hours!");
83  }
84 
85  publisher->Sync();
86  SafeWriteToFile("commit", session_dir + "/shellaction.marker", 0600);
87  LogCvmfs(kLogCvmfs, kLogStdout, "Changes saved!");
88  publisher->ExitShell();
89 
90  return 0;
91 }
92 
93 } // namespace publish
SettingsPublisher * CreateSettingsPublisher(const std::string &ident, bool needs_managed=false)
Definition: settings.cc:602
virtual int Main(const Options &options)
Definition: cmd_commit.cc:25
EFileSystemTypes type
Definition: posix.h:55
std::string msg() const
Definition: except.h:46
const SettingsPublisher & settings() const
Definition: repository.h:317
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
Definition: posix.cc:2158
void SetConfigPath(const std::string &config_path)
Definition: settings.h:535
const std::vector< Argument > & plain_args() const
Definition: command.h:137
FileSystemInfo GetFileSystemInfo(const std::string &path)
Definition: posix.cc:180
vector< string > SplitStringBounded(unsigned max_chunks, const string &str, char delim)
Definition: string.cc:294
EFailures failure() const
Definition: except.h:45
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
Definition: posix.cc:772
Publisher(const SettingsPublisher &settings, const bool exists=true)
Definition: repository.cc:594
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528