• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SCOPED_UMASK_H_
6 #define LIBBRILLO_BRILLO_SCOPED_UMASK_H_
7 
8 #include <sys/types.h>
9 
10 #include <base/macros.h>
11 #include <brillo/brillo_export.h>
12 
13 namespace brillo {
14 
15 // ScopedUmask is a helper class for temporarily setting the umask before a
16 // set of operations. umask(2) is never expected to fail.
17 class BRILLO_EXPORT ScopedUmask {
18  public:
19   explicit ScopedUmask(mode_t new_umask);
20   ~ScopedUmask();
21 
22  private:
23   mode_t saved_umask_;
24 
25   // Avoid reusing ScopedUmask for multiple masks. DISALLOW_COPY_AND_ASSIGN
26   // deletes the copy constructor and operator=, but there are other situations
27   // where reassigning a new ScopedUmask to an existing ScopedUmask object
28   // is problematic:
29   //
30   // /* starting umask: default_value
31   // auto a = std::make_unique<ScopedUmask>(first_value);
32   // ... code here ...
33   // a.reset(ScopedUmask(new_value));
34   //
35   // Here, the order of destruction of the old object and the construction of
36   // the new object is inverted. The recommended usage would be:
37   //
38   // {
39   //    ScopedUmask a(old_value);
40   //    ... code here ...
41   // }
42   //
43   // {
44   //    ScopedUmask a(new_value);
45   //    ... code here ...
46   // }
47   DISALLOW_COPY_AND_ASSIGN(ScopedUmask);
48 };
49 
50 }  // namespace brillo
51 
52 #endif  // LIBBRILLO_BRILLO_SCOPED_UMASK_H_
53