fix change token not fetched

This commit is contained in:
OMGeeky
2024-04-14 13:25:05 +02:00
parent 3c37a55991
commit 5167c8ebd4
6 changed files with 87 additions and 10 deletions

View File

@@ -13,7 +13,9 @@ use std::fmt::{Debug, Display, Formatter};
use tokio::fs;
const FIELDS_FILE: &'static str = "id, name, size, mimeType, kind, md5Checksum, parents, trashed, createdTime, modifiedTime, viewedByMeTime";
const FIELDS_CHANGE: &str = formatcp!("changes(removed, fileId, changeType, file({FIELDS_FILE}))");
const FIELDS_CHANGE: &str = formatcp!(
"nextPageToken, newStartPageToken, changes(removed, fileId, changeType, file({FIELDS_FILE}))"
);
#[derive(Clone)]
pub struct GoogleDrive {
hub: DriveHub<HttpsConnector<HttpConnector>>,
@@ -80,6 +82,7 @@ impl GoogleDrive {
//region changes
#[instrument]
pub async fn get_changes(&mut self) -> Result<Vec<Change>> {
info!("Getting changes");
let mut page_token = Some(self.change_start_token().await?);
let mut changes = Vec::new();
while let Some(current_page_token) = page_token {
@@ -97,7 +100,9 @@ impl GoogleDrive {
.include_items_from_all_drives(false)
.doit()
.await?;
self.changes_start_page_token = body.new_start_page_token;
if let Some(token) = body.new_start_page_token {
self.changes_start_page_token = Some(token);
}
if response.status().is_success() {
changes.extend(body.changes.unwrap_or_default());
page_token = body.next_page_token;
@@ -113,7 +118,7 @@ impl GoogleDrive {
async fn change_start_token(&mut self) -> Result<String> {
Ok(match &self.changes_start_page_token {
None => {
//
info!("Getting start page token");
let token = fs::read_to_string(SETTINGS.get_changes_file_path())
.await
.unwrap_or_default();
@@ -122,14 +127,19 @@ impl GoogleDrive {
} else {
Some(self.get_changes_start_token_from_api().await?)
};
info!("Got start page token: {:?}", self.changes_start_page_token);
self.changes_start_page_token
.clone()
.expect("We just set it")
}
Some(start_token) => start_token.clone(),
Some(start_token) => {
info!("Using cached start page token");
start_token.clone()
}
})
}
async fn get_changes_start_token_from_api(&self) -> Result<String> {
info!("Getting start page token from API");
let (response, body) = self
.hub
.changes()

View File

@@ -1,5 +1,7 @@
use crate::prelude::*;
use futures::{future, prelude::*};
use gdriver_common::drive_structure::meta;
use gdriver_common::ipc::gdriver_service::SETTINGS;
use std::net::SocketAddr;
use tarpc::{
context,
@@ -19,6 +21,11 @@ pub(crate) async fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
#[tokio::main]
async fn main() -> Result<()> {
gdriver_common::tracing_setup::init_tracing();
SETTINGS.initialize_dirs()?;
let root_meta_file = SETTINGS.get_metadata_file_path(&ROOT_ID);
let root_meta = meta::Metadata::root();
meta::write_metadata_file(&root_meta_file, &root_meta)?;
// sample::main().await?;
service::start().await?;
Ok(())

View File

@@ -58,6 +58,9 @@ impl GDriverService for GdriverServer {
context: Context,
id: DriveId,
) -> StdResult<(), GetMetadataError> {
if id == *ROOT_ID {
return Ok(());
}
Err(GetMetadataError::Other)
}
@@ -119,7 +122,7 @@ impl GDriverService for GdriverServer {
Err(UpdateChangesError::Other)
}
async fn update_changes(self, context: Context) -> StdResult<(), UpdateChangesError> {
async fn update_changes(self, _context: Context) -> StdResult<(), UpdateChangesError> {
let drive = self.drive.try_lock();
match drive {
Ok(mut drive) => {
@@ -128,12 +131,13 @@ impl GDriverService for GdriverServer {
dbg!(e);
UpdateChangesError::Remote
})?;
Ok(())
}
Err(_) => {
return Err(UpdateChangesError::Running);
info!("Drive is already updating");
Err(UpdateChangesError::Running)
}
}
Ok(())
}
async fn do_something2(

View File

@@ -173,6 +173,13 @@ impl fuser::Filesystem for Filesystem {
) {
let id = self.get_id_from_ino(ino);
info!("Reading dir: {id:?}/{ino}");
if let Err(e) = utils::update::update(self) {
error!("Got an error during update in readdir: {}", e);
dbg!(e);
reply.error(libc::EIO);
return;
}
match id {
None => {}
Some(id) => {
@@ -225,6 +232,16 @@ mod errors {
mod utils {
use super::*;
use crate::filesystem::attributes::InodeAttributes;
pub mod update {
use super::*;
#[instrument(skip(fs))]
pub fn update(fs: &Filesystem) -> StdResult<(), FilesystemError> {
info!("Updating changes");
send_request!(fs.gdriver_client.update_changes(current_context(),))?
.map_err(GDriverServiceError::from)?;
Ok(())
}
}
pub mod lookup {
use super::*;
use crate::filesystem::attributes::InodeAttributes;

View File

@@ -20,12 +20,33 @@ pub struct Metadata {
pub gid: u32,
pub xattrs: BTreeMap<Vec<u8>, Vec<u8>>,
}
impl Metadata {
pub fn root() -> Self {
Self {
state: FileState::Root,
size: 0,
last_accessed: (0, 0),
last_modified: (0, 0),
last_metadata_changed: (0, 0),
kind: FileKind::Directory,
mode: 0,
hardlinks: 0,
uid: 0,
gid: 0,
xattrs: Default::default(),
}
}
}
pub fn read_metadata_file(path: &Path) -> Result<Metadata> {
debug!("Reading metadata file: {:?}", path);
let reader = File::open(path)?;
Ok(serde_json::from_reader(reader)?)
}
pub fn write_metadata_file(path: &Path, metadata: &Metadata) -> Result<()> {
let reader = File::open(path)?;
debug!("Writing metadata file: {:?}", path);
let reader = File::create(path)?;
Ok(serde_json::to_writer(reader, metadata)?)
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Serialize, Deserialize, Clone, Hash)]
@@ -33,6 +54,7 @@ pub enum FileState {
Downloaded,
Cached,
MetadataOnly,
Root,
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Serialize, Deserialize, Clone, Hash)]

View File

@@ -1,4 +1,4 @@
use crate::prelude::DriveId;
use crate::prelude::*;
use crate::project_dirs::PROJECT_DIRS;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
@@ -10,7 +10,24 @@ pub struct GDriverSettings {
downloaded_path: PathBuf,
data_path: PathBuf,
}
impl GDriverSettings {
#[instrument]
pub fn initialize_dirs(&self) -> Result<()> {
info!("Initializing dirs");
let dirs = vec![
&self.metadata_path,
&self.cache_path,
&self.downloaded_path,
&self.data_path,
];
for dir in dirs {
info!("Creating dir: {:?}", dir);
std::fs::create_dir_all(dir)?;
}
info!("Dirs created");
Ok(())
}
}
impl GDriverSettings {
pub fn metadata_path(&self) -> &Path {
&self.metadata_path