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

feat: executable command #1872

Closed
wants to merge 1 commit into from
Closed

feat: executable command #1872

wants to merge 1 commit into from

Conversation

FroyaTheHen
Copy link
Contributor

@FroyaTheHen FroyaTheHen commented Jan 8, 2025

No description provided.

@FroyaTheHen FroyaTheHen requested a review from a team as a code owner January 8, 2025 10:47
@FroyaTheHen FroyaTheHen force-pushed the spr/main/c985abe1 branch 2 times, most recently from 2d0d616 to 8e3458e Compare January 9, 2025 09:35
github-merge-queue bot pushed a commit that referenced this pull request Jan 10, 2025
commit-id:12d49314

---

**Stack**:
- #1872
- #1871⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do
not merge manually using the UI - doing so may have unexpected results.*
Base automatically changed from spr/main/12d49314 to main January 10, 2025 07:15
extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
extensions/scarb-cairo-execute/src/main.rs Show resolved Hide resolved
extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
Comment on lines 185 to 186
if !filepath.exists() {
fs::File::create(&filepath)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not atomic, as we first ask OS whether the file exists or not and only then attempt creating it. It's prone to a race condition, as another process might have been scheduled by the OS between call to file metadata (exists) and call to create it.
Instead, please do:

        let file = OpenOptions::new().create_new(true).open(path.as_std_path());
        return match file {
            Err(e) => {
                if e.kind() == std::io::ErrorKind::AlreadyExists {
                    continue;
                }
                Err(e.into())
            }
            Ok(_) => Ok(filepath),
        };

.create_new(true) ensures new file will be created, or else AlreadyExists thrown.
If the file has not been created because of some error, other than already exist (e.g. running out of disk space) we can show the error to the user.

Similar approach can be used for directories:

        let dir = fs::create_dir(&filepath);
        return match dir {
            Err(e) => {
                if e.kind() == std::io::ErrorKind::AlreadyExists {
                    continue;
                }
                Err(e.into())
            }
            Ok(_) => Ok(filepath),

extensions/scarb-cairo-execute/src/main.rs Outdated Show resolved Hide resolved
commit-id:c985abe1
let absolute = |path_buf: PathBuf| {
path_buf
.as_path()
.canonicalize()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cargo clippy warns agains using canonicalize(), should i use fsx::canonicalize_utf8 does it apply in this case? please advise

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you should. fsx is basically our wrapper over path manipulation functions that handles some edge-cases that we have run into in the past. The clippy lint you see has been added by us here

disallowed-methods = [

@maciektr
Copy link
Contributor

Replaced with #1885

@maciektr maciektr closed this Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants