aboutsummaryrefslogtreecommitdiff
path: root/tests/entry-time/driver.cxx
blob: 98f0bd7490a4ae49d057b724dda5e1385ad45b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// file      : tests/entry-time/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules
#include <string>
#include <chrono>
#include <iostream>
#endif

// Other includes.

#ifdef __cpp_modules
#ifdef __cpp_lib_modules
import std.core;
import std.io;
#endif
import butl.path;
import butl.filesystem;

import butl.optional;     // @@ MOD Clang should not be necessary.
#else
#include <libbutl/path.mxx>
#include <libbutl/optional.mxx>
#include <libbutl/timestamp.mxx>
#include <libbutl/filesystem.mxx>
#endif

using namespace std;
using namespace butl;

// Usage: argv[0] (-p|-s <time>) (-f|-d) (-m|-a) <path>
//
// Prints or sets the modification or access time for the specified filesystem
// entry.
//
// -p
//    Print the filesystem entry time as as a number of milliseconds passed
//    since UNIX epoch.
//
// -s
//    Set the filesystem entry time that is represented as a number of
//    milliseconds passed since UNIX epoch.
//
// -f
//    The specified path is a file.
//
// -d
//    The specified path is a directory.
//
// -m
//    Print or set the filesystem entry modification time.
//
// -a
//    Print or set the filesystem entry access time.
//
int
main (int argc, const char* argv[])
{
  using butl::optional;

  optional<bool> set;
  optional<bool> dir;
  optional<bool> mod;
  optional<path> p;
  timestamp tm;

  assert (argc > 0);

  for (int i (1); i != argc; ++i)
  {
    string v (argv[i]);

    if (v == "-p")
    {
      assert (!set);
      set = false;
    }
    else if (v == "-s")
    {
      assert (!set);
      set = true;

      assert (i + 1 != argc);
      tm = timestamp (chrono::duration_cast<duration> (
                        chrono::milliseconds (stoull (argv[++i]))));
    }
    else if (v == "-f")
    {
      assert (!dir);
      dir = false;
    }
    else if (v == "-d")
    {
      assert (!dir);
      dir = true;
    }
    else if (v == "-m")
    {
      assert (!mod);
      mod = true;
    }
    else if (v == "-a")
    {
      assert (!mod);
      mod = false;
    }
    else
    {
      assert (set && dir && mod && !p);
      p = path (move (v));
    }
  }

  assert (set);
  assert (dir);
  assert (mod);
  assert (p);

  if (*set)
  {
    if (*dir)
    {
      if (*mod)
        dir_mtime (path_cast<dir_path> (*p), tm);
      else
        dir_atime (path_cast<dir_path> (*p), tm);
    }
    else
    {
      if (*mod)
        file_mtime (*p, tm);
      else
        file_atime (*p, tm);
    }
  }
  else
  {
    if (*dir)
      tm = *mod
        ? dir_mtime (path_cast<dir_path> (*p))
        : dir_atime (path_cast<dir_path> (*p));
    else
      tm = *mod ? file_mtime (*p) : file_atime (*p);

    cout << chrono::duration_cast<chrono::milliseconds> (
      tm.time_since_epoch ()).count () << endl;
  }
}