Skip to content

Commit

Permalink
Deleting a regular file moves it to Trash. Deleting a trashed file
Browse files Browse the repository at this point in the history
removes it permanently.
  • Loading branch information
harababurel committed Dec 21, 2018
1 parent 753ab88 commit bd528c3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "gcsf"
description = "Filesystem based on Google Drive"
version = "0.1.18"
version = "0.1.19"
repository = "https://github.com/harababurel/gcsf"
authors = ["Sergiu Puscas <srg.pscs@gmail.com>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/cli.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: GCSF
version: "0.1.18"
version: "0.1.19"
author: Sergiu Puscas <srg.pscs@gmail.com>
about: File system based on Google Drive
subcommands:
Expand Down
2 changes: 1 addition & 1 deletion src/gcsf/drive_facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ impl DriveFacade {
.add_scope(drive3::Scope::Full)
.doit_without_upload()
.map(|_| ())
.map_err(|e| err_msg(format!("DriveFacade::move_to() {}", e)))
.map_err(|e| err_msg(format!("DriveFacade::move_to_trash() {}", e)))
}

pub fn flush(&mut self, id: &DriveId) -> Result<(), Error> {
Expand Down
27 changes: 27 additions & 0 deletions src/gcsf/file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::DateTime;
use drive3;
use failure::{err_msg, Error};
use fuse::{FileAttr, FileType};
use id_tree::NodeId;
use std::collections::HashMap;
Expand Down Expand Up @@ -142,6 +143,32 @@ impl File {
!forbidden.contains(*c)
}

/// Whether this file is trashed on Drive.
pub fn is_trashed(&self) -> bool {
self.drive_file
.as_ref()
.map(|f| f.trashed)
.unwrap_or_default()
.unwrap_or(false)
}

// Trashing a file does not trigger a file update from Drive. Therefore this field must be
// set manually so that GCSF knows that this particular file is trashed and should be deleted
// permanently the next time unlink() is called.
pub fn set_trashed(&mut self, trashed: bool) -> Result<(), Error> {
let ino = self.inode();
if let Some(ref mut drive_file) = self.drive_file.as_mut() {
drive_file.trashed = Some(trashed);
Ok(())
} else {
Err(err_msg(format!(
"Could not set trashed={} because there is no drive file associated to {:?}",
trashed,
FileId::Inode(ino)
)))
}
}

#[allow(dead_code)]
pub fn is_drive_document(&self) -> bool {
self.drive_file
Expand Down
14 changes: 14 additions & 0 deletions src/gcsf/file_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,27 @@ impl FileManager {

self.tree.move_node(&node_id, ToParent(&trash_id))?;

// File cannot be identified by FileId::ParentAndName now because the parent has changed.
// Using DriveId instead.
if also_on_drive {
self.get_mut_file(&FileId::DriveId(drive_id.clone()))
.ok_or(err_msg(format!("Cannot find {:?}", &drive_id)))?
.set_trashed(true)?;
self.df.move_to_trash(drive_id)?;
}

Ok(())
}

/// Whether a file is trashed on Drive.
pub fn file_is_trashed(&mut self, id: &FileId) -> Result<bool, Error> {
let file = self
.get_file(id)
.ok_or(err_msg(format!("Cannot find node_id of {:?}", &id)))?;

Ok(file.is_trashed())
}

/// Moves/renames a file locally *and* on Drive.
pub fn rename(
&mut self,
Expand Down
29 changes: 24 additions & 5 deletions src/gcsf/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,35 @@ impl Filesystem for GCSF {
return;
}

match self.manager.move_file_to_trash(&id, true) {
Ok(response) => {
debug!("{:?}", response);
reply.ok();
match self.manager.file_is_trashed(&id) {
Ok(trashed) => {
let ret = if trashed {
debug!("{:?} is already trashed. Deleting permanently.", id);
self.manager.delete(&id)
} else {
debug!(
"{:?} was not trashed. Moving it to Trash instead of deleting permanently.",
id
);
self.manager.move_file_to_trash(&id, true)
};

match ret {
Ok(response) => {
debug!("{:?}", response);
reply.ok();
}
Err(e) => {
error!("{:?}", e);
reply.error(EREMOTE);
}
};
}
Err(e) => {
error!("{:?}", e);
reply.error(EREMOTE);
}
};
}
}

fn forget(&mut self, _req: &Request, _ino: u64, _nlookup: u64) {}
Expand Down

0 comments on commit bd528c3

Please sign in to comment.