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
|
/* A handle to a filesystem location
(C) 2017 Niall Douglas <http://www.nedproductions.biz/> (20 commits)
File Created: Jul 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file Licence.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
#include "../../../path_handle.hpp"
#include "import.hpp"
LLFIO_V2_NAMESPACE_BEGIN
result<bool> path_handle::exists(path_view_type path) const noexcept {
try
{
LLFIO_LOG_FUNCTION_CALL(this);
path_view::c_str<> zpath(path, path_view::zero_terminated);
int x = ::faccessat(native_handle().fd, zpath.buffer, F_OK, AT_SYMLINK_NOFOLLOW);
if(x < 0)
{
auto ret = posix_error();
if(ret == errc::no_such_file_or_directory)
{
return false;
}
return failure(std::move(ret));
}
return (x == F_OK);
}
catch(...)
{
return error_from_exception();
}
}
result<path_handle> path_handle::path(const path_handle &base, path_handle::path_view_type path) noexcept
{
result<path_handle> ret(in_place_type<path_handle>);
native_handle_type &nativeh = ret.value()._v;
LLFIO_LOG_FUNCTION_CALL(&ret);
nativeh.behaviour |= native_handle_type::disposition::directory;
nativeh.behaviour &= ~native_handle_type::disposition::seekable; // not seekable
int attribs = O_CLOEXEC | O_RDONLY;
#ifdef O_DIRECTORY
attribs |= O_DIRECTORY;
#endif
#ifdef O_PATH
// Linux provides this extension opening a super light weight fd to just an anchor on the filing system
attribs |= O_PATH;
#endif
path_view::c_str<> zpath(path, path_view::zero_terminated);
if(base.is_valid())
{
nativeh.fd = ::openat(base.native_handle().fd, zpath.buffer, attribs);
}
else
{
nativeh.fd = ::open(zpath.buffer, attribs);
}
if(-1 == nativeh.fd)
{
return posix_error();
}
return ret;
}
LLFIO_V2_NAMESPACE_END
|