Skip to content

Commit b64d020

Browse files
authored
Merge pull request #4 from zigcc/0103
add 01-03
2 parents 0d6e75f + 275413f commit b64d020

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

examples/src/01-03.zig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! File names that have been modified in the last 24 hours
2+
3+
const std = @import("std");
4+
const fs = std.fs;
5+
const print = std.debug.print;
6+
7+
pub fn main() !void {
8+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
9+
defer _ = gpa.deinit();
10+
const allocator = gpa.allocator();
11+
12+
var iter_dir = try fs.cwd().openIterableDir(".", .{
13+
.no_follow = true, // `true` means it won't dereference the symlinks.
14+
});
15+
defer iter_dir.close();
16+
17+
var walker = try iter_dir.walk(allocator);
18+
defer walker.deinit();
19+
20+
const now = std.time.nanoTimestamp();
21+
while (try walker.next()) |entry| {
22+
if (entry.kind != .file) {
23+
continue;
24+
}
25+
26+
const file = try iter_dir.dir.openFile(entry.path, .{});
27+
const md = try file.metadata();
28+
const last_modified = md.modified();
29+
const duration = now - last_modified;
30+
if (duration < std.time.ns_per_hour * 24) {
31+
print("Last modified: {d} seconds ago, read_only:{any}, size:{d} bytes, filename: {s}\n", .{
32+
@divTrunc(duration, std.time.ns_per_s),
33+
md.permissions().readOnly(),
34+
md.size(),
35+
entry.path,
36+
});
37+
}
38+
}
39+
}

src/01-03-file-modified-24h-ago.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# File names that have been modified in the last 24 hours
2+
3+
```zig
4+
const std = @import("std");
5+
const fs = std.fs;
6+
const print = std.debug.print;
7+
8+
pub fn main() !void {
9+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
10+
defer _ = gpa.deinit();
11+
const allocator = gpa.allocator();
12+
13+
var iter_dir = try fs.cwd().openIterableDir(".", .{
14+
.no_follow = true, // `true` means it won't dereference the symlinks.
15+
});
16+
defer iter_dir.close();
17+
18+
var walker = try iter_dir.walk(allocator);
19+
defer walker.deinit();
20+
21+
const now = std.time.nanoTimestamp();
22+
while (try walker.next()) |entry| {
23+
if (entry.kind != .file) {
24+
continue;
25+
}
26+
27+
const file = try iter_dir.dir.openFile(entry.path, .{});
28+
const md = try file.metadata();
29+
const last_modified = md.modified();
30+
const duration = now - last_modified;
31+
if (duration < std.time.ns_per_hour * 24) {
32+
print("Last modified: {d} seconds ago, read_only:{any}, size:{d} bytes, filename: {s}\n", .{
33+
@divTrunc(duration, std.time.ns_per_s),
34+
md.permissions().readOnly(),
35+
md.size(),
36+
entry.path,
37+
});
38+
}
39+
}
40+
}
41+
42+
```

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [File System]()
66
- [Read file line by line](./01-01-read-file-line-by-line.md)
77
- [Mmap file](./01-02-mmap-file.md)
8+
- [Find File modified in the last 24 hours](./01-03-file-modified-24h-ago.md)
89

910
- [Cryptography]()
1011
- [Calculate SHA-256 digest of a file](./02-01-sha-digest.md)

0 commit comments

Comments
 (0)