Skip to content

Commit

Permalink
Merge pull request #4 from zigcc/0103
Browse files Browse the repository at this point in the history
add 01-03
  • Loading branch information
jiacai2050 authored Dec 14, 2023
2 parents 0d6e75f + 275413f commit b64d020
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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

0 comments on commit b64d020

Please sign in to comment.