Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fs_util subcommand to list known directories #8100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/rust/engine/fs/fs_util/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ to this directory.",
true,
)),
)
.subcommand(
SubCommand::with_name("directories")
.subcommand(SubCommand::with_name("list"))
.about("List all directory digests known in the local store")
)
.subcommand(
SubCommand::with_name("gc")
.about("Garbage collect the on-disk store. Note that after running this command, any processes with an open store (e.g. a pantsd) may need to re-initialize their store.")
Expand Down Expand Up @@ -545,6 +550,18 @@ fn execute(top_match: &clap::ArgMatches<'_>) -> Result<(), ExitError> {
)),
}
}
("directories", Some(sub_match)) => match sub_match.subcommand() {
("list", _) => {
for digest in store
.all_local_digests(::store::EntryType::Directory)
.expect("Error opening store")
{
println!("{} {}", digest.0, digest.1);
}
Ok(())
}
_ => unimplemented!(),
},
("gc", Some(args)) => {
let target_size_bytes = value_t!(args.value_of("target-size-bytes"), usize)
.expect("--target-size-bytes must be passed as a non-negative integer");
Expand Down
32 changes: 32 additions & 0 deletions src/rust/engine/fs/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,10 @@ impl Store {
})
.to_boxed()
}

pub fn all_local_digests(&self, entry_type: EntryType) -> Result<Vec<Digest>, String> {
self.local.all_digests(entry_type)
}
}

// Only public for testing.
Expand Down Expand Up @@ -1180,6 +1184,26 @@ mod local {
}
}).to_boxed()
}

pub fn all_digests(&self, entry_type: EntryType) -> Result<Vec<Digest>, String> {
let database = match entry_type {
EntryType::File => self.inner.file_dbs.clone(),
EntryType::Directory => self.inner.directory_dbs.clone(),
};
let mut digests = vec![];
for &(ref env, ref database, ref _lease_database) in &database?.all_lmdbs() {
let txn = env
.begin_ro_txn()
.map_err(|err| format!("Error beginning transaction to garbage collect: {}", err))?;
let mut cursor = txn
.open_ro_cursor(*database)
.map_err(|err| format!("Failed to open lmdb read cursor: {}", err))?;
for (key, bytes) in cursor.iter() {
digests.push(Digest(Fingerprint::from_bytes_unsafe(key), bytes.len()));
}
}
Ok(digests)
}
}

#[derive(Eq, PartialEq, Ord, PartialOrd)]
Expand Down Expand Up @@ -1624,6 +1648,14 @@ mod local {
)
}

#[test]
pub fn all_digests() {
let dir = TempDir::new().unwrap();
let store = new_store(dir.path());
let digest = prime_store_with_file_bytes(&store, TestData::roland().bytes());
assert_eq!(Ok(vec![digest]), store.all_digests(EntryType::File));
}

pub fn new_store<P: AsRef<Path>>(dir: P) -> ByteStore {
ByteStore::new(task_executor::Executor::new(), dir).unwrap()
}
Expand Down