Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#include "swissknife_assistant.h" |
6 |
|
|
|
7 |
|
|
#include <unistd.h> |
8 |
|
|
|
9 |
|
|
#include <cassert> |
10 |
|
|
#include <cstdlib> |
11 |
|
|
|
12 |
|
|
#include "catalog.h" |
13 |
|
|
#include "catalog_rw.h" |
14 |
|
|
#include "history.h" |
15 |
|
|
#include "history_sqlite.h" |
16 |
|
|
#include "manifest.h" |
17 |
|
|
#include "network/download.h" |
18 |
|
|
#include "util/logging.h" |
19 |
|
|
#include "util/posix.h" |
20 |
|
|
|
21 |
|
|
using namespace std; // NOLINT |
22 |
|
|
|
23 |
|
|
namespace swissknife { |
24 |
|
|
|
25 |
|
✗ |
catalog::Catalog *Assistant::GetCatalog( |
26 |
|
|
const shash::Any &catalog_hash, |
27 |
|
|
OpenMode open_mode) |
28 |
|
|
{ |
29 |
|
✗ |
assert(shash::kSuffixCatalog == catalog_hash.suffix); |
30 |
|
✗ |
string local_path = CreateTempPath(tmp_dir_ + "/catalog", 0600); |
31 |
|
✗ |
assert(!local_path.empty()); |
32 |
|
|
|
33 |
|
✗ |
if (!FetchObject(catalog_hash, local_path)) { |
34 |
|
✗ |
return NULL; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
✗ |
const std::string catalog_root_path = ""; |
38 |
|
|
catalog::Catalog *catalog; |
39 |
|
✗ |
switch (open_mode) { |
40 |
|
✗ |
case kOpenReadWrite: |
41 |
|
✗ |
catalog = catalog::WritableCatalog::AttachFreely(catalog_root_path, |
42 |
|
|
local_path, |
43 |
|
|
catalog_hash); |
44 |
|
✗ |
break; |
45 |
|
✗ |
case kOpenReadOnly: |
46 |
|
✗ |
catalog = catalog::Catalog::AttachFreely(catalog_root_path, |
47 |
|
|
local_path, |
48 |
|
|
catalog_hash); |
49 |
|
✗ |
break; |
50 |
|
✗ |
default: |
51 |
|
✗ |
abort(); |
52 |
|
|
} |
53 |
|
✗ |
assert(catalog != NULL); |
54 |
|
✗ |
catalog->TakeDatabaseFileOwnership(); |
55 |
|
✗ |
return catalog; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
|
59 |
|
✗ |
history::History *Assistant::GetHistory(OpenMode open_mode) { |
60 |
|
✗ |
const shash::Any history_hash = manifest_->history(); |
61 |
|
|
history::History *history; |
62 |
|
|
|
63 |
|
✗ |
string local_path = CreateTempPath(tmp_dir_ + "/history", 0600); |
64 |
|
✗ |
assert(!local_path.empty()); |
65 |
|
|
|
66 |
|
✗ |
if (history_hash.IsNull()) { |
67 |
|
✗ |
history = history::SqliteHistory::Create(local_path, |
68 |
|
✗ |
manifest_->repository_name()); |
69 |
|
✗ |
if (NULL == history) { |
70 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "failed to create history database"); |
71 |
|
✗ |
return NULL; |
72 |
|
|
} |
73 |
|
✗ |
return history; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
✗ |
if (!FetchObject(history_hash, local_path)) |
77 |
|
✗ |
return NULL; |
78 |
|
|
|
79 |
|
✗ |
switch (open_mode) { |
80 |
|
✗ |
case kOpenReadWrite: |
81 |
|
✗ |
history = history::SqliteHistory::OpenWritable(local_path); |
82 |
|
✗ |
break; |
83 |
|
✗ |
case kOpenReadOnly: |
84 |
|
✗ |
history = history::SqliteHistory::Open(local_path); |
85 |
|
✗ |
break; |
86 |
|
✗ |
default: |
87 |
|
✗ |
abort(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
✗ |
if (history == NULL) { |
91 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "failed to open history database (%s)", |
92 |
|
|
local_path.c_str()); |
93 |
|
✗ |
unlink(local_path.c_str()); |
94 |
|
✗ |
return NULL; |
95 |
|
|
} |
96 |
|
✗ |
assert(history->fqrn() == manifest_->repository_name()); |
97 |
|
✗ |
history->TakeDatabaseFileOwnership(); |
98 |
|
✗ |
return history; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
|
102 |
|
✗ |
bool Assistant::FetchObject(const shash::Any &id, const string &local_path) { |
103 |
|
✗ |
assert(!id.IsNull()); |
104 |
|
|
|
105 |
|
|
download::Failures dl_retval; |
106 |
|
✗ |
const std::string url = repository_url_ + "/data/" + id.MakePath(); |
107 |
|
|
|
108 |
|
✗ |
cvmfs::PathSink pathsink(local_path); |
109 |
|
✗ |
download::JobInfo download_info(&url, true, false, &id, &pathsink); |
110 |
|
✗ |
dl_retval = download_mgr_->Fetch(&download_info); |
111 |
|
|
|
112 |
|
✗ |
if (dl_retval != download::kFailOk) { |
113 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "failed to download object '%s' (%d - %s)", |
114 |
|
✗ |
id.ToStringWithSuffix().c_str(), |
115 |
|
|
dl_retval, download::Code2Ascii(dl_retval)); |
116 |
|
✗ |
return false; |
117 |
|
|
} |
118 |
|
✗ |
return true; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
} // namespace swissknife |
122 |
|
|
|