From 31f4af38e03bbeeb5b6e749cc172c5d9f46f5d20 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sat, 28 Sep 2024 02:46:43 +0900 Subject: [PATCH 01/15] define enum `CustomFile` --- judge-control-app/src/custom_file/traits.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index d9a6977..2f78887 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -19,3 +19,16 @@ pub trait FileFactory: Sized { original: FileType, ) -> Result; } + +pub enum CustomFile { + Directory(Directory), + TextFile(TextFile), +} + +pub struct Directory { + path: PathBuf, +} + +pub struct TextFile { + path: PathBuf, +} From 2aca5719564bb6eea422f756c50a1799531a94d0 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sat, 28 Sep 2024 04:27:25 +0900 Subject: [PATCH 02/15] =?UTF-8?q?`Directory`,=20`TextFile`=20=E3=81=AE=20i?= =?UTF-8?q?mpl=20=E3=81=AE=E9=9B=9B=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge-control-app/src/custom_file/traits.rs | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 2f78887..0e7921c 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -32,3 +32,44 @@ pub struct Directory { pub struct TextFile { path: PathBuf, } + +impl File for Directory { + type InitArgs = todo!(); + fn new(path: PathBuf, args: Self::InitArgs) -> Result { + todo!(); + } + fn get_hardlink_to(&self, path: PathBuf) -> Result { + todo!(); + } +} + +impl File for TextFile { + type InitArgs = todo!(); + fn new(path: PathBuf, args: Self::InitArgs) -> Result { + todo!(); + } + fn get_hardlink_to(&self, path: PathBuf) -> Result { + todo!(); + } +} + +impl Drop for Directory { + fn drop(&mut self) { + todo!(); + } +} + +impl Drop for TextFile { + fn drop(&mut self) { + todo!(); + } +} + +impl TextFile { + fn read(&self) -> String { + todo!(); + } + fn write(&self, contents: String) { + todo!(); + } +} From 48f586babca97bc2af6dcbf5f37d7457b0e7a0ef Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sat, 28 Sep 2024 11:24:23 +0900 Subject: [PATCH 03/15] =?UTF-8?q?`Directory`,=20`TextFile`=20=E3=81=AE=20i?= =?UTF-8?q?mpl=20=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge-control-app/src/custom_file/traits.rs | 30 +++++++++++++-------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 0e7921c..41c5db1 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -1,5 +1,5 @@ #![allow(drop_bounds)] -use anyhow::Result; +use anyhow::{anyhow, Result}; use std::path::PathBuf; use uuid::Uuid; @@ -36,40 +36,48 @@ pub struct TextFile { impl File for Directory { type InitArgs = todo!(); fn new(path: PathBuf, args: Self::InitArgs) -> Result { - todo!(); + std::fs::create_dir_all(&path)?; + Ok(Directory { path }) } fn get_hardlink_to(&self, path: PathBuf) -> Result { - todo!(); + Err(anyhow!("hard link not allowed for directory")) } } impl File for TextFile { type InitArgs = todo!(); fn new(path: PathBuf, args: Self::InitArgs) -> Result { - todo!(); + std::fs::File::create(&path)?; + Ok(TextFile { path }) } fn get_hardlink_to(&self, path: PathBuf) -> Result { - todo!(); + std::fs::hard_link(&self.path, &path)?; + TextFile::new(path, todo!()) } } impl Drop for Directory { fn drop(&mut self) { - todo!(); + std::fs::remove_dir_all(&self.path); } } impl Drop for TextFile { fn drop(&mut self) { - todo!(); + std::fs::remove_file(&self.path); } } impl TextFile { - fn read(&self) -> String { - todo!(); + fn read(&self) -> Result { + let mut f = OpenOptions::new().read(true).open(&self.path)?; + let mut contents = String::new(); + f.read_to_string(&mut contents)?; + Ok(contents) } - fn write(&self, contents: String) { - todo!(); + fn write(&self, contents: String) -> Result<()> { + let mut f = OpenOptions::new().write(true).open(&self.path)?; + f.write_all(contents.as_bytes())?; + Ok(()) } } From 9b9f6bb7640030e7234d57317ee5b7c482176718 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sun, 29 Sep 2024 22:41:03 +0900 Subject: [PATCH 04/15] =?UTF-8?q?`InitArgs`=20=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge-control-app/src/custom_file/traits.rs | 29 +++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 41c5db1..168e3fc 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -1,6 +1,9 @@ #![allow(drop_bounds)] use anyhow::{anyhow, Result}; -use std::path::PathBuf; +use std::{ + io::{Read, Write}, + path::PathBuf, +}; use uuid::Uuid; pub trait File: Sized + Drop { @@ -34,25 +37,29 @@ pub struct TextFile { } impl File for Directory { - type InitArgs = todo!(); - fn new(path: PathBuf, args: Self::InitArgs) -> Result { + type InitArgs = (); + fn new(path: PathBuf, _args: Self::InitArgs) -> Result { std::fs::create_dir_all(&path)?; Ok(Directory { path }) } - fn get_hardlink_to(&self, path: PathBuf) -> Result { + fn get_hardlink_to(&self, _path: PathBuf) -> Result { Err(anyhow!("hard link not allowed for directory")) } } impl File for TextFile { - type InitArgs = todo!(); + type InitArgs = Option; fn new(path: PathBuf, args: Self::InitArgs) -> Result { std::fs::File::create(&path)?; - Ok(TextFile { path }) + let res = TextFile { path }; + if let Some(contents) = args { + res.write(contents)?; + } + Ok(res) } fn get_hardlink_to(&self, path: PathBuf) -> Result { std::fs::hard_link(&self.path, &path)?; - TextFile::new(path, todo!()) + TextFile::new(path, None) } } @@ -70,14 +77,14 @@ impl Drop for TextFile { impl TextFile { fn read(&self) -> Result { - let mut f = OpenOptions::new().read(true).open(&self.path)?; + let mut file = std::fs::OpenOptions::new().read(true).open(&self.path)?; let mut contents = String::new(); - f.read_to_string(&mut contents)?; + file.read_to_string(&mut contents)?; Ok(contents) } fn write(&self, contents: String) -> Result<()> { - let mut f = OpenOptions::new().write(true).open(&self.path)?; - f.write_all(contents.as_bytes())?; + let mut file = std::fs::OpenOptions::new().write(true).open(&self.path)?; + file.write_all(contents.as_bytes())?; Ok(()) } } From 19fc767a58c69bf5efb10b70cb8cbf76298c10ce Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sun, 29 Sep 2024 23:24:14 +0900 Subject: [PATCH 05/15] =?UTF-8?q?`FileManager`=20=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge-control-app/src/custom_file/traits.rs | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 168e3fc..3d54e6f 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -88,3 +88,25 @@ impl TextFile { Ok(()) } } + +pub struct FileManager { + path: PathBuf, +} + +impl FileFactory for FileManager { + fn new(path: PathBuf) -> Result { + if path.is_dir() { + Ok(FileManager { path }) + } else { + Err(anyhow!("path must be an existing dir")) + } + } + fn get_file(&self, uuid: Uuid, args: FileType::InitArgs) -> Result { + let path = self.path.join(uuid.to_string()); + FileType::new(path, args) + } + fn get_hardlink_of(&self, uuid: Uuid, original: FileType) -> Result { + let path = self.path.join(uuid.to_string()); + original.get_hardlink_to(path) + } +} From 183d1cbfa1f361bc94b9bb558c76f4dd6c17a355 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Mon, 30 Sep 2024 00:15:06 +0900 Subject: [PATCH 06/15] Add debug pring on remove failure --- judge-control-app/src/custom_file/traits.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 3d54e6f..187755b 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -65,13 +65,17 @@ impl File for TextFile { impl Drop for Directory { fn drop(&mut self) { - std::fs::remove_dir_all(&self.path); + if let Err(e) = std::fs::remove_dir_all(&self.path) { + eprintln!("{:?}", e); + } } } impl Drop for TextFile { fn drop(&mut self) { - std::fs::remove_file(&self.path); + if let Err(e) = std::fs::remove_file(&self.path) { + eprintln!("{:?}", e); + } } } From 3af4f83688ed7e6a30827046e85f3086a6fef969 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Mon, 30 Sep 2024 00:30:50 +0900 Subject: [PATCH 07/15] Remove unnecessary enum --- judge-control-app/src/custom_file/traits.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 187755b..f0a80f4 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -23,11 +23,6 @@ pub trait FileFactory: Sized { ) -> Result; } -pub enum CustomFile { - Directory(Directory), - TextFile(TextFile), -} - pub struct Directory { path: PathBuf, } From 80d7f25da40f1a0d4f18544f753d87213d4d941a Mon Sep 17 00:00:00 2001 From: hayatroid Date: Fri, 4 Oct 2024 16:00:36 +0900 Subject: [PATCH 08/15] Change `get` to `create` --- judge-control-app/src/custom_file/traits.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index f0a80f4..6d06329 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -37,7 +37,7 @@ impl File for Directory { std::fs::create_dir_all(&path)?; Ok(Directory { path }) } - fn get_hardlink_to(&self, _path: PathBuf) -> Result { + fn create_hardlink_to(&self, _path: PathBuf) -> Result { Err(anyhow!("hard link not allowed for directory")) } } @@ -52,7 +52,7 @@ impl File for TextFile { } Ok(res) } - fn get_hardlink_to(&self, path: PathBuf) -> Result { + fn create_hardlink_to(&self, path: PathBuf) -> Result { std::fs::hard_link(&self.path, &path)?; TextFile::new(path, None) } @@ -100,12 +100,12 @@ impl FileFactory for FileManager { Err(anyhow!("path must be an existing dir")) } } - fn get_file(&self, uuid: Uuid, args: FileType::InitArgs) -> Result { + fn create_file(&self, uuid: Uuid, args: FileType::InitArgs) -> Result { let path = self.path.join(uuid.to_string()); FileType::new(path, args) } - fn get_hardlink_of(&self, uuid: Uuid, original: FileType) -> Result { + fn create_hardlink_of(&self, uuid: Uuid, original: FileType) -> Result { let path = self.path.join(uuid.to_string()); - original.get_hardlink_to(path) + original.create_hardlink_to(path) } } From 101ebac31541675bdf9c283b6dba010cff775325 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Fri, 4 Oct 2024 16:20:09 +0900 Subject: [PATCH 09/15] fix: hardlink should not be created with `TextFile::new()` --- judge-control-app/src/custom_file/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 6d06329..07f7179 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -54,7 +54,7 @@ impl File for TextFile { } fn create_hardlink_to(&self, path: PathBuf) -> Result { std::fs::hard_link(&self.path, &path)?; - TextFile::new(path, None) + Ok(TextFile { path }) } } From c50e3fb98e9776367e8463f829a9daf54a6594c5 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Fri, 4 Oct 2024 16:29:34 +0900 Subject: [PATCH 10/15] Add comments --- judge-control-app/src/custom_file/traits.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 07f7179..e72b22b 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -93,6 +93,7 @@ pub struct FileManager { } impl FileFactory for FileManager { + // ベースとなる path を指定 fn new(path: PathBuf) -> Result { if path.is_dir() { Ok(FileManager { path }) @@ -100,10 +101,12 @@ impl FileFactory for FileManager { Err(anyhow!("path must be an existing dir")) } } + // path/{uuid} にファイルまたはディレクトリを作成 fn create_file(&self, uuid: Uuid, args: FileType::InitArgs) -> Result { let path = self.path.join(uuid.to_string()); FileType::new(path, args) } + // path/{uuid} に original のハードリンクを作成 fn create_hardlink_of(&self, uuid: Uuid, original: FileType) -> Result { let path = self.path.join(uuid.to_string()); original.create_hardlink_to(path) From fd25157785266380d812093d33a3fb291041855f Mon Sep 17 00:00:00 2001 From: hayatroid Date: Fri, 4 Oct 2024 18:01:37 +0900 Subject: [PATCH 11/15] =?UTF-8?q?`&`=20=E3=82=92=E3=81=A4=E3=81=91?= =?UTF-8?q?=E3=82=8B=E3=81=B9=E3=81=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge-control-app/src/custom_file/traits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index e72b22b..0a1706e 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -19,7 +19,7 @@ pub trait FileFactory: Sized { fn create_hardlink_of( &self, uuid: Uuid, - original: FileType, + original: &FileType, ) -> Result; } @@ -107,7 +107,7 @@ impl FileFactory for FileManager { FileType::new(path, args) } // path/{uuid} に original のハードリンクを作成 - fn create_hardlink_of(&self, uuid: Uuid, original: FileType) -> Result { + fn create_hardlink_of(&self, uuid: Uuid, original: &FileType) -> Result { let path = self.path.join(uuid.to_string()); original.create_hardlink_to(path) } From 481c7f4b583f3935b72a3a4230098894a43ea0a6 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Fri, 4 Oct 2024 19:14:03 +0900 Subject: [PATCH 12/15] Split file --- .../src/custom_file/directory.rs | 28 ++++++ .../src/custom_file/file_factory.rs | 39 ++++++++ .../src/custom_file/text_file.rs | 49 ++++++++++ judge-control-app/src/custom_file/traits.rs | 97 +------------------ 4 files changed, 118 insertions(+), 95 deletions(-) create mode 100644 judge-control-app/src/custom_file/directory.rs create mode 100644 judge-control-app/src/custom_file/file_factory.rs create mode 100644 judge-control-app/src/custom_file/text_file.rs diff --git a/judge-control-app/src/custom_file/directory.rs b/judge-control-app/src/custom_file/directory.rs new file mode 100644 index 0000000..405bcae --- /dev/null +++ b/judge-control-app/src/custom_file/directory.rs @@ -0,0 +1,28 @@ +use std::path::PathBuf; + +use anyhow::{anyhow, Result}; + +use super::traits::File; + +pub struct Directory { + path: PathBuf, +} + +impl File for Directory { + type InitArgs = (); + fn new(path: PathBuf, _args: Self::InitArgs) -> Result { + std::fs::create_dir_all(&path)?; + Ok(Directory { path }) + } + fn create_hardlink_to(&self, _path: PathBuf) -> Result { + Err(anyhow!("hard link not allowed for directory")) + } +} + +impl Drop for Directory { + fn drop(&mut self) { + if let Err(e) = std::fs::remove_dir_all(&self.path) { + eprintln!("{:?}", e); + } + } +} diff --git a/judge-control-app/src/custom_file/file_factory.rs b/judge-control-app/src/custom_file/file_factory.rs new file mode 100644 index 0000000..80892a7 --- /dev/null +++ b/judge-control-app/src/custom_file/file_factory.rs @@ -0,0 +1,39 @@ +use std::path::PathBuf; + +use anyhow::{anyhow, Result}; +use uuid::Uuid; + +use super::traits::{self, File}; + +pub struct FileFactory { + path: PathBuf, +} + +impl traits::FileFactory for FileFactory { + // ベースとなる path を指定 + fn new(path: PathBuf) -> Result { + if path.is_dir() { + Ok(FileFactory { path }) + } else { + Err(anyhow!("path must be an existing dir")) + } + } + // path/{uuid} にファイルまたはディレクトリを作成 + fn create_file( + &self, + uuid: Uuid, + args: FileType::InitArgs, + ) -> Result { + let path = self.path.join(uuid.to_string()); + FileType::new(path, args) + } + // path/{uuid} に original のハードリンクを作成 + fn create_hardlink_of( + &self, + uuid: Uuid, + original: &FileType, + ) -> Result { + let path = self.path.join(uuid.to_string()); + original.create_hardlink_to(path) + } +} diff --git a/judge-control-app/src/custom_file/text_file.rs b/judge-control-app/src/custom_file/text_file.rs new file mode 100644 index 0000000..56f8472 --- /dev/null +++ b/judge-control-app/src/custom_file/text_file.rs @@ -0,0 +1,49 @@ +use std::{ + io::{Read, Write}, + path::PathBuf, +}; + +use anyhow::Result; + +use super::traits::File; + +pub struct TextFile { + path: PathBuf, +} + +impl File for TextFile { + type InitArgs = Option; + fn new(path: PathBuf, args: Self::InitArgs) -> Result { + std::fs::File::create(&path)?; + let res = TextFile { path }; + if let Some(contents) = args { + res.write(contents)?; + } + Ok(res) + } + fn create_hardlink_to(&self, path: PathBuf) -> Result { + std::fs::hard_link(&self.path, &path)?; + Ok(TextFile { path }) + } +} +impl Drop for TextFile { + fn drop(&mut self) { + if let Err(e) = std::fs::remove_file(&self.path) { + eprintln!("{:?}", e); + } + } +} + +impl TextFile { + fn read(&self) -> Result { + let mut file = std::fs::OpenOptions::new().read(true).open(&self.path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) + } + fn write(&self, contents: String) -> Result<()> { + let mut file = std::fs::OpenOptions::new().write(true).open(&self.path)?; + file.write_all(contents.as_bytes())?; + Ok(()) + } +} diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 0a1706e..2646759 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -1,9 +1,6 @@ #![allow(drop_bounds)] -use anyhow::{anyhow, Result}; -use std::{ - io::{Read, Write}, - path::PathBuf, -}; +use anyhow::Result; +use std::path::PathBuf; use uuid::Uuid; pub trait File: Sized + Drop { @@ -22,93 +19,3 @@ pub trait FileFactory: Sized { original: &FileType, ) -> Result; } - -pub struct Directory { - path: PathBuf, -} - -pub struct TextFile { - path: PathBuf, -} - -impl File for Directory { - type InitArgs = (); - fn new(path: PathBuf, _args: Self::InitArgs) -> Result { - std::fs::create_dir_all(&path)?; - Ok(Directory { path }) - } - fn create_hardlink_to(&self, _path: PathBuf) -> Result { - Err(anyhow!("hard link not allowed for directory")) - } -} - -impl File for TextFile { - type InitArgs = Option; - fn new(path: PathBuf, args: Self::InitArgs) -> Result { - std::fs::File::create(&path)?; - let res = TextFile { path }; - if let Some(contents) = args { - res.write(contents)?; - } - Ok(res) - } - fn create_hardlink_to(&self, path: PathBuf) -> Result { - std::fs::hard_link(&self.path, &path)?; - Ok(TextFile { path }) - } -} - -impl Drop for Directory { - fn drop(&mut self) { - if let Err(e) = std::fs::remove_dir_all(&self.path) { - eprintln!("{:?}", e); - } - } -} - -impl Drop for TextFile { - fn drop(&mut self) { - if let Err(e) = std::fs::remove_file(&self.path) { - eprintln!("{:?}", e); - } - } -} - -impl TextFile { - fn read(&self) -> Result { - let mut file = std::fs::OpenOptions::new().read(true).open(&self.path)?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - Ok(contents) - } - fn write(&self, contents: String) -> Result<()> { - let mut file = std::fs::OpenOptions::new().write(true).open(&self.path)?; - file.write_all(contents.as_bytes())?; - Ok(()) - } -} - -pub struct FileManager { - path: PathBuf, -} - -impl FileFactory for FileManager { - // ベースとなる path を指定 - fn new(path: PathBuf) -> Result { - if path.is_dir() { - Ok(FileManager { path }) - } else { - Err(anyhow!("path must be an existing dir")) - } - } - // path/{uuid} にファイルまたはディレクトリを作成 - fn create_file(&self, uuid: Uuid, args: FileType::InitArgs) -> Result { - let path = self.path.join(uuid.to_string()); - FileType::new(path, args) - } - // path/{uuid} に original のハードリンクを作成 - fn create_hardlink_of(&self, uuid: Uuid, original: &FileType) -> Result { - let path = self.path.join(uuid.to_string()); - original.create_hardlink_to(path) - } -} From c9061ef048538e5ceb48e505b03107fdf8edb193 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sat, 5 Oct 2024 10:40:53 +0900 Subject: [PATCH 13/15] Add `pub mod` --- judge-control-app/src/custom_file.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/judge-control-app/src/custom_file.rs b/judge-control-app/src/custom_file.rs index f6ac8fc..b727e63 100644 --- a/judge-control-app/src/custom_file.rs +++ b/judge-control-app/src/custom_file.rs @@ -1 +1,5 @@ pub mod traits; + +pub mod directory; +pub mod file_factory; +pub mod text_file; From 8a26bfc8332c1b10c1f5ab71fd7badf04e97fc79 Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sun, 20 Oct 2024 15:10:07 +0900 Subject: [PATCH 14/15] =?UTF-8?q?draft=20PR=20=E3=81=A7=E3=81=AE=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=81=AE=E5=86=85=E5=AE=B9=E3=82=92=E5=8F=8D=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/custom_file/directory.rs | 56 ++++++++++---- .../src/custom_file/file_factory.rs | 34 +++------ .../src/custom_file/text_file.rs | 73 +++++++++++-------- judge-control-app/src/custom_file/traits.rs | 9 ++- 4 files changed, 101 insertions(+), 71 deletions(-) diff --git a/judge-control-app/src/custom_file/directory.rs b/judge-control-app/src/custom_file/directory.rs index 405bcae..cdd30ac 100644 --- a/judge-control-app/src/custom_file/directory.rs +++ b/judge-control-app/src/custom_file/directory.rs @@ -1,28 +1,56 @@ -use std::path::PathBuf; +use std::{ + path::PathBuf, + sync::{Arc, RwLock}, +}; -use anyhow::{anyhow, Result}; +use super::traits::{File, FileEntity, FileLink}; -use super::traits::File; - -pub struct Directory { +struct DirectoryEntity { path: PathBuf, } -impl File for Directory { +impl FileEntity for DirectoryEntity {} + +impl File for DirectoryEntity { type InitArgs = (); - fn new(path: PathBuf, _args: Self::InitArgs) -> Result { + fn new(path: PathBuf, _args: Self::InitArgs) -> anyhow::Result { std::fs::create_dir_all(&path)?; - Ok(Directory { path }) + Ok(Self { path }) + } +} + +impl Drop for DirectoryEntity { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.path); + unimplemented!("error handling for file deletion failure"); + } +} + +struct DirectoryLink { + path: PathBuf, + entity: Arc>, +} + +impl FileLink for DirectoryLink { + fn create_symlink_to(&self, path: PathBuf) -> anyhow::Result { + Self::new(path, self.entity.clone()) } - fn create_hardlink_to(&self, _path: PathBuf) -> Result { - Err(anyhow!("hard link not allowed for directory")) +} + +impl File for DirectoryLink { + type InitArgs = Arc>; + fn new(path: PathBuf, args: Self::InitArgs) -> anyhow::Result { + std::os::unix::fs::symlink(&args.read().unwrap().path, &path)?; + Ok(Self { + path, + entity: args.clone(), + }) } } -impl Drop for Directory { +impl Drop for DirectoryLink { fn drop(&mut self) { - if let Err(e) = std::fs::remove_dir_all(&self.path) { - eprintln!("{:?}", e); - } + let _ = std::fs::remove_dir_all(&self.path); + unimplemented!("error handling for file deletion failure"); } } diff --git a/judge-control-app/src/custom_file/file_factory.rs b/judge-control-app/src/custom_file/file_factory.rs index 80892a7..ea7c7fa 100644 --- a/judge-control-app/src/custom_file/file_factory.rs +++ b/judge-control-app/src/custom_file/file_factory.rs @@ -1,39 +1,27 @@ use std::path::PathBuf; -use anyhow::{anyhow, Result}; -use uuid::Uuid; - -use super::traits::{self, File}; - -pub struct FileFactory { +struct FileFactory { path: PathBuf, } -impl traits::FileFactory for FileFactory { - // ベースとなる path を指定 - fn new(path: PathBuf) -> Result { - if path.is_dir() { - Ok(FileFactory { path }) - } else { - Err(anyhow!("path must be an existing dir")) - } +impl super::traits::FileFactory for FileFactory { + fn new(path: std::path::PathBuf) -> anyhow::Result { + Ok(Self { path }) } - // path/{uuid} にファイルまたはディレクトリを作成 - fn create_file( + fn create_file( &self, - uuid: Uuid, + uuid: uuid::Uuid, args: FileType::InitArgs, - ) -> Result { + ) -> anyhow::Result { let path = self.path.join(uuid.to_string()); FileType::new(path, args) } - // path/{uuid} に original のハードリンクを作成 - fn create_hardlink_of( + fn create_symlink_of( &self, - uuid: Uuid, + uuid: uuid::Uuid, original: &FileType, - ) -> Result { + ) -> anyhow::Result { let path = self.path.join(uuid.to_string()); - original.create_hardlink_to(path) + original.create_symlink_to(path) } } diff --git a/judge-control-app/src/custom_file/text_file.rs b/judge-control-app/src/custom_file/text_file.rs index 56f8472..d94df99 100644 --- a/judge-control-app/src/custom_file/text_file.rs +++ b/judge-control-app/src/custom_file/text_file.rs @@ -1,49 +1,58 @@ use std::{ - io::{Read, Write}, + io::Write, path::PathBuf, + sync::{Arc, RwLock}, }; -use anyhow::Result; +use super::traits::{File, FileEntity, FileLink}; -use super::traits::File; - -pub struct TextFile { +struct TextFileEntity { path: PathBuf, } -impl File for TextFile { - type InitArgs = Option; - fn new(path: PathBuf, args: Self::InitArgs) -> Result { - std::fs::File::create(&path)?; - let res = TextFile { path }; - if let Some(contents) = args { - res.write(contents)?; - } - Ok(res) - } - fn create_hardlink_to(&self, path: PathBuf) -> Result { - std::fs::hard_link(&self.path, &path)?; - Ok(TextFile { path }) +impl FileEntity for TextFileEntity {} + +impl File for TextFileEntity { + type InitArgs = String; + fn new(path: PathBuf, args: Self::InitArgs) -> anyhow::Result { + let mut file = std::fs::File::create(&path)?; + file.write_all(args.as_bytes())?; + Ok(Self { path }) } } -impl Drop for TextFile { + +impl Drop for TextFileEntity { fn drop(&mut self) { - if let Err(e) = std::fs::remove_file(&self.path) { - eprintln!("{:?}", e); - } + let _ = std::fs::remove_file(&self.path); + unimplemented!("error handling for file deletion failure"); } } -impl TextFile { - fn read(&self) -> Result { - let mut file = std::fs::OpenOptions::new().read(true).open(&self.path)?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - Ok(contents) +struct TextFileLink { + path: PathBuf, + entity: Arc>, +} + +impl FileLink for TextFileLink { + fn create_symlink_to(&self, path: PathBuf) -> anyhow::Result { + Self::new(path, self.entity.clone()) } - fn write(&self, contents: String) -> Result<()> { - let mut file = std::fs::OpenOptions::new().write(true).open(&self.path)?; - file.write_all(contents.as_bytes())?; - Ok(()) +} + +impl File for TextFileLink { + type InitArgs = Arc>; + fn new(path: PathBuf, args: Self::InitArgs) -> anyhow::Result { + std::os::unix::fs::symlink(&args.read().unwrap().path, &path)?; + Ok(Self { + path, + entity: args.clone(), + }) + } +} + +impl Drop for TextFileLink { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.path); + unimplemented!("error handling for file deletion failure"); } } diff --git a/judge-control-app/src/custom_file/traits.rs b/judge-control-app/src/custom_file/traits.rs index 2646759..cc41e07 100644 --- a/judge-control-app/src/custom_file/traits.rs +++ b/judge-control-app/src/custom_file/traits.rs @@ -6,14 +6,19 @@ use uuid::Uuid; pub trait File: Sized + Drop { type InitArgs; fn new(path: PathBuf, args: Self::InitArgs) -> Result; - fn create_hardlink_to(&self, path: PathBuf) -> Result; +} + +pub trait FileEntity: File {} + +pub trait FileLink: File { + fn create_symlink_to(&self, path: PathBuf) -> Result; } pub trait FileFactory: Sized { fn new(path: PathBuf) -> Result; fn create_file(&self, uuid: Uuid, args: FileType::InitArgs) -> Result; - fn create_hardlink_of( + fn create_symlink_of( &self, uuid: Uuid, original: &FileType, From cb0a1b5e90a506e7cd8677584ee5fd255743003a Mon Sep 17 00:00:00 2001 From: hayatroid Date: Sun, 20 Oct 2024 16:00:39 +0900 Subject: [PATCH 15/15] =?UTF-8?q?=E5=90=84=E7=A8=AE=20struct=20=E3=82=92?= =?UTF-8?q?=20public=20=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge-control-app/src/custom_file/directory.rs | 4 ++-- judge-control-app/src/custom_file/file_factory.rs | 2 +- judge-control-app/src/custom_file/text_file.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/judge-control-app/src/custom_file/directory.rs b/judge-control-app/src/custom_file/directory.rs index cdd30ac..0f77713 100644 --- a/judge-control-app/src/custom_file/directory.rs +++ b/judge-control-app/src/custom_file/directory.rs @@ -5,7 +5,7 @@ use std::{ use super::traits::{File, FileEntity, FileLink}; -struct DirectoryEntity { +pub struct DirectoryEntity { path: PathBuf, } @@ -26,7 +26,7 @@ impl Drop for DirectoryEntity { } } -struct DirectoryLink { +pub struct DirectoryLink { path: PathBuf, entity: Arc>, } diff --git a/judge-control-app/src/custom_file/file_factory.rs b/judge-control-app/src/custom_file/file_factory.rs index ea7c7fa..859ba47 100644 --- a/judge-control-app/src/custom_file/file_factory.rs +++ b/judge-control-app/src/custom_file/file_factory.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -struct FileFactory { +pub struct FileFactory { path: PathBuf, } diff --git a/judge-control-app/src/custom_file/text_file.rs b/judge-control-app/src/custom_file/text_file.rs index d94df99..c78b3ea 100644 --- a/judge-control-app/src/custom_file/text_file.rs +++ b/judge-control-app/src/custom_file/text_file.rs @@ -6,7 +6,7 @@ use std::{ use super::traits::{File, FileEntity, FileLink}; -struct TextFileEntity { +pub struct TextFileEntity { path: PathBuf, } @@ -28,7 +28,7 @@ impl Drop for TextFileEntity { } } -struct TextFileLink { +pub struct TextFileLink { path: PathBuf, entity: Arc>, }