CernVM-FS  2.10.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 "logging.h"
15 #include "publish/cmd_util.h"
16 #include "publish/except.h"
17 #include "publish/repository.h"
18 #include "publish/settings.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  SplitString(options.plain_args()[0].value_str, '/', 2);
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
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
SettingsPublisher * CreateSettingsPublisher(const std::string &ident, bool needs_managed=false)
Definition: settings.cc:590
vector< string > SplitString(const string &str, const char delim, const unsigned max_chunks)
Definition: string.cc:288
virtual int Main(const Options &options)
Definition: cmd_commit.cc:25
EFileSystemTypes type
Definition: posix.h:51
std::string msg() const
Definition: except.h:46
const SettingsPublisher & settings() const
Definition: repository.h:316
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
Definition: posix.cc:2026
void SetConfigPath(const std::string &config_path)
Definition: settings.h:526
const std::vector< Argument > & plain_args() const
Definition: command.h:137
FileSystemInfo GetFileSystemInfo(const std::string &path)
Definition: posix.cc:215
EFailures failure() const
Definition: except.h:45
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
Definition: posix.cc:785
Publisher(const SettingsPublisher &settings, const bool exists=true)
Definition: repository.cc:604