Apache Mesos
touch.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_TOUCH_HPP__
14 #define __STOUT_OS_TOUCH_HPP__
15 
16 #include <stout/nothing.hpp>
17 #include <stout/try.hpp>
18 
19 #include <stout/os/close.hpp>
20 #include <stout/os/exists.hpp>
21 #include <stout/os/int_fd.hpp>
22 #include <stout/os/open.hpp>
23 #include <stout/os/utime.hpp>
24 
25 #ifdef __WINDOWS__
26 #include <stout/windows.hpp>
27 #endif // __WINDOWS__
28 
29 
30 namespace os {
31 
32 inline Try<Nothing> touch(const std::string& path)
33 {
34  if (!os::exists(path)) {
35  Try<int_fd> fd = os::open(
36  path,
37  O_RDWR | O_CREAT,
39 
40  if (fd.isError()) {
41  return Error("Failed to open file: " + fd.error());
42  }
43 
44  return os::close(fd.get());
45  }
46 
47  // Update the access and modification times.
48  return os::utime(path);
49 }
50 
51 } // namespace os {
52 
53 
54 #endif // __STOUT_OS_TOUCH_HPP__
Try< Nothing > touch(const std::string &path)
Definition: touch.hpp:32
Definition: path.hpp:26
bool exists(const std::string &path)
Definition: exists.hpp:26
Definition: errorbase.hpp:36
const mode_t S_IRGRP
Definition: windows.hpp:313
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
const mode_t S_IWUSR
Definition: windows.hpp:306
Try< int_fd > open(const std::string &path, int oflag, mode_t mode=0)
Definition: open.hpp:35
Definition: posix_signalhandler.hpp:23
const mode_t S_IRUSR
Definition: windows.hpp:305
Try< Nothing > close(int fd)
Definition: close.hpp:24
Try< Nothing > utime(const std::string &path)
Definition: utime.hpp:32
static Try error(const E &e)
Definition: try.hpp:42
bool isError() const
Definition: try.hpp:71
const mode_t S_IROTH
Definition: windows.hpp:321