• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IORAP_SRC_INODE2FILENAME_INODE_RESULT_H_
16 #define IORAP_SRC_INODE2FILENAME_INODE_RESULT_H_
17 
18 #include "common/expected.h"
19 #include "inode2filename/inode.h"
20 #include "inode2filename/inode_result.h"
21 #include "inode2filename/system_call.h"
22 
23 #include <rxcpp/rx.hpp>
24 
25 #include <memory>
26 #include <optional>
27 #include <string>
28 #include <vector>
29 namespace iorap::inode2filename {
30 
31 // Tuple of (Inode -> (Filename|Errno))
32 struct InodeResult {
33   // We set this error when all root directories have been searched and
34   // yet we still could not find a corresponding filename for the inode under search.
35   static constexpr int kCouldNotFindFilename = ENOKEY;
36 
37   // An initial inode->filename mapping was found, but subsequent verification for it failed.
38   static constexpr int kVerificationFailed = EKEYEXPIRED;
39 
40   // There is always an inode, but sometimes we may fail to resolve the filename.
41   Inode inode;
42   // Value: Contains the filename (with a root directory as a prefix).
43   // Error: Contains the errno, usually one of the above, otherwise some system error.
44   iorap::expected<std::string /*filename*/, int /*errno*/> data;
45 
makeSuccessInodeResult46   static InodeResult makeSuccess(Inode inode, std::string filename) {
47     return InodeResult{inode, std::move(filename)};
48   }
49 
makeFailureInodeResult50   static InodeResult makeFailure(Inode inode, int err_no) {
51     return InodeResult{inode, iorap::unexpected{err_no}};
52   }
53 
54   constexpr explicit operator bool() const {
55     return data.has_value();
56   }
57 
58   constexpr bool operator==(const InodeResult& other) const {
59     if (inode == other.inode) {
60       if (data && other.data) {
61         return *data == *other.data;
62       } else if (!data && !other.data) {
63         return data.error() == other.data.error();
64       }
65       // TODO: operator== for expected
66     }
67     return false;
68   }
69 
70   constexpr bool operator!=(const InodeResult& other) const {
71     return !(*this == other);
72   }
73 
74   // Returns a human-readable error message, or 'nullopt' if there was no error.
75   std::optional<std::string_view> ErrorMessage() const;
76 };
77 
78 std::ostream& operator<<(std::ostream& os, const InodeResult& result);
79 
80 }  // namespace iorap::inode2filename
81 
82 #endif  // IORAP_SRC_INODE2FILENAME_INODE_RESULT_H_