Inconsistent behavior of `mktemp` crate in Rust - rust

If I call .to_path_buf() immediately after expect, the temporary directory will not be created. Is this a bug or a Rust feature?
extern crate mktemp;
use std::path::Path;
fn main() {
let temp_dir = mktemp::Temp::new_dir().expect("Failed to create a temp directory");
let temp_dir_path = temp_dir.to_path_buf();
println!("tmp path exists: {}", Path::exists(&temp_dir_path));
let temp_dir_path = mktemp::Temp::new_dir().expect("Failed to create a temp directory").to_path_buf();
println!("tmp path exists: {}", Path::exists(&temp_dir_path));
}
Which outputs:
tmp path exists: true
tmp path exists: false

I don't know, but I wonder if there's something in the mktemp documentation about this...
Once the variable goes out of scope, the underlying file system resource is removed.
You're not storing the Temp in a variable, so it goes out of scope immediately. It's creating the directory and then immediately destroying it.

Related

Unable to override temporary file with `tempfile`

Getting an error when creating a tempfile for socket use.:
Error: Custom { kind: AlreadyExists, error: PathError { path: "/tmp", err: Custom { kind: AlreadyExists, error: "too many temporary files exist" } } }
use tempfile::{tempfile, Builder, NamedTempFile};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let file = Builder::new().prefix("testsock").rand_bytes(0).tempfile()?;
Ok(())
}
Is there any way I can override the file?
The two many files exist error occurs if the file it is trying to create already exists. You can solve this error by deleting the files in your temp dir. In your case this is probably in /tmp/testsock/, you may also need to remove the folder testsock if just deleting the files does not work.
The default temp file will be used if .tempfile()
However you can specify the tempfile if you use the following.
let tmp = Builder::new()
.prefix("example")
.rand_bytes(0)
.tempfile_in("./")?;
The documentation for the tempfile builder can be found:
https://docs.rs/tempfile/latest/tempfile/struct.Builder.html

What is the best way to check if an executable exists for `std::process::Command`?

I want to create a std::process::Command and check whether an executable actually exists at that location before actually spawning the Command later. What would be the pragmatic way to do this?
I have this code:
let c = std::process::Command::new("my_exe");
// something here
c.spawn().unwrap();
I want to be able to validate the my_exe path when creating the Command and then spawn way later.
check whether an executable actually exists at that location before actually spawning the Command.
Don't as that's a time-of-check to time-of-use race condition. Instead, check the error value from executing the Command: Check if a command is in PATH/executable as process.
If you want to check if a file is present in the working directory, you can use Path::exists, as described in How to check whether a path exists?.
Also, Command::spawn() takes a mutable reference to self so c needs to be mutable.
use std::path::Path;
use std::process::Command;
if Path::new("PATH_TO_EXECUTABLE").exists() {
let mut c = Command::new("PATH_TO_EXECUTABLE");
// do something
c.spawn().unwrap();
}
Use the is_executable crate:
use is_executable::IsExecutable;
use std::fs;
let paths = fs::read_dir("./").unwrap();
for path in paths {
let file_path = path.unwrap().path();
if file_path.is_executable() {
println!("File: {:?} is executable!", file_path);
} else {
println!("File: {:?} is _not_ executable!", file_path);
}
}

Is it possible to access current file name?

Is it possible to access current file name in Rust by
// main.rs
fn main() {
println!("filename: {}", FILE_NAME);
}
?
(This program should print filename: main.rs)
You can use the std::file macro to get the current source filename at the compile time.
let this_file = file!();
If you want to remove the path from the returned filename, you can construct a Path with it and call the file_name method.
let filename_only = Path::new(this_file).file_name().and_then(|s| s.to_str()).unwrap();
Playground

How to read file from another(nested) folder?

This is my folder structure.
src
├── main.rs
└── assembly_files
└── toyRISC.asm
Why is this not enough or where is the error?
I tried with raw &str:
let filename = "./assembly_files/toyRISC.asm";
let result = fs::read_to_string(path).expect("Please provide valid file name");
Also tried using Path and OsStr , shown in code below.
// main.rs
use std::{ffi::OsStr, fs, path::Path};
fn main() {
let filename = "assembly_files/toyRISC.asm";
let path = Path::new("./assembly_files/toyRISC.asm");
println!("{:?}", path);
let result = fs::read_to_string(path).expect("Please provide valid file name");
}
This is the error
thread 'main' panicked at 'Please provide valid file name: Os { code: 3, kind: NotFound, message: "The system cannot find the path specified."
}', src\main.rs:6:43
When you create a relative Path to a file, e.g. Path::new("./assembly_files/toyRISC.asm"); the path is relative to the folder from which you call the executable. Note that this isn't necessarily the folder where the executable is stored!
If you simply run cargo run at the root of your project to run it, then you need to change the path to Path::new("./src/assembly_files/toyRISC.asm");

How to create a folder outside the poject directory in rust

I using rust to create a folder in ~, however when my code runs the directory is created inside my project's folder instead in ~.
My code:
use std::fs::create_dir_all;
use std::path::Path;
fn main() {
let path = Path::new("~/.hidden_folder");
match create_dir_all(path) {
Ok(f) => {
println!("created folder")
},
Err(err) => {
println!("{:?}", err);
}
};
}
Any idea how to create the folder in the correct directory ?
If you want your home directory, I recommend using this crate or specifying the absolute path. If you want to save it in any other directories, just use relative or absolute paths, but don't use ~ because Rust doesn't know the context of ~.

Resources