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
|
// file : tests/fdstream/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <string>
#include <cassert>
#include <system_error>
#include <butl/path>
#include <butl/fdstream>
#include <butl/filesystem>
using namespace std;
using namespace butl;
static const string text1 ("ABCDEF\nXYZ");
static const string text2 ("12"); // Keep shorter than text1.
static const string text3 ("ABCDEF\r\nXYZ");
static string
from_file (const path& f, fdopen_mode m = fdopen_mode::none)
{
ifdstream ifs (fdopen (f, m | fdopen_mode::in));
ifs.exceptions (ifdstream::badbit);
string s;
getline (ifs, s, '\0');
return s;
}
static void
to_file (const path& f, const string& s, fdopen_mode m = fdopen_mode::none)
{
ofdstream ofs (fdopen (f, m | fdopen_mode::out));
ofs.exceptions (ofdstream::badbit | ofdstream::failbit);
ofs << s;
}
int
main ()
{
dir_path td (dir_path::temp_directory () / dir_path ("butl-fdstream"));
// 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_rmdir_r (td);
assert (try_mkdir (td) == mkdir_status::success);
path f (td / path ("file"));
try
{
fdopen (f, fdopen_mode::out); // fdopen_mode::create is missed.
assert (false);
}
catch (const system_error&)
{
}
// Read from the newly created empty file.
//
assert (from_file (f, fdopen_mode::create) == "");
assert (try_rmfile (f) == rmfile_status::success);
to_file (f, text1, fdopen_mode::create);
assert (from_file (f) == text1);
// Read from the file opened in R/W mode.
//
assert (from_file (f, fdopen_mode::out) == text1);
try
{
// Fail to create if the file already exists.
//
fdopen (
f, fdopen_mode::out | fdopen_mode::create | fdopen_mode::exclusive);
assert (false);
}
catch (const system_error&)
{
}
// Write text2 over text1.
//
to_file (f, text2);
string s (text2);
s += string (text1, text2.size ());
assert (from_file (f) == s);
// Truncate before reading.
//
assert (from_file (f, fdopen_mode::out | fdopen_mode::truncate) == "");
// Append to the file.
//
to_file (f, text1, fdopen_mode::truncate);
to_file (f, text2, fdopen_mode::append);
assert (from_file (f) == text1 + text2);
#ifndef _WIN32
// Fail for an existing symlink to unexistent file.
//
path link (td / path ("link"));
mksymlink (td / path ("unexistent"), link);
try
{
fdopen (
link, fdopen_mode::out | fdopen_mode::create | fdopen_mode::exclusive);
assert (false);
}
catch (const system_error&)
{
}
#else
// Check translation modes.
//
to_file (f, text1, fdopen_mode::truncate);
assert (from_file (f, fdopen_mode::binary) == text3);
to_file (f, text3, fdopen_mode::truncate | fdopen_mode::binary);
assert (from_file (f) == text1);
#endif
rmdir_r (td);
}
|