iocore 3.1.0

IOCore is a safe library for unix CLI tools and Systems programming. IOCore provides the [`iocore::Path`] abstraction of file-system paths designed to replace most [`std::path`] and [`std::fs`] operations with practical methods, other abstractions include: - handling file-system permissions via [`iocore::PathPermissions`] powered by the crate [`trilobyte`]. - handling file-system timestamps via [`iocore::PathTimestamps`] granularly via [`iocore::PathDateTime`]. IOCore provides the [`iocore::walk_dir`] function and its companion trait [`iocore::WalkProgressHandler`] which traverses file-systems quickly via threads. IOcore provides [`iocore::User`] which provides unix user information such as uid, path to home etc. The module [`iocore::env`] provides [`iocore::env:args`] returns a [`Vec<String>`] from [`std::env:args`], and [`iocore::env:var`] that returns environment variables as string.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::io::Write;
use std::os::unix::fs::MetadataExt;
use std::path::MAIN_SEPARATOR_STR;
use k9::assert_equal;

use iocore::{Error, Path, PathDateTime, PathPermissions, PathStatus, PathType, Result};
use iocore_test::{
    current_source_file, folder_path, path_to_test_directory, path_to_test_file,
    path_to_test_folder, seq_bytes,
};
use trilobyte::TriloByte;

#[test]
fn test_path_join() -> Result<()> {
    let folder = Path::new("folder");
    assert_equal!(folder.to_string(), "folder");
    assert_equal!(folder.join("a"), Path::new("folder/a"));
    assert_equal!(folder.join("a").to_string(), "folder/a");
    assert_equal!(folder.join("a/b"), Path::new("folder/a/b"));
    assert_equal!(folder.join("a").join("b"), Path::new("folder/a/b"));
    assert_equal!(folder.join("/a"), Path::new("/a"));
    Ok(())
}


#[test]
fn test_split_extension() -> Result<()> {
    let path = Path::new("/foo/baz.txt");
    assert_equal!(path.split_extension(), ("baz".to_string(), Some("txt".to_string())));
    Ok(())
}

#[test]
fn test_join_extension() -> Result<()> {
    let path = Path::join_extension("baz".to_string(), Some("txt".to_string()));
    assert_equal!(path, "baz.txt");
    Ok(())
}

#[test]
fn test_abbreviate() -> Result<()> {
    let cargo_path = Path::raw(iocore::USER.home()?).join(".cargo");

    assert_equal!(cargo_path.to_string().starts_with("~"), false);
    assert_equal!(cargo_path.abbreviate().to_string(), "~/.cargo");

    assert_equal!(Path::new(cargo_path.abbreviate().to_string()).to_string(), cargo_path.to_string());
    Ok(())
}

#[test]
fn test_path_path() -> Result<()> {
    let test_path = Path::raw(current_source_file!()).relative_to_cwd();
    let mut pathbuf = std::path::PathBuf::new();

    pathbuf.push("tests");
    pathbuf.push("test_path.rs");
    assert_equal!(test_path.path(), pathbuf.as_path());
    Ok(())
}

#[test]
fn test_path_contains() -> Result<()> {
    let test_path = Path::raw(current_source_file!());
    assert!(test_path.contains("test_path.rs"));
    assert!(test_path.contains("tests/test_path.rs"));
    assert!(test_path.contains("sts/test_path.rs"));
    assert!(test_path.contains("_path.rs"));
    Ok(())
}

#[cfg(target_os = "macos")]
#[test]
fn test_path_safe() -> Result<()> {
    let long_name = (0..64).map(|_| "noon".to_string()).collect::<String>();
    assert_equal!(
        Path::safe(long_name),
        Err(Error::FileSystemError("path too long in macos: \"noonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoonnoon\" [iocore::fs::Path::safe:[crates/src/src/fs.rs:89]]\n".to_string()))
    );
    let long_path_with_short_names = (0..256)
        .map(|_| "noon".to_string())
        .collect::<Vec<String>>()
        .join(MAIN_SEPARATOR_STR);
    assert_equal!(
        Path::safe(&long_path_with_short_names),
        Ok(Path::raw(&long_path_with_short_names))
    );
    Ok(())
}

#[cfg(target_os = "linux")]
#[test]
fn test_path_safe() -> Result<()> {
    let path_string = (0..255)
        .map(|_| format!("path"))
        .collect::<Vec<String>>()
        .join(MAIN_SEPARATOR_STR);
    assert_equal!(
        Path::safe(path_string),
        Err(Error::FileSystemError(String::from(
            "iocore::fs::Path path too long in \"linux\": \"path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path/path\""
        )))
    );
    Ok(())
}

