Event ‣ set_times - chung-leong/zigar GitHub Wiki

Occurs when a system call is made to change the last-modified and last-accessed time of a file.

Usage

const std = @import("std");
pub const utimes = std.c.utimes;
import { __zigar, utimes } from './event-set_times-example-1.zig';
const { on } = __zigar;

on('set_times', (evt) => {
  console.log(evt);
  return true;
});

utimes('/var/mushroom/kingdom/mario.txt', [
  { sec: 5000, usec: 1234 },
  { sec: 8000, usec: 4567 },
]);
{
  parent: null,
  path: 'var/mushroom/kingdom/mario.txt',
  times: { atime: 5000001234000n, mtime: 8000004567000n },
  flags: { symlinkFollow: true }
}

Using a file descriptor:

const std = @import("std");
pub const utimes = std.c.futimes;

pub fn setTimes(path: [:0]const u8, times: ?*[2]std.c.timeval) !void {
    const fd = try std.posix.open(path, .{}, 0);
    defer std.posix.close(fd);
    if (std.c.futimes(fd, times) != 0) return error.UnableToSetTimes;
}
import { __zigar, setTimes } from './event-set_times-example-2.zig';
const { on } = __zigar;

on('open', (evt) => {
  return new Uint8Array(16);
});
on('set_times', (evt) => {
  console.log(evt);
  return true;
});

setTimes('/var/mushroom/kingdom/mario.txt', [
  { sec: 5000, usec: 1234 },
  { sec: 8000, usec: 4567 },
]);
{
  parent: null,
  path: 'var/mushroom/kingdom/mario.txt',
  target: Uint8Array(16) [
    0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0,
    0, 0, 0, 0
  ],
  times: { atime: 5000001234000n, mtime: 8000004567000n },
  flags: {}
}

Event fields:

  • parent: object
    The parent directory, if the event is triggered by a call to utimensat() or if the file descriptor was obtained using openat(). It's null when an absolute path is used to reference the file.
  • target: object The object previously returned by the open listener. Present only when event is triggered by a call to futimes() or futimens().
  • path: string
    Relative path to the file. No leading slash even when an absolute path is used.
  • times: { atime: bigint, mtime: bigint } The new last-accessed and last-modified time, in nanoseconds.
  • flags: { symlinkFollow: boolean }

Return value:

boolean | undefined
true if the operation succeeds or false otherwise. undefined means the listener declined to handle the event, allowing the operation to be handled by the actual file system.

Note:

The callback function can be async, provided that it's never invoked in the main thread.


Events