]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - io/utimes.c
xfs: convert to new timestamp accessors
[thirdparty/xfsprogs-dev.git] / io / utimes.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
8211d1b5
DD
2/*
3 * Copyright (c) 2016 Deepa Dinamani
4 * All Rights Reserved.
8211d1b5
DD
5 */
6
7#include "command.h"
8#include "input.h"
9#include "init.h"
10#include "io.h"
11
12static cmdinfo_t utimes_cmd;
13
14static void
15utimes_help(void)
16{
17 printf(_(
18"\n"
19" Update file atime and mtime of the current file with nansecond precision.\n"
20"\n"
21" Usage: utimes atime_sec atime_nsec mtime_sec mtime_nsec.\n"
22" *_sec: Seconds elapsed since 1970-01-01 00:00:00 UTC.\n"
23" *_nsec: Nanoseconds since the corresponding *_sec.\n"
24"\n"));
25}
26
27static int
28utimes_f(
29 int argc,
30 char **argv)
31{
32 struct timespec t[2];
33 int result;
34
35 /* Get the timestamps */
36 result = timespec_from_string(argv[1], argv[2], &t[0]);
37 if (result) {
38 fprintf(stderr, "Bad value for atime\n");
9e1595e6 39 exitcode = 1;
8211d1b5
DD
40 return 0;
41 }
42 result = timespec_from_string(argv[3], argv[4], &t[1]);
43 if (result) {
44 fprintf(stderr, "Bad value for mtime\n");
9e1595e6 45 exitcode = 1;
8211d1b5
DD
46 return 0;
47 }
48
49 /* Call futimens to update time. */
50 if (futimens(file->fd, t)) {
51 perror("futimens");
9e1595e6 52 exitcode = 1;
8211d1b5
DD
53 return 0;
54 }
55
56 return 0;
57}
58
59void
60utimes_init(void)
61{
62 utimes_cmd.name = "utimes";
63 utimes_cmd.cfunc = utimes_f;
64 utimes_cmd.argmin = 4;
65 utimes_cmd.argmax = 4;
66 utimes_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
67 utimes_cmd.args = _("atime_sec atime_nsec mtime_sec mtime_nsec");
68 utimes_cmd.oneline = _("Update file times of the current file");
69 utimes_cmd.help = utimes_help;
70
71 add_command(&utimes_cmd);
72}