Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#7 add custom file #59

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open

Conversation

hayatroid
Copy link
Collaborator

@hayatroid hayatroid commented Oct 5, 2024

関連Issue

概要

ファイルを管理する構造体を実装しました。下記のような使い方を想定しています。

  • FileFactory::new() に基準となるパスを渡す。ファイル / ディレクトリ / ハードリンクはこのパス直下に作られる。
  • FileFactory::create_file() でファイル / ディレクトリを作成する。
  • FileFactory::create_hardlink_of() でファイルのハードリンクを作成する。
  • ファイル / ディレクトリがドロップすると、これは削除される。
#[cfg(test)]
mod tests {
    use std::{path::PathBuf, thread::sleep, time::Duration};

    use anyhow::Result;
    use uuid::Uuid;

    use crate::custom_file::{
        directory::Directory, file_factory::FileFactory, text_file::TextFile,
        traits::FileFactory as _,
    };

    #[test]
    fn test() -> Result<()> {
        let path = PathBuf::from(".");
        let ff = FileFactory::new(path.clone())?;
        {
            // "./{uuid}" ディレクトリを作成
            let uuid = Uuid::new_v4();
            let dir = ff.create_file::<Directory>(uuid, ())?;

            let path2 = path.join(uuid.to_string());
            let ff2 = FileFactory::new(path2)?;
            {
                // "./{uuid}/{uuid2}" ファイルを作成,"hoge" と書き込み
                let uuid2 = Uuid::new_v4();
                let contents = "hoge".to_string();
                let file = ff2.create_file::<TextFile>(uuid2, Some(contents))?;
                {
                    // "./{uuid}/{uuid3}" に "./{uuid}/{uuid2}" のハードリンクを作成
                    let uuid3 = Uuid::new_v4();
                    let hardlink = ff2.create_hardlink_of::<TextFile>(uuid3, &file)?;
                    sleep(Duration::from_secs(5));
                    // hardlink がドロップ,"./{uuid}/{uuid3}" ファイルを削除
                }
                sleep(Duration::from_secs(5));
                // file がドロップ,"./{uuid}/{uuid2}" ファイルを削除
            }
            sleep(Duration::from_secs(5));
            // dir がドロップ,"./{uuid}" ディレクトリを削除
        }
        Ok(())
    }
}

チェックリスト

  • テストが通っている
  • 下記のいずれかを満たしている
      • 変更点についてテストを追加/修正した
      • テストを追加するissueを立てた
      • テストが不要な変更である
  • レビュワーを指定した
  • タグをつけた

補足

  • 使用する側は、use crate::custom_file::traits::FileFactory as _; をする必要があります(structtrait の名前が被っているため)。

@hayatroid hayatroid added the enhancement New feature or request label Oct 5, 2024
@hayatroid
Copy link
Collaborator Author

チェックリストのテストについてはどうするのがよいですか