#[test]
fn test_path_from_path_buf() -> Result<()> {
    let mut pathbuf = std::path::PathBuf::new();
    pathbuf.push("/resolved");
    pathbuf.push("path");

    assert_equal!(Path::from_path_buf(&pathbuf), Path::raw("/resolved/path"));
    Ok(())
}

#[test]
fn test_path_from_std_path() -> Result<()> {
    let mut pathbuf = std::path::PathBuf::new();
    pathbuf.push("/resolved");
    pathbuf.push("path");
    let std_path = pathbuf.as_path();
    assert_equal!(Path::from_std_path(std_path), Path::raw("/resolved/path"));
    Ok(())
}

#[test]
fn test_path_inner_string() -> Result<()> {
    assert_equal!(Path::raw("string").inner_string(), String::from("string"));
    Ok(())
}

#[test]
fn test_path_with_filename() -> Result<()> {
    let path = Path::raw("path/with-filename.rs");
    assert_equal!(path.with_filename("with-filename.go"), Path::raw("path/with-filename.go"));
    Ok(())
}

#[test]
fn test_path_status() -> Result<()> {
    let file = path_to_test_file!("test_path_status_file").write(&[])?;
    let folder = folder_path!("test_path_status_folder").mkdir()?;
    assert_equal!(file.status(), PathStatus::WritableFile);
    assert_equal!(folder.status(), PathStatus::WritableDirectory);
    Ok(())
}

#[test]
fn test_path_create() -> Result<()> {
    let path = path_to_test_file!("test_path_create").write(&[])?;
    let mut created = path.create()?;
    created.write(b"resolved")?;
    assert_equal!(path.read()?, "resolved");
    Ok(())
}

#[test]
fn test_path_append() -> Result<()> {
    let path = path_to_test_file!("test_path_append").write(&[])?;
    let mut append = path.create()?;
    append.write(b"resolved")?;
    path.append(b"\nend")?;
    assert_equal!(path.read()?, "resolved\nend");

    let path = Path::tmp_file();
    path.append(b"data")?;
    assert_equal!(path.read()?, "data");
    Ok(())
}


#[test]
fn test_path_timestamps() -> Result<()> {
    let modified_path_datetime =
        PathDateTime::parse_from_str("2025-03-18T23:49:43.445802000Z", "%Y-%m-%dT%H:%M:%S.%fZ")?;
    let file_mode_640 = path_to_test_file!("test_path_timestamps.640")
        .write(&[])?
        .set_mode(0o640)?
        .set_modified_time(&modified_path_datetime)?;
    let timestamps = file_mode_640.timestamps()?;

    assert_equal!(&timestamps.path, &file_mode_640);
    if std::env::var("TZ").unwrap_or_default() == "UTC" {
        assert_equal!(format!("{}", timestamps.modified), "2025-03-18T23:49:43.445802000Z");
        assert_equal!(
            format!("{:#?}", timestamps.modified),
            "PathDateTime[2025-03-18T23:49:43.445802000Z]"
        );
    } else {
        assert_equal!(format!("{}", timestamps.modified), "2025-03-18T20:49:43.445802000-03:00");
        assert_equal!(
            format!("{:#?}", timestamps.modified),
            "PathDateTime[2025-03-18T20:49:43.445802000-03:00]"
        );
    }
    Ok(())
}

#[test]
fn test_path_timestamps_accessed() -> Result<()> {
    let file = path_to_test_file!("test_path_timestamps_accessed").write(&[])?;
    let timestamps = file.timestamps()?;

    assert_equal!(file.accessed(), Some(timestamps.accessed));
    Ok(())
}
#[test]
fn test_path_timestamps_created() -> Result<()> {
    let file = path_to_test_file!("test_path_timestamps_created").write(&[])?;
    let timestamps = file.timestamps()?;

    assert_equal!(file.created(), Some(timestamps.created));
    Ok(())
}
#[test]
fn test_path_timestamps_modified() -> Result<()> {
    let file = path_to_test_file!("test_path_timestamps_modified").write(&[])?;
    let timestamps = file.timestamps()?;

    assert_equal!(file.modified(), Some(timestamps.modified));
    Ok(())
}

