aboutsummaryrefslogtreecommitdiff
path: root/tests/link/driver.cxx
blob: 6b898f56a96bc7079a227a7434d188d01b57abaf (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// file      : tests/link/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_lib_modules_ts
#include <set>
#include <utility>      // pair
#include <iostream>     // cerr
#include <system_error>
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.path;
import butl.path_io;
import butl.utility;
import butl.fdstream;
import butl.filesystem;
#else
#include <libbutl/path.mxx>
#include <libbutl/path-io.mxx>
#include <libbutl/utility.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/filesystem.mxx>
#endif

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace butl;

static const char text[] = "ABCDEF";

enum class mklink
{
  sym,
  hard,
  any
};

static bool
link_file (const path& target, const path& link, mklink ml, bool check_content)
{
  try
  {
    switch (ml)
    {
    case mklink::sym:  mksymlink  (target, link);                  break;
    case mklink::hard: mkhardlink (target, link);                  break;
    case mklink::any:  mkanylink  (target, link, true /* copy */); break;
    }

    pair<bool, entry_stat> es (path_entry (link));
    assert (es.first);

    if (es.second.type == entry_type::symlink && readsymlink (link) != target)
      return false;
  }
  catch (const system_error&)
  {
    //cerr << e << endl;
    return false;
  }
  catch (const pair<entry_type, system_error>&)
  {
    //cerr << e.second << endl;
    return false;
  }

  if (!check_content)
    return true;

  string s;
  ifdstream ifs (link);
  ifs >> s;
  ifs.close (); // Not to miss failed close of the underlying file descriptor.
  return s == text;
}

static bool
link_dir (const dir_path& target,
          const dir_path& link,
          bool hard,
          bool check_content)
{
  try
  {
    if (hard)
      mkhardlink (target, link);
    else
      mksymlink (target, link);

    pair<bool, entry_stat> es (path_entry (link));
    assert (es.first);

    if (es.second.type == entry_type::symlink)
      readsymlink (link); // // Check for errors.
  }
  catch (const system_error&)
  {
    //cerr << e << endl;
    return false;
  }

  {
    auto pe (path_entry (link, false /* follow_symlinks */));
    assert (pe.first && pe.second.type == entry_type::symlink);
  }

  {
    auto pe (path_entry (link, true /* follow_symlinks */));
    assert (!pe.first || pe.second.type == entry_type::directory);
  }

  if (!check_content)
    return true;

  dir_path tp (target.absolute () ? target : link.directory () / target);

  set<pair<entry_type, path>> te;
  for (const dir_entry& de: dir_iterator (tp, false /* ignore_dangling */))
    te.emplace (de.ltype (), de.path ());

  set<pair<entry_type, path>> le;
  for (const dir_entry& de: dir_iterator (link, false /* ignore_dangling */))
    le.emplace (de.ltype (), de.path ());

  return te == le;
}

// Usages:
//
// argv[0]
// argv[0] -s <target> <link>
// argv[0] -f <path>
//
// In the first form run the basic symbolic and hard link tests.
//
// In the second form create a symlink. On error print the diagnostics to
// stderr and exit with the one code.
//
// In the third form follow symlinks and print the resulting target path to
// stdout. On error print the diagnostics to stderr and exit with the one
// code.
//
int
main (int argc, const char* argv[])
{
  bool create_symlink  (false);
  bool follow_symlinks (false);

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

    if (v == "-s")
      create_symlink = true;
    else if (v == "-f")
      follow_symlinks = true;
    else
      break;
  }

  if (create_symlink)
  {
    assert (!follow_symlinks);
    assert (i + 2 == argc);

    try
    {
      path t (argv[i]);
      path l (argv[i + 1]);

      bool dir (dir_exists (t.relative () ? l.directory () / t : t));
      mksymlink (t, l, dir);

      path lt (readsymlink (l));

      // The targets may only differ for Windows directory junctions.
      //
      assert (lt == t || dir);

      return 0;
    }
    catch (const system_error& e)
    {
      cerr << "error: " << e << endl;
      return 1;
    }
  }
  else if (follow_symlinks)
  {
    assert (!create_symlink);
    assert (i + 1 == argc);

    try
    {
      cout << try_followsymlink (path (argv[i])).first << endl;
      return 0;
    }
    catch (const system_error& e)
    {
      cerr << "error: " << e << endl;
      return 1;
    }
  }
  else
    assert (i == argc);

  dir_path td (dir_path::temp_directory () / dir_path ("butl-link"));

  // Recreate the temporary directory (that possibly exists from the previous
  // faulty run) for the test files. Delete the directory only if the test
  // succeeds to simplify the failure research.
  //
  try
  {
    try_rmdir_r (td);
  }
  catch (const system_error& e)
  {
    cerr << "unable to remove " << td << ": " << e << endl;
    return 1;
  }

  assert (try_mkdir (td) == mkdir_status::success);

  // Prepare the target file.
  //
  path fn ("target");
  path fp (td / fn);

  {
    ofdstream ofs (fp);
    ofs << text;
    ofs.close ();
  }

  // Create the file hard link.
  //
  assert (link_file (fp, td / path ("hlink"), mklink::hard, true));

#ifndef _WIN32
  // Create the file symlink using an absolute path.
  //
  assert (link_file (fp, td / path ("slink"), mklink::sym, true));

  // Create the file symlink using a relative path.
  //
  assert (link_file (fn, td / path ("rslink"), mklink::sym, true));

  // Create the file symlink using an unexistent file path.
  //
  assert (link_file (fp + "-a", td / path ("sa"), mklink::sym, false));
#endif

  // Create the file any link.
  //
  assert (link_file (fp, td / path ("alink"), mklink::any, true));

  // Prepare the target directory.
  //
  dir_path dn ("dir");
  dir_path dp (td / dn);

  assert (try_mkdir (dp) == mkdir_status::success);

  {
    ofdstream ofs (dp / path ("f"));
    ofs << text;
    ofs.close ();
  }

#ifndef _WIN32
  assert (link_file (fp, dp / path ("hlink"), mklink::hard, true));
  assert (link_file (fp, dp / path ("slink"), mklink::sym, true));
#endif

  // Create the directory symlink using an absolute path.
  //
  dir_path ld (td / dir_path ("dslink"));
  assert (link_dir (dp, ld, false /* hard */, true /* check_content */));

  // Create the symlink to a directory symlink using an absolute path.
  //
  dir_path lld (td / dir_path ("dslinkslink"));
  assert (link_dir (ld, lld, false /* hard */, true /* check_content */));

  {
    pair<bool, entry_stat> pe (path_entry (ld / "f"));
    assert (pe.first && pe.second.type == entry_type::regular);
  }

  {
    pair<bool, entry_stat> pe (path_entry (lld / "f"));
    assert (pe.first && pe.second.type == entry_type::regular);
  }

  {
    pair<bool, entry_stat> pe (path_entry (ld));
    assert (pe.first && pe.second.type == entry_type::symlink);
  }

  {
    pair<bool, entry_stat> pe (path_entry (ld, true /* follow_symlinks */));
    assert (pe.first && pe.second.type == entry_type::directory);
  }

  {
    pair<bool, entry_stat> pe (path_entry (lld));
    assert (pe.first && pe.second.type == entry_type::symlink);
  }

  {
    pair<bool, entry_stat> pe (path_entry (lld, true /* follow_symlinks */));
    assert (pe.first && pe.second.type == entry_type::directory);
  }

  for (const dir_entry& de: dir_iterator (td, false /* ignore_dangling */))
  {
    assert (de.path () != path ("dslink") ||
            (de.type () == entry_type::directory &&
             de.ltype () == entry_type::symlink));

    assert (de.path () != path ("dslinkslink") ||
            (de.type () == entry_type::directory &&
             de.ltype () == entry_type::symlink));
  }

  // Remove the directory symlink and make sure the target's content still
  // exists.
  //
  assert (try_rmsymlink (lld) == rmfile_status::success);
  assert (try_rmsymlink (ld) == rmfile_status::success);

  {
    pair<bool, entry_stat> pe (path_entry (dp / "f"));
    assert (pe.first && pe.second.type == entry_type::regular);
  }

#ifndef _WIN32
  // Create the directory symlink using an unexistent directory path.
  //
  assert (link_dir (dp / dir_path ("a"), td / dir_path ("dsa"), false, false));

  // Create the directory symlink using a relative path.
  //
  assert (link_dir (dn, td / dir_path ("rdslink"), false, true));
#endif

  // Delete the junction target and verify the junction entry status.
  //
  assert (link_dir (dp, ld, false /* hard */, true /* check_content */));
  rmdir_r (dp);

  {
    pair<bool, entry_stat> pe (path_entry (ld));
    assert (pe.first && pe.second.type == entry_type::symlink);
  }

  {
    pair<bool, entry_stat> pe (path_entry (ld, true /* follow_symlinks */));
    assert (!pe.first);
  }

  assert (try_rmsymlink (ld) == rmfile_status::success);

  // Try to create a dangling regular file symlink and make sure it is
  // properly removed via its parent recursive removal.
  //
  assert (try_mkdir (dp) == mkdir_status::success);

  // Note that on Windows regular file symlinks may not be supported (see
  // mksymlink() for details), so the following tests are allowed to fail
  // with ENOSYS on Windows.
  //
  try
  {
    mksymlink (dp / "non-existing", dp / "lnk");
    assert (!dir_empty (dp));
    assert (dir_iterator (dp, true /* ignore_dangling */) == dir_iterator ());
  }
  catch (const system_error& e)
  {
#ifndef _WIN32
    assert (false);
#else
    assert (e.code ().category () == generic_category () &&
            e.code ().value () == ENOSYS);
#endif
  }

  rmdir_r (dp);

  // Create a dangling directory symlink and make sure it is properly removed
  // via its parent recursive removal. Also make sure that removing directory
  // symlink keeps its target intact.
  //
  assert (try_mkdir (dp) == mkdir_status::success);

  dir_path tgd (td / dir_path ("tdir"));
  assert (try_mkdir (tgd) == mkdir_status::success);

  mksymlink  (dp / "non-existing", dp / "lnk1", true /* dir */);
  assert (!dir_empty (dp));
  assert (dir_iterator (dp, true /* ignore_dangling */) == dir_iterator ());

  mksymlink  (tgd, dp / "lnk2", true /* dir */);
  assert (dir_iterator (dp, true /* ignore_dangling */) != dir_iterator ());

  rmdir_r (dp);
  assert (dir_exists (tgd));

  try
  {
    rmdir_r (td);
  }
  catch (const system_error& e)
  {
    cerr << "unable to remove " << td << ": " << e << endl;
    return 1;
  }
}