xref: /aosp_15_r20/external/libbrillo/brillo/files/scoped_dir.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2019 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_FILES_SCOPED_DIR_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_FILES_SCOPED_DIR_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <dirent.h>
9*1a96fba6SXin Li 
10*1a96fba6SXin Li #include <base/scoped_generic.h>
11*1a96fba6SXin Li 
12*1a96fba6SXin Li #define HANDLE_EINTR_IF_EQ(x, val)                             \
13*1a96fba6SXin Li   ({                                                           \
14*1a96fba6SXin Li     decltype(x) eintr_wrapper_result;                          \
15*1a96fba6SXin Li     do {                                                       \
16*1a96fba6SXin Li       eintr_wrapper_result = (x);                              \
17*1a96fba6SXin Li     } while (eintr_wrapper_result == (val) && errno == EINTR); \
18*1a96fba6SXin Li     eintr_wrapper_result;                                      \
19*1a96fba6SXin Li   })
20*1a96fba6SXin Li 
21*1a96fba6SXin Li namespace brillo {
22*1a96fba6SXin Li 
23*1a96fba6SXin Li struct ScopedDIRCloseTraits {
InvalidValueScopedDIRCloseTraits24*1a96fba6SXin Li   static DIR* InvalidValue() { return nullptr; }
FreeScopedDIRCloseTraits25*1a96fba6SXin Li   static void Free(DIR* dir) {
26*1a96fba6SXin Li     if (dir != nullptr) {
27*1a96fba6SXin Li       closedir(dir);
28*1a96fba6SXin Li     }
29*1a96fba6SXin Li   }
30*1a96fba6SXin Li };
31*1a96fba6SXin Li 
32*1a96fba6SXin Li typedef base::ScopedGeneric<DIR*, ScopedDIRCloseTraits> ScopedDIR;
33*1a96fba6SXin Li 
34*1a96fba6SXin Li }  // namespace brillo
35*1a96fba6SXin Li 
36*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_FILES_SCOPED_DIR_H_
37