Skip to content

Commit 0083b13

Browse files
[feature] Implement file.pwd() (#1419)
* feat(eldritch-libfile): Add pwd method Added `pwd` method to `FileLibrary` trait and implementations. - `lib.rs`: Added `pwd` to trait definition. - `std.rs`: Implemented `pwd` using `std::env::current_dir`. - `fake.rs`: Implemented mock `pwd`. - Added unit test for `pwd`. * feat(eldritch-libfile): Add pwd method and update documentation Added `pwd` method to `FileLibrary` trait and implementations. - `lib.rs`: Added `pwd` to trait definition. - `std.rs`: Implemented `pwd` using `std::env::current_dir`. - `fake.rs`: Implemented mock `pwd`. - Added unit test for `pwd`. - Updated `eldritch.md` with documentation for `file.pwd`, labeled as `(V2-Only)`. * feat(eldritch-libfile): Add pwd method, update documentation and tests Added `pwd` method to `FileLibrary` trait and implementations. - `lib.rs`: Added `pwd` to trait definition. - `std.rs`: Implemented `pwd` using `std::env::current_dir`. - `fake.rs`: Implemented mock `pwd`. - Added unit test for `pwd`. - Updated `eldritch.md` with documentation for `file.pwd`, labeled as `(V2-Only)`. - Updated `bindings_test.rs` to verify `file.pwd` is exposed. * feat(eldritch-libfile): Add pwd method, update documentation and tests Added `pwd` method to `FileLibrary` trait and implementations. - `lib.rs`: Added `pwd` to trait definition. - `std.rs`: Implemented `pwd` using `std::env::current_dir`. - `fake.rs`: Implemented mock `pwd`. - Added unit test for `pwd`. - Updated `eldritch.md` with documentation for `file.pwd`, labeled as `(V2-Only)`. - Updated `bindings_test.rs` to verify `file.pwd` is exposed. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: KCarretto <Kcarretto@gmail.com>
1 parent 3c61bbc commit 0083b13

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

docs/_docs/user-guide/eldritch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,12 @@ The <b>file.moveto</b> method moves a file or directory from `src` to `dst`. If
455455

456456
The <b>file.parent_dir</b> method returns the parent directory of a give path. Eg `/etc/ssh/sshd_config` -> `/etc/ssh`
457457

458+
### file.pwd (V2-Only)
459+
460+
`file.pwd() -> Option<str>`
461+
462+
The <b>file.pwd</b> method returns the current working directory of the process. If it could not be determined, `None` is returned.
463+
458464
### file.read
459465

460466
`file.read(path: str) -> str`

implants/lib/eldritchv2/eldritchv2/src/bindings_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn test_file_bindings() {
6060
"mkdir",
6161
"move",
6262
"parent_dir",
63+
"pwd",
6364
"read",
6465
"read_binary",
6566
"remove",

implants/lib/eldritchv2/stdlib/eldritch-libfile/src/fake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ impl FileLibrary for FileLibraryFake {
253253
}
254254
}
255255

256+
fn pwd(&self) -> Result<Option<String>, String> {
257+
Ok(Some("/home/user".to_string()))
258+
}
259+
256260
fn remove(&self, path: String) -> Result<(), String> {
257261
let mut root = self.root.lock();
258262
let parts = Self::normalize_path(&path);

implants/lib/eldritchv2/stdlib/eldritch-libfile/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ pub trait FileLibrary {
223223
/// - Returns an error string if the file cannot be read.
224224
fn read_binary(&self, path: String) -> Result<Vec<u8>, String>;
225225

226+
#[eldritch_method]
227+
/// Returns the current working directory of the process.
228+
///
229+
/// **Returns**
230+
/// - `Option<str>`: The current working directory path, or None if it cannot be determined.
231+
fn pwd(&self) -> Result<Option<String>, String>;
232+
226233
#[eldritch_method]
227234
/// Deletes a file or directory recursively.
228235
///

implants/lib/eldritchv2/stdlib/eldritch-libfile/src/std.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ impl FileLibrary for StdFileLibrary {
124124
fs::read(&path).map_err(|e| format!("Failed to read file {path}: {e}"))
125125
}
126126

127+
fn pwd(&self) -> Result<Option<String>, String> {
128+
Ok(::std::env::current_dir()
129+
.ok()
130+
.map(|p| p.to_string_lossy().to_string()))
131+
}
132+
127133
fn remove(&self, path: String) -> Result<(), String> {
128134
let p = Path::new(&path);
129135
if p.is_dir() {
@@ -1346,4 +1352,14 @@ cb
13461352

13471353
Ok(())
13481354
}
1355+
1356+
#[test]
1357+
fn test_pwd() -> AnyhowResult<()> {
1358+
let lib = StdFileLibrary;
1359+
let pwd = lib.pwd().unwrap();
1360+
assert!(pwd.is_some());
1361+
let pwd = pwd.unwrap();
1362+
assert!(std::path::Path::new(&pwd).is_absolute());
1363+
Ok(())
1364+
}
13491365
}

0 commit comments

Comments
 (0)