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

add 01-03 #4

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/src/01-03.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! File names that have been modified in the last 24 hours

const std = @import("std");
const fs = std.fs;
const print = std.debug.print;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var iter_dir = try fs.cwd().openIterableDir(".", .{
.no_follow = true, // `true` means it won't dereference the symlinks.
});
defer iter_dir.close();

var walker = try iter_dir.walk(allocator);
defer walker.deinit();

const now = std.time.nanoTimestamp();
while (try walker.next()) |entry| {
if (entry.kind != .file) {
continue;
}

const file = try iter_dir.dir.openFile(entry.path, .{});
const md = try file.metadata();
const last_modified = md.modified();
const duration = now - last_modified;
if (duration < std.time.ns_per_hour * 24) {
print("Last modified: {d} seconds ago, read_only:{any}, size:{d} bytes, filename: {s}\n", .{
@divTrunc(duration, std.time.ns_per_s),
md.permissions().readOnly(),
md.size(),
entry.path,
});
}
}
}
42 changes: 42 additions & 0 deletions src/01-03-file-modified-24h-ago.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# File names that have been modified in the last 24 hours

```zig
const std = @import("std");
const fs = std.fs;
const print = std.debug.print;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var iter_dir = try fs.cwd().openIterableDir(".", .{
.no_follow = true, // `true` means it won't dereference the symlinks.
});
defer iter_dir.close();

var walker = try iter_dir.walk(allocator);
defer walker.deinit();

const now = std.time.nanoTimestamp();
while (try walker.next()) |entry| {
if (entry.kind != .file) {
continue;
}

const file = try iter_dir.dir.openFile(entry.path, .{});
const md = try file.metadata();
const last_modified = md.modified();
const duration = now - last_modified;
if (duration < std.time.ns_per_hour * 24) {
print("Last modified: {d} seconds ago, read_only:{any}, size:{d} bytes, filename: {s}\n", .{
@divTrunc(duration, std.time.ns_per_s),
md.permissions().readOnly(),
md.size(),
entry.path,
});
}
}
}

```
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [File System]()
- [Read file line by line](./01-01-read-file-line-by-line.md)
- [Mmap file](./01-02-mmap-file.md)
- [Find File modified in the last 24 hours](./01-03-file-modified-24h-ago.md)

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