##// END OF EJS Templates
revlog: add a mechanism to verify expected file position before appending...
revlog: add a mechanism to verify expected file position before appending If someone uses `hg debuglocks`, or some non-hg process writes to the .hg directory without respecting the locks, or if the repo's on a networked filesystem, it's possible for the revlog code to write out corrupted data. The form of this corruption can vary depending on what data was written and how that happened. We are in the "networked filesystem" case (though I've had users also do this to themselves with the "`hg debuglocks`" scenario), and most often see this with the changelog. What ends up happening is we produce two items (let's call them rev1 and rev2) in the .i file that have the same linkrev, baserev, and offset into the .d file, while the data in the .d file is appended properly. rev2's compressed_size is accurate for rev2, but when we go to decompress the data in the .d file, we use the offset that's recorded in the index file, which is the same as rev1, and attempt to decompress rev2.compressed_size bytes of rev1's data. This usually does not succeed. :) When using inline data, this also fails, though I haven't investigated why too closely. This shows up as a "patch decode" error. I believe what's happening there is that we're basically ignoring the offset field, getting the data properly, but since baserev != rev, it thinks this is a delta based on rev (instead of a full text) and can't actually apply it as such. For now, I'm going to make this an optional component and default it to entirely off. I may increase the default severity of this in the future, once I've enabled it for my users and we gain more experience with it. Luckily, most of my users have a versioned filesystem and can roll back to before the corruption has been written, it's just a hassle to do so and not everyone knows how (so it's a support burden). Users on other filesystems will not have that luxury, and this can cause them to have a corrupted repository that they are unlikely to know how to resolve, and they'll see this as a data-loss event. Refusing to create the corruption is a much better user experience. This mechanism is not perfect. There may be false-negatives (racy writes that are not detected). There should not be any false-positives (non-racy writes that are detected as such). This is not a mechanism that makes putting a repo on a networked filesystem "safe" or "supported", just *less* likely to cause corruption. Differential Revision: https://phab.mercurial-scm.org/D9952

File last commit:

r47343:755c31a1 default
r47349:e9901d01 default
Show More
main.rs
191 lines | 5.8 KiB | application/rls-services+xml | RustLexer
Antoine Cezar
rhg: Add debug timing...
r46101 extern crate log;
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 use crate::ui::Ui;
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593 use clap::App;
use clap::AppSettings;
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253 use clap::Arg;
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 use clap::ArgMatches;
Simon Sapin
rhg: Simplify CommandError based on its use...
r47174 use format_bytes::format_bytes;
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 use hg::config::Config;
Simon Sapin
rhg: Move `Repo` object creation into `main()`...
r47335 use hg::repo::{Repo, RepoError};
use std::path::{Path, PathBuf};
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 mod blackbox;
Antoine Cezar
rhg: add Command trait for subcommands implemented by rhg...
r45515 mod error;
Antoine Cezar
rhg: add rhg crate...
r45503 mod exitcode;
Antoine Cezar
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`...
r45592 mod ui;
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 use error::CommandError;
Antoine Cezar
rhg: add rhg crate...
r45503
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253 fn add_global_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
app.arg(
Arg::with_name("repository")
.help("repository root directory")
.short("-R")
.long("--repository")
.value_name("REPO")
.takes_value(true),
)
Simon Sapin
rhg: Add support for --config CLI arguments...
r47254 .arg(
Arg::with_name("config")
.help("set/override config option (use 'section.name=value')")
.long("--config")
.value_name("CONFIG")
.takes_value(true)
// Ok: `--config section.key1=val --config section.key2=val2`
.multiple(true)
// Not ok: `--config section.key1=val section.key2=val2`
.number_of_values(1),
)
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253 }
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 fn main_with_result(
ui: &ui::Ui,
process_start_time: &blackbox::ProcessStartTime,
) -> Result<(), CommandError> {
Antoine Cezar
rhg: Add debug timing...
r46101 env_logger::init();
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 let app = App::new("rhg")
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593 .setting(AppSettings::AllowInvalidUtf8)
.setting(AppSettings::SubcommandRequired)
.setting(AppSettings::VersionlessSubcommands)
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 .version("0.0.1");
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253 let app = add_global_args(app);
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 let app = add_subcommand_args(app);
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593
Simon Sapin
rhg: Remove error message on unsupported CLI arguments...
r47333 let matches = app.clone().get_matches_safe()?;
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 let (subcommand_name, subcommand_matches) = matches.subcommand();
let run = subcommand_run_fn(subcommand_name)
.expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired");
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 let subcommand_args = subcommand_matches
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253 // Global arguments can be in either based on e.g. `hg -R ./foo log` v.s.
// `hg log -R ./foo`
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 let value_of_global_arg = |name| {
subcommand_args
.value_of_os(name)
.or_else(|| matches.value_of_os(name))
};
Simon Sapin
rhg: Add support for --config CLI arguments...
r47254 // For arguments where multiple occurences are allowed, return a
// possibly-iterator of all values.
let values_of_global_arg = |name: &str| {
let a = matches.values_of_os(name).into_iter().flatten();
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 let b = subcommand_args.values_of_os(name).into_iter().flatten();
Simon Sapin
rhg: Add support for --config CLI arguments...
r47254 a.chain(b)
};
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253
Simon Sapin
rhg: Remove error message on unsupported CLI arguments...
r47333 let config_args = values_of_global_arg("config")
Simon Sapin
rust: Introduce a get_bytes_from_os_str utility function...
r47338 .map(hg::utils::files::get_bytes_from_os_str);
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 let non_repo_config = &hg::config::Config::load(config_args)?;
let repo_path = value_of_global_arg("repository").map(Path::new);
Simon Sapin
rhg: Move `Repo` object creation into `main()`...
r47335 let repo = match Repo::find(non_repo_config, repo_path) {
Ok(repo) => Ok(repo),
Err(RepoError::NotFound { at }) if repo_path.is_none() => {
// Not finding a repo is not fatal yet, if `-R` was not given
Err(NoRepoInCwdError { cwd: at })
}
Err(error) => return Err(error.into()),
};
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 let invocation = CliInvocation {
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 ui,
subcommand_args,
non_repo_config,
Simon Sapin
rhg: Move `Repo` object creation into `main()`...
r47335 repo: repo.as_ref(),
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 };
let blackbox = blackbox::Blackbox::new(&invocation, process_start_time)?;
blackbox.log_command_start();
let result = run(&invocation);
blackbox.log_command_end(exit_code(&result));
result
Simon Sapin
rhg: Remove error message on unsupported CLI arguments...
r47333 }
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252
Simon Sapin
rhg: Remove error message on unsupported CLI arguments...
r47333 fn main() {
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 // Run this first, before we find out if the blackbox extension is even
// enabled, in order to include everything in-between in the duration
// measurements. Reading config files can be slow if they’re on NFS.
let process_start_time = blackbox::ProcessStartTime::now();
Simon Sapin
rhg: Move `Repo` object creation into `main()`...
r47335 let ui = ui::Ui::new();
Simon Sapin
rhg: Remove error message on unsupported CLI arguments...
r47333
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 let result = main_with_result(&ui, &process_start_time);
if let Err(CommandError::Abort { message }) = &result {
if !message.is_empty() {
// Ignore errors when writing to stderr, we’re already exiting
// with failure code so there’s not much more we can do.
let _ = ui.write_stderr(&format_bytes!(b"abort: {}\n", message));
}
}
std::process::exit(exit_code(&result))
}
fn exit_code(result: &Result<(), CommandError>) -> i32 {
match result {
Simon Sapin
rhg: Remove error message on unsupported CLI arguments...
r47333 Ok(()) => exitcode::OK,
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 Err(CommandError::Abort { .. }) => exitcode::ABORT,
Simon Sapin
rhg: Simplify CommandError based on its use...
r47174
// Exit with a specific code and no error message to let a potential
// wrapper script fallback to Python-based Mercurial.
Err(CommandError::Unimplemented) => exitcode::UNIMPLEMENTED,
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 }
Antoine Cezar
rhg: add rhg crate...
r45503 }
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 macro_rules! subcommands {
($( $command: ident )+) => {
mod commands {
$(
pub mod $command;
)+
}
fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
app
$(
Simon Sapin
rhg: Add support for -R and --repository command-line arguments...
r47253 .subcommand(add_global_args(commands::$command::args()))
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 )+
}
Simon Sapin
rhg: Parse system and user configuration at program start...
r47213
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>;
fn subcommand_run_fn(name: &str) -> Option<RunFn> {
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 match name {
$(
stringify!($command) => Some(commands::$command::run),
)+
_ => None,
}
Antoine Cezar
rhg: add a limited `rhg cat -r` subcommand...
r46113 }
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 };
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 }
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252
subcommands! {
cat
debugdata
debugrequirements
files
root
Simon Sapin
rhg: add limited support for the `config` sub-command...
r47255 config
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47252 }
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 pub struct CliInvocation<'a> {
ui: &'a Ui,
subcommand_args: &'a ArgMatches<'a>,
non_repo_config: &'a Config,
Simon Sapin
rhg: Move `Repo` object creation into `main()`...
r47335 /// References inside `Result` is a bit peculiar but allow
/// `invocation.repo?` to work out with `&CliInvocation` since this
/// `Result` type is `Copy`.
repo: Result<&'a Repo, &'a NoRepoInCwdError>,
}
struct NoRepoInCwdError {
cwd: PathBuf,
Simon Sapin
rhg: Group values passed to every sub-command into a struct...
r47334 }
Simon Sapin
rhg: Move `Repo` object creation into `main()`...
r47335
impl CliInvocation<'_> {
fn config(&self) -> &Config {
if let Ok(repo) = self.repo {
repo.config()
} else {
self.non_repo_config
}
}
}