aboutsummaryrefslogtreecommitdiff
path: root/butl/path.cxx
blob: 9325c1e641660b580efe97e709e9bf683108a01c (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// file      : butl/path.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <butl/path>

#ifdef _WIN32
#  include <butl/win32-utility>

#  include <stdlib.h>  // _MAX_PATH, _wgetenv()
#  include <direct.h>  // _[w]getcwd(), _[w]chdir()
#  include <shlobj.h>   // SHGetFolderPath*(), CSIDL_PROFILE
#  include <winerror.h> // SUCCEEDED()
#else
#  include <pwd.h>       // struct passwd, getpwuid_r()
#  include <errno.h>     // EINVAL
#  include <stdlib.h>    // mbstowcs(), wcstombs(), realpath(), getenv()
#  include <limits.h>    // PATH_MAX
#  include <unistd.h>    // getcwd(), chdir()
#  include <string.h>    // strlen(), strcpy()
#  include <sys/stat.h>  // stat(), S_IS*
#  include <sys/types.h> // stat

#  include <vector>
#endif

#include <atomic>
#include <cassert>
#include <system_error>

#include <butl/export>

#include <butl/process>

#ifndef _WIN32
#  ifndef PATH_MAX
#    define PATH_MAX 4096
#  endif
#endif

using namespace std;

#ifdef _WIN32
using namespace butl::win32;
#endif

namespace butl
{
  char const* invalid_path_base::
  what () const throw ()
  {
    return "invalid filesystem path";
  }

  //
  // char
  //

  template <>
  LIBBUTL_EXPORT path_traits<char>::string_type path_traits<char>::
  current ()
  {
#ifdef _WIN32
    char cwd[_MAX_PATH];
    if (_getcwd (cwd, _MAX_PATH) == 0)
      throw system_error (errno, system_category ());
#else
    char cwd[PATH_MAX];
    if (getcwd (cwd, PATH_MAX) == 0)
      throw system_error (errno, system_category ());
#endif

    return cwd;
  }

  template <>
  LIBBUTL_EXPORT void path_traits<char>::
  current (string_type const& s)
  {
#ifdef _WIN32
    if (_chdir (s.c_str ()) != 0)
      throw system_error (errno, system_category ());
#else
    if (chdir (s.c_str ()) != 0)
      throw system_error (errno, system_category ());
#endif
  }

#ifndef _WIN32
  static const char*
  temp_directory ()
  {
    const char* dir (nullptr);
    const char* env[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr};

    for (auto e (env); dir == nullptr && *e != nullptr; ++e)
      dir = getenv (*e);

    if (dir == nullptr)
      dir = "/tmp";

    struct stat s;
    if (stat (dir, &s) != 0)
      throw system_error (errno, system_category ());

    if (!S_ISDIR (s.st_mode))
      throw system_error (ENOTDIR, system_category ());

    return dir;
  }

  static string
  home ()
  {
    if (const char* h = getenv ("HOME"))
      return h;

    // Struct passwd has 5 members that will use this buffer. Two are the
    // home directory and shell paths. The other three are the user login
    // name, password, and real name (comment). We expect them to fit into
    // PATH_MAX * 2.
    //
    char buf[PATH_MAX * 4];

    passwd pw;
    passwd* rpw;

    int r (getpwuid_r (getuid (), &pw, buf, sizeof (buf), &rpw));
    if (r == -1)
      throw system_error (errno, system_category ());

    if (r == 0 && rpw == nullptr)
      // According to POSIX errno should be left unchanged if an entry is not
      // found.
      //
      throw system_error (ENOENT, system_category ());

    return pw.pw_dir;
  }
#endif

  template <>
  LIBBUTL_EXPORT path_traits<char>::string_type path_traits<char>::
  temp_directory ()
  {
#ifdef _WIN32
    char d[_MAX_PATH + 1];
    if (GetTempPathA (_MAX_PATH + 1, d) == 0)
    {
      string e (last_error_msg ());
      throw system_error (ENOTDIR, system_category (), e);
    }

    return d;
#else
    return butl::temp_directory ();
#endif
  }

  static atomic<size_t> temp_name_count;

  template <>
  LIBBUTL_EXPORT path_traits<char>::string_type path_traits<char>::
  temp_name (string_type const& prefix)
  {
    return prefix
      + "-" + to_string (process::current_id ())
      + "-" + to_string (temp_name_count++);
  }

  template <>
  LIBBUTL_EXPORT path_traits<char>::string_type path_traits<char>::
  home ()
  {
#ifndef _WIN32
    return butl::home ();
#else
    // Could be set by, e.g., MSYS and Cygwin shells.
    //
    if (const char* h = getenv ("HOME"))
      return h;

    char h[_MAX_PATH];
    HRESULT r (SHGetFolderPathA (NULL, CSIDL_PROFILE, NULL, 0, h));

    if (!SUCCEEDED (r))
    {
      string e (error_msg (r));
      throw system_error (ENOTDIR, system_category (), e);
    }

    return h;
#endif
  }

#ifndef _WIN32
  template <>
  LIBBUTL_EXPORT void path_traits<char>::
  realize (string_type& s)
  {
    char r[PATH_MAX];
    if (realpath (s.c_str (), r) == nullptr)
    {
      // @@ If there were a message in invalid_basic_path, we could have said
      // what exactly is wrong with the path.
      //
      if (errno == EACCES || errno == ENOENT || errno == ENOTDIR)
        throw invalid_basic_path<char> (s);
      else
        throw system_error (errno, system_category ());
    }

    s = r;
  }
#endif

  //
  // wchar_t
  //

  template <>
  LIBBUTL_EXPORT path_traits<wchar_t>::string_type path_traits<wchar_t>::
  current ()
  {
#ifdef _WIN32
    wchar_t wcwd[_MAX_PATH];
    if (_wgetcwd (wcwd, _MAX_PATH) == 0)
      throw system_error (errno, system_category ());
#else
    char cwd[PATH_MAX];
    if (getcwd (cwd, PATH_MAX) == 0)
      throw system_error (errno, system_category ());

    wchar_t wcwd[PATH_MAX];
    if (mbstowcs (wcwd, cwd, PATH_MAX) == size_type (-1))
      throw system_error (EINVAL, system_category ());
#endif

    return wcwd;
  }

  template <>
  LIBBUTL_EXPORT void path_traits<wchar_t>::
  current (string_type const& s)
  {
#ifdef _WIN32
    if (_wchdir (s.c_str ()) != 0)
      throw system_error (errno, system_category ());
#else
    char ns[PATH_MAX + 1];

    if (wcstombs (ns, s.c_str (), PATH_MAX) == size_type (-1))
      throw system_error (EINVAL, system_category ());

    ns[PATH_MAX] = '\0';

    if (chdir (ns) != 0)
      throw system_error (errno, system_category ());
#endif
  }

  template <>
  LIBBUTL_EXPORT path_traits<wchar_t>::string_type path_traits<wchar_t>::
  temp_directory ()
  {
#ifdef _WIN32
    wchar_t d[_MAX_PATH + 1];
    if (GetTempPathW (_MAX_PATH + 1, d) == 0)
    {
      string e (last_error_msg ());
      throw system_error (ENOTDIR, system_category (), e);
    }
#else
    wchar_t d[PATH_MAX];

    // The usage of mbstowcs() supposes the program's C-locale is set to the
    // proper locale before the call (can be done with setlocale(LC_ALL, "...")
    // call). Otherwise mbstowcs() fails with EILSEQ errno for non-ASCII
    // directory paths.
    //
    size_t r (mbstowcs (d, butl::temp_directory (), PATH_MAX));

    if (r == size_t (-1))
      throw system_error (EINVAL, system_category ());

    if (r == PATH_MAX)
      throw system_error (ENOTSUP, system_category ());
#endif

    return d;
  }

  template <>
  LIBBUTL_EXPORT path_traits<wchar_t>::string_type path_traits<wchar_t>::
  temp_name (string_type const& prefix)
  {
    return prefix +
      L"-" + to_wstring (process::current_id ()) +
      L"-" + to_wstring (temp_name_count++);
  }

  template <>
  LIBBUTL_EXPORT path_traits<wchar_t>::string_type path_traits<wchar_t>::
  home ()
  {
#ifndef _WIN32
    wchar_t d[PATH_MAX];
    size_t r (mbstowcs (d, butl::home ().c_str (), PATH_MAX));

    if (r == size_t (-1))
      throw system_error (EINVAL, system_category ());

    if (r == PATH_MAX)
      throw system_error (ENOTSUP, system_category ());

    return d;
#else
    // Could be set by, e.g., MSYS and Cygwin shells.
    //
    if (const wchar_t* h = _wgetenv (L"HOME"))
      return h;

    wchar_t h[_MAX_PATH];
    HRESULT r (SHGetFolderPathW (NULL, CSIDL_PROFILE, NULL, 0, h));

    if (!SUCCEEDED (r))
    {
      string e (error_msg (r));
      throw system_error (ENOTDIR, system_category (), e);
    }

    return h;
#endif
  }

#ifndef _WIN32
  template <>
  LIBBUTL_EXPORT void path_traits<wchar_t>::
  realize (string_type&)
  {
    assert (false); // Implement if/when needed.
  }
#endif
}