aboutsummaryrefslogtreecommitdiff
path: root/build/context.txx
blob: 53081b144fb5a719e17ed0d09b4e4f2d8fa91bdc (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
// file      : build/context.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <system_error>

#include <build/diagnostics>

namespace build
{
  template <typename T>
  fs_status<rmfile_status>
  rmfile (const path& f, const T& t)
  {
    // We don't want to print the command if we couldn't remove the
    // file because it does not exist (just like we don't print the
    // update command if the file is up to date). This makes the
    // below code a bit ugly.
    //
    rmfile_status rs;

    try
    {
      rs = try_rmfile (f);
    }
    catch (const std::system_error& e)
    {
      if (verb >= 1)
        text << "rm " << f.string ();
      else
        text << "rm " << t;

      fail << "unable to remove file " << f.string () << ": " << e.what ();
    }

    if (rs == rmfile_status::success)
    {
      if (verb >= 1)
        text << "rm " << f.string ();
      else
        text << "rm " << t;
    }

    return rs;
  }

  template <typename T>
  fs_status<rmdir_status>
  rmdir (const path& d, const T& t)
  {
    bool w (d == work); // Don't try to remove working directory.
    rmdir_status rs;

    // We don't want to print the command if we couldn't remove the
    // directory because it does not exist (just like we don't print
    // mkdir if it already exists) or if it is not empty. This makes
    // the below code a bit ugly.
    //
    try
    {
      rs = !w ? try_rmdir (d) : rmdir_status::not_empty;
    }
    catch (const std::system_error& e)
    {
      if (verb >= 1)
        text << "rmdir " << d.string ();
      else
        text << "rmdir " << t;

      fail << "unable to remove directory " << d.string () << ": "
           << e.what ();
    }

    switch (rs)
    {
    case rmdir_status::success:
      {
        if (verb >= 1)
          text << "rmdir " << d.string ();
        else
          text << "rmdir " << t;

        break;
      }
    case rmdir_status::not_empty:
      {
        if (verb >= 1)
          text << "directory " << d.string () << " is "
               << (w ? "current working directory" : "not empty")
               << ", not removing";

        break;
      }
    case rmdir_status::not_exist:
      break;
    }

    return rs;
  }
}