Ok(Directory { path })
}
fn create_hardlink_to(&self, _path: PathBuf) -> Result<Self> {
Err(anyhow!("hard link not allowed for directory"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ知りませんでしたごめんなさい
シンボリックリンクで同じような振る舞いを再現してほしいです

impl Drop for Directory {
fn drop(&mut self) {
if let Err(e) = std::fs::remove_dir_all(&self.path) {
eprintln!("{:?}", e);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grafanaについて調べてから実装したいので、この部分を!unimplementedに変更してほしいです

}

impl TextFile {
fn read(&self) -> Result<String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readがdead codeになる気がします

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@comavius どう対処すればよいですか?#[allow(dead_code)] でよいですか?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

クラス内部で呼び出さないならいらない気がします

Copy link
Collaborator

@comavius comavius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

設計に色々不備があってごめん

@comavius
Copy link
Collaborator

comavius commented Oct 5, 2024

チェックリストのテストについてはどうするのがよいですか

今回はなしでissue立てだけお願いします

@hayatroid
Copy link
Collaborator Author

@comavius レビューお願いします.directory.rstext_file.rs にある 4 箇所の unimplemented!(ファイル削除時のエラーハンドリングをどうするかの部分)を消して,下記のようなプログラムを書くとテストすることができると思います.

シンボリックリンクを発行する機能を Entity に実装していないので,

  • EntityArc<RwLock<>> でラップして使う.
  • Entity のシンボリックリンクは FileFactory::create_file() で作成する
  • Link のシンボリックリンクは FileFactory::create_symlink_of() で作成する

とする必要があります.これまでに指摘された点はすべて修正したつもりですが,反映忘れ等あったらすみません.

#[cfg(test)]
mod tests {
    use std::{
        path::PathBuf,
        sync::{Arc, RwLock},
        thread::sleep,
        time::Duration,
    };

    use uuid::Uuid;

    use crate::custom_file::{
        directory::{DirectoryEntity, DirectoryLink},
        file_factory::FileFactory,
        text_file::{TextFileEntity, TextFileLink},
        traits::FileFactory as _,
    };

    fn create_entity() -> anyhow::Result<(
        FileFactory,
        Arc<RwLock<DirectoryEntity>>,
        Arc<RwLock<TextFileEntity>>,
    )> {
        let ff = FileFactory::new(PathBuf::from("."))?;
        let dir_entity = ff.create_file::<DirectoryEntity>(Uuid::new_v4(), ())?;
        let txt_entity = ff.create_file::<TextFileEntity>(Uuid::new_v4(), "hoge".to_string())?;
        Ok((ff, Arc::new(RwLock::new(dir_entity)), Arc::new(RwLock::new(txt_entity))))
    }

    fn create_symlink_of_entity() -> anyhow::Result<(FileFactory, DirectoryLink, TextFileLink)> {
        let (ff, dir_entity, txt_entity) = create_entity()?;
        let dir_symlink = ff.create_file::<DirectoryLink>(Uuid::new_v4(), dir_entity.clone())?;
        let txt_symlink = ff.create_file::<TextFileLink>(Uuid::new_v4(), txt_entity.clone())?;
        Ok((ff, dir_symlink, txt_symlink))
    }

    fn create_symlink_of_symlink() -> anyhow::Result<(FileFactory, DirectoryLink, TextFileLink)> {
        let (ff, dir_symlink, txt_symlink) = create_symlink_of_entity()?;
        let dir_symlink2 = ff.create_symlink_of(Uuid::new_v4(), &dir_symlink)?;
        let txt_symlink2 = ff.create_symlink_of(Uuid::new_v4(), &txt_symlink)?;
        Ok((ff, dir_symlink2, txt_symlink2))
    }

    #[test]
    fn test_create_entity() -> anyhow::Result<()> {
        // "." 以下に dir_entity, txt_entity が作られる.
        // dir_entity, txt_entity が 5 秒後に消える.
        let (_, dir_entity, txt_entity) = create_entity()?;
        sleep(Duration::from_secs(5));
        drop(dir_entity);
        drop(txt_entity);
        Ok(())
    }

    #[test]
    fn test_create_symlink_of_entity() -> anyhow::Result<()> {
        // "." 以下に dir_entity, txt_entity が作られる.
        // "." 以下に dir_symlink, txt_symlink が作られる.
        // dir_symlink, txt_symlink が 5 秒後に消える.
        // dir_entity, txt_entity が 5 秒後に消える.
        let (_, dir_symlink, txt_symlink) = create_symlink_of_entity()?;
        sleep(Duration::from_secs(5));
        drop(dir_symlink);
        drop(txt_symlink);
        Ok(())
    }

    #[test]
    fn test_create_symlink_of_symlink() -> anyhow::Result<()> {
        // "." 以下に dir_entity, txt_entity が作られる.
        // "." 以下に dir_symlink, txt_symlink が作られる.
        // "." 以下に dir_symlink2, txt_symlink2 が作られる.
        // dir_symlink, txt_symlink が即消える.
        // dir_symlink2, txt_symlink2 が 5 秒後に消える.
        // dir_entity, txt_entity が 5 秒後に消える.
        let (_, dir_symlink2, txt_symlink2) = create_symlink_of_symlink()?;
        sleep(Duration::from_secs(5));
        drop(dir_symlink2);
        drop(txt_symlink2);
        Ok(())
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ファイルを管理する構造体を作る
2 participants