]> git.proxmox.com Git - proxmox-backup.git/commitdiff
tree-wide: cleanup manual map/flatten
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 30 Dec 2021 13:35:24 +0000 (14:35 +0100)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 30 Dec 2021 14:02:07 +0000 (15:02 +0100)
found with clippy, best viewed with `-w` ;)

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
pbs-datastore/src/datastore.rs
pbs-fuse-loop/src/fuse_loop.rs
proxmox-restore-daemon/src/proxmox_restore_daemon/api.rs
src/api2/backup/environment.rs
src/api2/node/syslog.rs
src/tools/disks/mod.rs

index fb262fdc3b4af7f1abca113463bc12c8238e2cae..37ef753e9b34f1c24096e0f479a17254e2aff4cb 100644 (file)
@@ -232,21 +232,18 @@ impl DataStore {
         wanted_files.insert(CLIENT_LOG_BLOB_NAME.to_string());
         manifest.files().iter().for_each(|item| { wanted_files.insert(item.filename.clone()); });
 
-        for item in proxmox_sys::fs::read_subdir(libc::AT_FDCWD, &full_path)? {
-            if let Ok(item) = item {
-                if let Some(file_type) = item.file_type() {
-                    if file_type != nix::dir::Type::File { continue; }
-                }
-                let file_name = item.file_name().to_bytes();
-                if file_name == b"." || file_name == b".." { continue; };
-
-                if let Ok(name) = std::str::from_utf8(file_name) {
-                    if wanted_files.contains(name) { continue; }
-                }
-                println!("remove unused file {:?}", item.file_name());
-                let dirfd = item.parent_fd();
-                let _res = unsafe { libc::unlinkat(dirfd, item.file_name().as_ptr(), 0) };
+        for item in proxmox_sys::fs::read_subdir(libc::AT_FDCWD, &full_path)?.flatten() {
+            if let Some(file_type) = item.file_type() {
+                if file_type != nix::dir::Type::File { continue; }
+            }
+            let file_name = item.file_name().to_bytes();
+            if file_name == b"." || file_name == b".." { continue; };
+            if let Ok(name) = std::str::from_utf8(file_name) {
+                if wanted_files.contains(name) { continue; }
             }
+            println!("remove unused file {:?}", item.file_name());
+            let dirfd = item.parent_fd();
+            let _res = unsafe { libc::unlinkat(dirfd, item.file_name().as_ptr(), 0) };
         }
 
         Ok(())
index 6a9e8bff0084071dbe033d59fbd71de25249e03a..a763c3080468ecf1f5d4dda26b1da81aa9bb2c43 100644 (file)
@@ -365,13 +365,11 @@ fn unmap_from_backing(backing_file: &Path, loopdev: Option<&str>) -> Result<(),
 pub fn find_all_mappings() -> Result<impl Iterator<Item = (String, Option<String>)>, Error> {
     // get map of all /dev/loop mappings belonging to us
     let mut loopmap = HashMap::new();
-    for ent in proxmox_sys::fs::scan_subdir(libc::AT_FDCWD, Path::new("/dev/"), &LOOPDEV_REGEX)? {
-        if let Ok(ent) = ent {
-            let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy());
-            if let Ok(file) = get_backing_file(&loopdev) {
-                // insert filename only, strip RUN_DIR/
-                loopmap.insert(file[RUN_DIR.len() + 1..].to_owned(), loopdev);
-            }
+    for ent in proxmox_sys::fs::scan_subdir(libc::AT_FDCWD, Path::new("/dev/"), &LOOPDEV_REGEX)?.flatten() {
+        let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy());
+        if let Ok(file) = get_backing_file(&loopdev) {
+            // insert filename only, strip RUN_DIR/
+            loopmap.insert(file[RUN_DIR.len() + 1..].to_owned(), loopdev);
         }
     }
 
index 081e4576ac58377569d7a3ebe164abc54383d242..4c7552109a9f42ce936cbb865c07911e3bbf30f0 100644 (file)
@@ -158,34 +158,30 @@ fn list(
                 }
                 DirEntryAttribute::Directory { .. } => {
                     // list on directory, return all contained files/dirs
-                    for f in read_subdir(libc::AT_FDCWD, &vm_path)? {
-                        if let Ok(f) = f {
-                            let name = f.file_name().to_bytes();
-                            let path = &Path::new(OsStr::from_bytes(name));
-                            if path.components().count() == 1 {
-                                // ignore '.' and '..'
-                                match path.components().next().unwrap() {
-                                    std::path::Component::CurDir
-                                    | std::path::Component::ParentDir => continue,
-                                    _ => {}
-                                }
-                            }
-
-                            let mut full_vm_path = PathBuf::new();
-                            full_vm_path.push(&vm_path);
-                            full_vm_path.push(path);
-                            let mut full_path = PathBuf::new();
-                            full_path.push(param_path_buf);
-                            full_path.push(path);
-
-                            let entry = get_dir_entry(&full_vm_path);
-                            if let Ok(entry) = entry {
-                                res.push(ArchiveEntry::new(
-                                    full_path.as_os_str().as_bytes(),
-                                    Some(&entry),
-                                ));
+                    for f in read_subdir(libc::AT_FDCWD, &vm_path)?.flatten() {
+                        let name = f.file_name().to_bytes();
+                        let path = &Path::new(OsStr::from_bytes(name));
+                        if path.components().count() == 1 {
+                            // ignore '.' and '..'
+                            match path.components().next().unwrap() {
+                                std::path::Component::CurDir
+                                | std::path::Component::ParentDir => continue,
+                                _ => {}
                             }
                         }
+                        let mut full_vm_path = PathBuf::new();
+                        full_vm_path.push(&vm_path);
+                        full_vm_path.push(path);
+                        let mut full_path = PathBuf::new();
+                        full_path.push(param_path_buf);
+                        full_path.push(path);
+                        let entry = get_dir_entry(&full_vm_path);
+                        if let Ok(entry) = entry {
+                            res.push(ArchiveEntry::new(
+                                full_path.as_os_str().as_bytes(),
+                                Some(&entry),
+                            ));
+                        }
                     }
                 }
                 _ => unreachable!(),
index fc1b30e870d40ff13eb0ef9a5926286804f8d09f..3e23840fa728cfa1f8b4f8b279a3088fffd35275 100644 (file)
@@ -246,10 +246,7 @@ impl BackupEnvironment {
     pub fn lookup_chunk(&self, digest: &[u8; 32]) -> Option<u32> {
         let state = self.state.lock().unwrap();
 
-        match state.known_chunks.get(digest) {
-            Some(len) => Some(*len),
-            None => None,
-        }
+        state.known_chunks.get(digest).map(|len| *len)
     }
 
     /// Store the writer with an unique ID
index 08dc5edbb25a1fe0c6f52a98a3d3f8d26def0567..443a8b75a336e2be64425ab1216bc4ecbe246667 100644 (file)
@@ -134,11 +134,7 @@ fn get_syslog(
     mut rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<Value, Error> {
 
-    let service = if let Some(service) = param["service"].as_str() {
-        Some(crate::api2::node::services::real_service_name(service))
-    } else {
-        None
-    };
+    let service = param["service"].as_str().map(|service| crate::api2::node::services::real_service_name(service));
 
     let (count, lines) = dump_journal(
         param["start"].as_u64(),
index 867aa6245cb5f3c3d967375e91058f0b86ad17be..2f7d8ce6aa9583975229132c2aca4c766c799d56 100644 (file)
@@ -982,21 +982,14 @@ pub fn create_file_system(disk: &Disk, fs_type: FileSystemType) -> Result<(), Er
 
 /// Block device name completion helper
 pub fn complete_disk_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
-    let mut list = Vec::new();
-
     let dir = match proxmox_sys::fs::scan_subdir(libc::AT_FDCWD, "/sys/block", &BLOCKDEVICE_NAME_REGEX) {
         Ok(dir) => dir,
-        Err(_) => return list,
+        Err(_) => return vec![],
     };
 
-    for item in dir {
-        if let Ok(item) = item {
-            let name = item.file_name().to_str().unwrap().to_string();
-            list.push(name);
-        }
-    }
-
-    list
+    dir.flatten().map(|item| {
+        item.file_name().to_str().unwrap().to_string()
+    }).collect()
 }
 
 /// Read the FS UUID (parse blkid output)