feat(server): implement manager remove functions

main
Jef Roosens 2023-07-13 23:02:20 +02:00
parent 0a0a139407
commit 1149061618
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 83 additions and 58 deletions

View File

@ -71,64 +71,6 @@ impl RepoGroupManager {
ar_db.close().and(ar_files.close()).map_err(Into::into)
}
pub fn remove_pkg_from_arch_repo(
&mut self,
repo: &str,
arch: &str,
pkg_name: &str,
) -> io::Result<bool> {
let arch_repo_dir = self.repo_dir.join(repo).join(arch);
if !arch_repo_dir.exists() {
return Ok(false);
}
for entry in arch_repo_dir.read_dir()? {
let entry = entry?;
// Make sure we skip the archive files
if !entry.metadata()?.is_dir() {
continue;
}
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();
// The directory name should only contain the name of the package. The last two parts
// when splitting on a dash are the pkgver and pkgrel, so we trim those
let name_parts = file_name.split('-').collect::<Vec<_>>();
let name = name_parts[..name_parts.len() - 2].join("-");
if name == pkg_name {
fs::remove_dir_all(entry.path())?;
// Also remove the old package archive
let arch_repo_pkg_dir = self.pkg_dir.join(repo).join(arch);
arch_repo_pkg_dir.read_dir()?.try_for_each(|res| {
res.and_then(|entry: fs::DirEntry| {
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();
// Same trick, but for package files, we also need to trim the arch
let name_parts = file_name.split('-').collect::<Vec<_>>();
let name = name_parts[..name_parts.len() - 3].join("-");
if name == pkg_name {
fs::remove_file(entry.path())
} else {
Ok(())
}
})
})?;
return Ok(true);
}
}
Ok(false)
}
pub fn add_pkg_from_path<P: AsRef<Path>>(&mut self, repo: &str, path: P) -> io::Result<()> {
let mut pkg = Package::open(&path)?;
pkg.calculate_checksum()?;
@ -201,4 +143,87 @@ impl RepoGroupManager {
self.sync(repo, arch)
}
pub fn remove_repo(&mut self, repo: &str) -> io::Result<bool> {
let repo_dir = self.repo_dir.join(&repo);
if !repo_dir.exists() {
Ok(false)
} else {
fs::remove_dir_all(&repo_dir).and(fs::remove_dir_all(self.pkg_dir.join(repo)))?;
Ok(true)
}
}
pub fn remove_arch_repo(&mut self, repo: &str, arch: &str) -> io::Result<bool> {
let sub_path = PathBuf::from(repo).join(arch);
let repo_dir = self.repo_dir.join(&repo).join(&sub_path);
if !repo_dir.exists() {
Ok(false)
} else {
fs::remove_dir_all(&repo_dir).and(fs::remove_dir_all(self.pkg_dir.join(sub_path)))?;
Ok(true)
}
}
pub fn remove_pkg_from_arch_repo(
&mut self,
repo: &str,
arch: &str,
pkg_name: &str,
) -> io::Result<bool> {
let arch_repo_dir = self.repo_dir.join(repo).join(arch);
if !arch_repo_dir.exists() {
return Ok(false);
}
for entry in arch_repo_dir.read_dir()? {
let entry = entry?;
// Make sure we skip the archive files
if !entry.metadata()?.is_dir() {
continue;
}
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();
// The directory name should only contain the name of the package. The last two parts
// when splitting on a dash are the pkgver and pkgrel, so we trim those
let name_parts = file_name.split('-').collect::<Vec<_>>();
let name = name_parts[..name_parts.len() - 2].join("-");
if name == pkg_name {
fs::remove_dir_all(entry.path())?;
// Also remove the old package archive
let arch_repo_pkg_dir = self.pkg_dir.join(repo).join(arch);
arch_repo_pkg_dir.read_dir()?.try_for_each(|res| {
res.and_then(|entry: fs::DirEntry| {
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();
// Same trick, but for package files, we also need to trim the arch
let name_parts = file_name.split('-').collect::<Vec<_>>();
let name = name_parts[..name_parts.len() - 3].join("-");
if name == pkg_name {
fs::remove_file(entry.path())
} else {
Ok(())
}
})
})?;
return Ok(true);
}
}
Ok(false)
}
}