#[test]
fn test_path_ordering() -> Result<()> {
    let mut paths = vec![
        folder_path!("test_path_ordering/a").mkdir()?,
        path_to_test_file!("test_path_ordering/a/a").write(&[])?,
        path_to_test_file!("test_path_ordering/a/b").write(&[])?,
        path_to_test_file!("test_path_ordering/a/c").write(&[])?,
        path_to_test_file!("test_path_ordering/a/d").write(&[])?,
        folder_path!("test_path_ordering/b").mkdir()?,
        path_to_test_file!("test_path_ordering/b/a").write(&[])?,
        path_to_test_file!("test_path_ordering/b/b").write(&[])?,
        path_to_test_file!("test_path_ordering/b/c").write(&[])?,
        path_to_test_file!("test_path_ordering/b/d").write(&[])?,
        folder_path!("test_path_ordering/c").mkdir()?,
        path_to_test_file!("test_path_ordering/c/a").write(&[])?,
        path_to_test_file!("test_path_ordering/c/b").write(&[])?,
        path_to_test_file!("test_path_ordering/c/c").write(&[])?,
        path_to_test_file!("test_path_ordering/c/d").write(&[])?,
        folder_path!("test_path_ordering/d").mkdir()?,
        path_to_test_file!("test_path_ordering/d/a").write(&[])?,
        path_to_test_file!("test_path_ordering/d/b").write(&[])?,
        path_to_test_file!("test_path_ordering/d/c").write(&[])?,
        path_to_test_file!("test_path_ordering/d/d").write(&[])?,
    ];
    paths.sort();

    assert_equal!(
        paths,
        vec![
            folder_path!("test_path_ordering/a"),
            folder_path!("test_path_ordering/b"),
            folder_path!("test_path_ordering/c"),
            folder_path!("test_path_ordering/d"),
            path_to_test_file!("test_path_ordering/a/a"),
            path_to_test_file!("test_path_ordering/a/b"),
            path_to_test_file!("test_path_ordering/a/c"),
            path_to_test_file!("test_path_ordering/a/d"),
            path_to_test_file!("test_path_ordering/b/a"),
            path_to_test_file!("test_path_ordering/b/b"),
            path_to_test_file!("test_path_ordering/b/c"),
            path_to_test_file!("test_path_ordering/b/d"),
            path_to_test_file!("test_path_ordering/c/a"),
            path_to_test_file!("test_path_ordering/c/b"),
            path_to_test_file!("test_path_ordering/c/c"),
            path_to_test_file!("test_path_ordering/c/d"),
            path_to_test_file!("test_path_ordering/d/a"),
            path_to_test_file!("test_path_ordering/d/b"),
            path_to_test_file!("test_path_ordering/d/c"),
            path_to_test_file!("test_path_ordering/d/d"),
        ]
    );

    Ok(())
}

#[test]
fn test_path_size() -> Result<()> {
    let path_a = path_to_test_file!("test_path_size/a").write(&seq_bytes(104))?;
    assert_equal!(path_a.size()?.as_u64(), 104);
    assert_equal!(path_a.size()?.to_string(), "104B");

    let path_b = path_to_test_file!("test_path_size/b").write(&seq_bytes(4096))?;
    assert_equal!(path_b.size()?.as_u64(), 4096);
    assert_equal!(path_b.size()?.to_string(), "4Kb");

    let path_c = path_to_test_file!("test_path_size/c").write(&seq_bytes(4194304))?;
    assert_equal!(path_c.size()?.to_string(), "4Mb");
    assert_equal!(path_c.size()?.as_u64(), 4194304);

    let mut sizes = vec![path_b.size()?, path_c.size()?, path_a.size()?];
    sizes.sort();
    assert_equal!(sizes, vec![path_a.size()?, path_b.size()?, path_c.size()?]);
    Ok(())
}

#[test]
fn test_path_file() -> Result<()> {
    let existing_file_path_string = path_to_test_file!("file").write_unchecked(&[]).to_string();

    assert!(Path::file(&existing_file_path_string).is_ok());
    Path::file(&existing_file_path_string)?.delete()?;
    assert!(Path::file(&existing_file_path_string).is_err());
    Ok(())
}

#[test]
fn test_path_directory() -> Result<()> {
    let existing_directory_path_string = path_to_test_folder!("folder").to_string();

    assert_equal!(
        Path::directory(&existing_directory_path_string),
        Ok(Path::new(&existing_directory_path_string))
    );
    Path::directory(&existing_directory_path_string)?.delete()?;
    assert_equal!(Path::directory(&Path::raw(existing_directory_path_string)).is_err(), true);

    Ok(())
}

#[test]
fn test_path_kind() -> Result<()> {
    let file = path_to_test_file!("test_path_kind_file").write_unchecked(&[]);
    let folder = folder_path!("test_path_kind_folder").mkdir_unchecked();
    assert_equal!(file.kind(), PathType::File);
    assert_equal!(folder.kind(), PathType::Directory);
    Ok(())
}


