From 275413fad65874efe6c81a2f0f2e2535a20f19f4 Mon Sep 17 00:00:00 2001 From: jiacai2050 Date: Thu, 14 Dec 2023 23:03:54 +0800 Subject: [PATCH] add 01-03 --- examples/src/01-03.zig | 39 +++++++++++++++++++++++++++ src/01-03-file-modified-24h-ago.md | 42 ++++++++++++++++++++++++++++++ src/SUMMARY.md | 1 + 3 files changed, 82 insertions(+) create mode 100644 examples/src/01-03.zig create mode 100644 src/01-03-file-modified-24h-ago.md diff --git a/examples/src/01-03.zig b/examples/src/01-03.zig new file mode 100644 index 0000000..4c2c813 --- /dev/null +++ b/examples/src/01-03.zig @@ -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, + }); + } + } +} diff --git a/src/01-03-file-modified-24h-ago.md b/src/01-03-file-modified-24h-ago.md new file mode 100644 index 0000000..c0f299d --- /dev/null +++ b/src/01-03-file-modified-24h-ago.md @@ -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, + }); + } + } +} + +``` diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 25da775..1b4acff 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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)