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