• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2018 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.h"
16 
17 #include <android-base/logging.h>
18 #include <android-base/parseint.h>
19 #include <android-base/strings.h>
20 
21 #include <string>
22 #include <vector>
23 
24 #include <sys/sysmacros.h>
25 
26 using android::base::ParseUint;
27 
28 namespace iorap::inode2filename {
29 
30 // TODO: refactor to return expected<Inode, string>
Parse(const std::string & str,Inode * out,std::string * error_msg)31 bool Inode::Parse(const std::string& str, Inode* out, std::string* error_msg) {
32   DCHECK(out != nullptr);
33   DCHECK(error_msg != nullptr);
34 
35   // Major:minor:inode OR dev_t@inode
36   std::vector<std::string> lst_pair = android::base::Split(str, "@");
37   if (lst_pair.size() == 2) {
38     size_t dev_whole = 0;
39     if (!ParseUint(lst_pair[0], &dev_whole)) {
40       *error_msg = "Failed to parse the whole device id as uint.";
41       return false;
42     }
43 
44     dev_t dev_w = static_cast<dev_t>(dev_whole);
45     out->device_major = major(dev_w);
46     out->device_minor = minor(dev_w);
47 
48     if (!ParseUint(lst_pair[1], &out->inode)) {
49       *error_msg = "Failed to parse inode as uint.";
50       return false;
51     }
52 
53     return true;
54   }
55 
56   std::vector<std::string> lst = android::base::Split(str, ":");
57 
58   if (lst.size() != 3) {
59     *error_msg = "Too few : separated items";
60     return false;
61   }
62 
63   if (!ParseUint(lst[0], &out->device_major)) {
64     *error_msg = "Failed to parse 0th element as a uint";
65     return false;
66   }
67 
68   if (!ParseUint(lst[1], &out->device_minor)) {
69     *error_msg = "Failed to parse 1st element as a uint";
70     return false;
71   }
72 
73   if (!ParseUint(lst[2], &out->inode)) {
74     *error_msg = "Failed to parse 2nd element as a uint";
75     return false;
76   }
77 
78   return true;
79 }
80 
81 }  // namespace iorap::inode2filename
82