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 #include "inode2filename/inode_result.h"
16 
17 #include <string.h>
18 
19 namespace iorap::inode2filename {
20 
ErrorMessage() const21 std::optional<std::string_view> InodeResult::ErrorMessage() const {
22   if (data) {
23     return std::nullopt;
24   }
25 
26   const int err_no = data.error();
27   return std::string_view{
28     [=]() -> const char * {
29       switch (err_no) {
30         case InodeResult::kCouldNotFindFilename:
31           return "Could not find filename";
32         case InodeResult::kVerificationFailed:
33           return "Verification failed";
34         default:
35           return strerror(err_no);
36       }
37     }()
38   };
39 }
40 
operator <<(std::ostream & os,const InodeResult & result)41 std::ostream& operator<<(std::ostream& os, const InodeResult& result) {
42   os << "InodeResult{";
43   if (result) {
44       os << "OK,";
45   } else {
46       os << "ERR,";
47   }
48 
49   os << result.inode << ",";
50 
51   if (result) {
52     os << "\"" << result.data.value() << "\"";
53   } else {
54     os << result.data.error();
55     os << " (" << *result.ErrorMessage() << ")";
56   }
57 
58   os << "}";
59   return os;
60 }
61 
62 }  // namespace iorap::inode2filename