• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INCLUDE_PERFETTO_BASE_SCOPED_FILE_H_
18 #define INCLUDE_PERFETTO_BASE_SCOPED_FILE_H_
19 
20 #include "perfetto/base/build_config.h"
21 
22 #include <fcntl.h>
23 #include <stdio.h>
24 
25 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) && \
26     !PERFETTO_BUILDFLAG(PERFETTO_COMPILER_GCC)
27 #include <corecrt_io.h>
28 typedef int mode_t;
29 #else
30 #include <dirent.h>
31 #include <unistd.h>
32 #endif
33 
34 #include <string>
35 
36 #include "perfetto/base/logging.h"
37 
38 namespace perfetto {
39 namespace base {
40 
41 constexpr mode_t kInvalidMode = static_cast<mode_t>(-1);
42 
43 // RAII classes for auto-releasing fds and dirs.
44 template <typename T,
45           int (*CloseFunction)(T),
46           T InvalidValue,
47           bool CheckClose = true>
48 class ScopedResource {
49  public:
t_(t)50   explicit ScopedResource(T t = InvalidValue) : t_(t) {}
ScopedResource(ScopedResource && other)51   ScopedResource(ScopedResource&& other) noexcept {
52     t_ = other.t_;
53     other.t_ = InvalidValue;
54   }
55   ScopedResource& operator=(ScopedResource&& other) {
56     reset(other.t_);
57     other.t_ = InvalidValue;
58     return *this;
59   }
get()60   T get() const { return t_; }
61   T operator*() const { return t_; }
62   explicit operator bool() const { return t_ != InvalidValue; }
63   void reset(T r = InvalidValue) {
64     if (t_ != InvalidValue) {
65       int res = CloseFunction(t_);
66       if (CheckClose)
67         PERFETTO_CHECK(res == 0);
68     }
69     t_ = r;
70   }
release()71   T release() {
72     T t = t_;
73     t_ = InvalidValue;
74     return t;
75   }
~ScopedResource()76   ~ScopedResource() { reset(InvalidValue); }
77 
78  private:
79   ScopedResource(const ScopedResource&) = delete;
80   ScopedResource& operator=(const ScopedResource&) = delete;
81 
82   T t_;
83 };
84 
85 using ScopedFile = ScopedResource<int, close, -1>;
86 inline static ScopedFile OpenFile(const std::string& path,
87                                   int flags,
88                                   mode_t mode = kInvalidMode) {
89   PERFETTO_DCHECK((flags & O_CREAT) == 0 || mode != kInvalidMode);
90 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
91   ScopedFile fd(open(path.c_str(), flags, mode));
92 #else
93   // Always open a ScopedFile with O_CLOEXEC so we can safely fork and exec.
94   ScopedFile fd(open(path.c_str(), flags | O_CLOEXEC, mode));
95 #endif
96   return fd;
97 }
98 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
99 using ScopedDir = ScopedResource<DIR*, closedir, nullptr>;
100 #endif
101 
102 using ScopedFstream = ScopedResource<FILE*, fclose, nullptr>;
103 
104 }  // namespace base
105 }  // namespace perfetto
106 
107 #endif  // INCLUDE_PERFETTO_BASE_SCOPED_FILE_H_
108