|
7 | 7 | #include <fmt/std.h> |
8 | 8 | #include <spdlog/spdlog.h> |
9 | 9 |
|
| 10 | +#include <uenv/env.h> |
10 | 11 | #include <uenv/parse.h> |
11 | 12 | #include <uenv/print.h> |
12 | 13 | #include <uenv/repository.h> |
@@ -77,12 +78,33 @@ int image_add(const image_add_args& args, const global_settings& settings) { |
77 | 78 |
|
78 | 79 | spdlog::info("image_add: label {}", *label); |
79 | 80 |
|
80 | | - auto sqfs = uenv::validate_squashfs_image(args.squashfs); |
81 | | - if (!sqfs) { |
82 | | - term::error("invalid squashfs file {}: {}", args.squashfs, |
83 | | - sqfs.error()); |
| 81 | + const auto env = concretise_env(args.squashfs, {}, settings.config.repo, |
| 82 | + settings.calling_environment); |
| 83 | + if (!env) { |
| 84 | + term::error("{}", env.error()); |
84 | 85 | return 1; |
85 | 86 | } |
| 87 | + if (env->uenvs.size() != 1) { |
| 88 | + term::error("Too many arguments provided for source squashfs file"); |
| 89 | + return 1; |
| 90 | + } |
| 91 | + |
| 92 | + auto concrete_uenv = env->uenvs.begin()->second; |
| 93 | + bool from_label = concrete_uenv.label.has_value(); |
| 94 | + |
| 95 | + util::expected<squashfs_image, std::string> sqfs; |
| 96 | + if (from_label) { |
| 97 | + sqfs.emplace(concrete_uenv.sqfs_path, concrete_uenv.meta_path, |
| 98 | + concrete_uenv.digest.value()); |
| 99 | + } else { |
| 100 | + sqfs = |
| 101 | + uenv::validate_squashfs_image(env->uenvs.begin()->second.sqfs_path); |
| 102 | + if (!sqfs) { |
| 103 | + term::error("invalid squashfs file {}: {}", args.squashfs, |
| 104 | + sqfs.error()); |
| 105 | + return 1; |
| 106 | + } |
| 107 | + } |
86 | 108 | spdlog::info("image_add: squashfs {}", sqfs.value()); |
87 | 109 |
|
88 | 110 | // |
@@ -144,67 +166,82 @@ int image_add(const image_add_args& args, const global_settings& settings) { |
144 | 166 | } |
145 | 167 |
|
146 | 168 | const auto uenv_paths = store->uenv_paths(sqfs->hash); |
147 | | - |
148 | | - // |
149 | | - // create the path inside the repo |
150 | | - // |
151 | | - std::error_code ec; |
152 | | - // if the path exists, delete it, as it might contain a partial download |
153 | | - if (fs::exists(uenv_paths.store)) { |
154 | | - spdlog::debug("image_add: remove the target path {} before copying", |
155 | | - uenv_paths.store.string()); |
156 | | - fs::remove_all(uenv_paths.store); |
157 | | - } |
158 | 169 | uenv::uenv_date date{*util::file_creation_date(sqfs->sqfs)}; |
159 | 170 |
|
160 | | - fs::create_directories(uenv_paths.store, ec); |
161 | | - if (ec) { |
162 | | - spdlog::error("unable to create path {}: {}", uenv_paths.store.string(), |
163 | | - ec.message()); |
164 | | - term::error("unable to add the uenv"); |
| 171 | + bool source_in_repo = |
| 172 | + util::is_child(sqfs->sqfs, settings.config.repo.value()); |
| 173 | + |
| 174 | + // If an sqfs file is already in repo, and it was pulled from a repository |
| 175 | + // then there is a digest mismatch. Do not try to add this image |
| 176 | + // Such images should be retaged with the command: |
| 177 | + // uenv image add <new-label> <existing-label> |
| 178 | + if (source_in_repo && !existing_hash) { |
| 179 | + term::error("image_add: Trying to add a squashfs file which is already " |
| 180 | + "in the repository, but the hashes do not match"); |
165 | 181 | return 1; |
166 | 182 | } |
167 | 183 |
|
168 | | - // |
169 | | - // copy the meta data into the repo |
170 | | - // |
171 | | - if (sqfs->meta) { |
172 | | - fs::copy_options options{}; |
173 | | - options |= fs::copy_options::recursive; |
174 | | - fs::copy(sqfs->meta.value(), uenv_paths.meta, options, ec); |
175 | | - if (ec) { |
176 | | - spdlog::error("unable to copy meta data to {}: {}", |
177 | | - uenv_paths.meta.string(), ec.message()); |
178 | | - term::error("unable to add the uenv"); |
179 | | - return 1; |
| 184 | + if (!source_in_repo) { |
| 185 | + // |
| 186 | + // create the path inside the repo |
| 187 | + // |
| 188 | + std::error_code ec; |
| 189 | + // if the path exists, delete it, as it might contain a partial download |
| 190 | + if (fs::exists(uenv_paths.store)) { |
| 191 | + spdlog::debug("image_add: remove the target path {} before copying", |
| 192 | + uenv_paths.store.string()); |
| 193 | + fs::remove_all(uenv_paths.store); |
180 | 194 | } |
181 | | - } |
182 | 195 |
|
183 | | - // copy or move the |
184 | | - if (!args.move) { |
185 | | - fs::copy_file(sqfs->sqfs, uenv_paths.squashfs, ec); |
| 196 | + fs::create_directories(uenv_paths.store, ec); |
186 | 197 | if (ec) { |
187 | | - spdlog::error("unable to copy squashfs image {} to {}: {}", |
188 | | - sqfs->sqfs.string(), uenv_paths.squashfs.string(), |
189 | | - ec.message()); |
| 198 | + spdlog::error("unable to create path {}: {}", |
| 199 | + uenv_paths.store.string(), ec.message()); |
190 | 200 | term::error("unable to add the uenv"); |
191 | 201 | return 1; |
192 | 202 | } |
193 | | - } else { |
194 | | - fs::rename(sqfs->sqfs, uenv_paths.squashfs, ec); |
195 | | - if (ec) { |
196 | | - spdlog::error("unable to move squashfs image {} to {}: {}", |
197 | | - sqfs->sqfs.string(), uenv_paths.squashfs.string(), |
198 | | - ec.message()); |
199 | | - term::error( |
200 | | - "unable to add the uenv\n{}", |
201 | | - help::item{help::block{ |
202 | | - help::block::admonition::note, |
203 | | - fmt::format( |
204 | | - "check that the file {} is on the same filesystem as " |
205 | | - "the repository, and that you have write access to it.", |
206 | | - sqfs->sqfs.string())}}); |
207 | | - return 1; |
| 203 | + |
| 204 | + // |
| 205 | + // copy the meta data into the repo |
| 206 | + // |
| 207 | + if (sqfs->meta) { |
| 208 | + fs::copy_options options{}; |
| 209 | + options |= fs::copy_options::recursive; |
| 210 | + fs::copy(sqfs->meta.value(), uenv_paths.meta, options, ec); |
| 211 | + if (ec) { |
| 212 | + spdlog::error("unable to copy meta data to {}: {}", |
| 213 | + uenv_paths.meta.string(), ec.message()); |
| 214 | + term::error("unable to add the uenv"); |
| 215 | + return 1; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + // copy or move the |
| 220 | + if (!args.move) { |
| 221 | + fs::copy_file(sqfs->sqfs, uenv_paths.squashfs, ec); |
| 222 | + if (ec) { |
| 223 | + spdlog::error("unable to copy squashfs image {} to {}: {}", |
| 224 | + sqfs->sqfs.string(), uenv_paths.squashfs.string(), |
| 225 | + ec.message()); |
| 226 | + term::error("unable to add the uenv"); |
| 227 | + return 1; |
| 228 | + } |
| 229 | + } else { |
| 230 | + fs::rename(sqfs->sqfs, uenv_paths.squashfs, ec); |
| 231 | + if (ec) { |
| 232 | + spdlog::error("unable to move squashfs image {} to {}: {}", |
| 233 | + sqfs->sqfs.string(), uenv_paths.squashfs.string(), |
| 234 | + ec.message()); |
| 235 | + term::error("unable to add the uenv\n{}", |
| 236 | + help::item{help::block{ |
| 237 | + help::block::admonition::note, |
| 238 | + fmt::format("check that the file {} is on the " |
| 239 | + "same filesystem as " |
| 240 | + "the repository, and that you have " |
| 241 | + "write access to it.", |
| 242 | + sqfs->sqfs.string())}}); |
| 243 | + return 1; |
| 244 | + } |
208 | 245 | } |
209 | 246 | } |
210 | 247 |
|
|
0 commit comments