aboutsummaryrefslogtreecommitdiff
path: root/tests/path-entry/driver.cxx
blob: e4dddf3200e525c7b39cb869d4a4c88f891e4e50 (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
// file      : tests/path-entry/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <iostream>
#include <system_error>
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.utility;    // operator<<(ostream, exception)
import butl.filesystem;
#else
#include <libbutl/utility.mxx>
#include <libbutl/filesystem.mxx>
#endif

using namespace std;
using namespace butl;

// Usage: argv[0] [-l] <path>
//
// If path entry exists then print it's type and size (meaningful for the
// regular file only) to STDOUT, and exit with the zero code. Otherwise exit
// with the one code. Don't follow symlink. On failure print the error
// description to STDERR and exit with the two code.
//
// -l
//    Follow symlinks.
//
int
main (int argc, const char* argv[])
try
{
  bool follow_symlinks (false);

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

    if (v == "-l")
      follow_symlinks = true;
    else
      break;
  }

  assert (i == argc - 1);

  auto es (path_entry (argv[i], follow_symlinks));

  if (!es.first)
    return 1;

  switch (es.second.type)
  {
  case entry_type::unknown: cout << "unknown"; break;
  case entry_type::regular: cout << "regular"; break;
  case entry_type::directory: cout << "directory"; break;
  case entry_type::symlink: cout << "symlink"; break;
  case entry_type::other: cout << "other"; break;
  }

  cout << endl << es.second.size << endl;
  return 0;
}
catch (const system_error& e)
{
  cerr << e << endl;
  return 2;
}