#[test]
fn test_file() -> Result<()> {
    let test_file = path_to_test_file!("a/b/c").write_unchecked(&[]);
    assert_equal!(Path::file(test_file.to_string()), Ok(test_file));
    Ok(())
}

#[test]
fn test_directory() -> Result<()> {
    let test_directory = path_to_test_directory!("a/b/c").mkdir()?;
    assert_equal!(Path::directory(test_directory.to_string()), Ok(test_directory));
    Ok(())
}

#[test]
fn test_path_tmp_file() -> Result<()> {
    let tmp = Path::tmp_file();
    assert_equal!(tmp.exists(), true);
    assert_equal!(tmp.is_file(), true);
    Ok(())
}

#[test]
fn test_path_tmp() -> Result<()> {
    let tmp = Path::tmp();
    assert_equal!(tmp.exists(), true);
    assert_equal!(tmp.is_directory(), true);
    Ok(())
}

#[test]
fn test_path_canonicalize() -> Result<()> {
    assert_equal!(
        Path::raw("~").canonicalize()?,
        Path::raw(iocore::USERS_PATH).join(&iocore::User::id()?.name)
    );
    assert_equal!(
        Path::raw(file!()).canonicalize()?.to_string().ends_with("test_path.rs"),
        true

    );
    Ok(())
}

#[test]
fn test_path_permissions() -> Result<()> {
    let file_mode_640 =
        path_to_test_file!("test_path_permissions.640").write(&[])?.set_mode(0o640)?;
    let metadata = std::fs::metadata(file_mode_640.path())?;

    assert_equal!(format!("{:o}", metadata.mode()), "100640");
    assert_equal!(
        PathPermissions::from_u32(metadata.mode())?,
        PathPermissions {
            user: TriloByte::from(0b0110),
            group: TriloByte::from(0b100),
            other: TriloByte::from(0b00),
        }
    );

    assert_equal!(file_mode_640.mode(), 0o640);
    assert_equal!(file_mode_640.permissions(), PathPermissions::from_u32(metadata.mode())?);

    assert_equal!(file_mode_640.readable(), true);
    assert_equal!(file_mode_640.writable(), true);
    assert_equal!(file_mode_640.executable(), false);
    assert_equal!(file_mode_640.permissions().readable(), true);
    assert_equal!(file_mode_640.permissions().writable(), true);
    assert_equal!(file_mode_640.permissions().executable(), false);

    assert_equal!(file_mode_640.permissions().user().writable(), true);
    assert_equal!(file_mode_640.permissions().user().readable(), true);
    assert_equal!(file_mode_640.permissions().user().executable(), false);

    assert_equal!(file_mode_640.permissions().group().writable(), false);
    assert_equal!(file_mode_640.permissions().group().readable(), true);
    assert_equal!(file_mode_640.permissions().group().executable(), false);

    assert_equal!(file_mode_640.permissions().other().writable(), false);
    assert_equal!(file_mode_640.permissions().other().readable(), false);
    assert_equal!(file_mode_640.permissions().other().executable(), false);
    Ok(())
}

#[test]
fn test_path_set_mode() -> Result<()> {
    let mut file = Path::tmp_file();
    file.set_mode(0o755)?;
    assert_equal!(format!("{:o}", file.mode()), "755");
    Ok(())
}
#[test]
fn test_path_set_permissions() -> Result<()> {
    let mut file = Path::tmp_file();

    file.set_permissions(&PathPermissions::from_u32(0o777)?)?;
    assert_equal!(format!("{:o}", file.mode()), "777");
    Ok(())
}

#[test]
fn test_expand_home() -> Result<()> {
    let path = Path::raw("~/.config/ps1.toml").try_canonicalize();
    assert_equal!(path.to_string(), concat!(env!("HOME"), "/.config/ps1.toml"));
    let path = Path::raw("~/foo.bar").try_canonicalize();
    assert_equal!(path.to_string(), concat!(env!("HOME"), "/foo.bar"));
    Ok(())
}

#[test]
fn test_path_rename() -> Result<()> {
    let to_folder = path_to_test_folder!("to");
    let from_file = Path::tmp_file().write(b"data")?;
    let to = to_folder.join("tmp");
    assert_equal!(to.is_file(), false);
    from_file.rename(&to, true)?;
    assert_equal!(to.is_file(), true);
    to_folder.delete()?;
    assert_equal!(to_folder.exists(), false);

    Ok(())
}