blob: 3f9daf30a5626ca500cbc020314459e69f42acdb [file] [log] [blame]
Joe Hattori8def8382025-07-03 00:41:35 -07001/*
2 * Copyright (C) 2025 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <modprobe/utils.h>
18
19#include <android-base/logging.h>
20#include <android-base/strings.h>
21
22namespace android {
23namespace modprobe {
24
Joe Hattoria8827b12025-07-07 02:17:23 +000025std::string CanonicalizeModulePath(const std::string& module_path) {
Joe Hattori8def8382025-07-03 00:41:35 -070026 auto start = module_path.find_last_of('/');
27 if (start == std::string::npos) {
28 start = 0;
29 } else {
30 start += 1;
31 }
32 auto end = module_path.size();
33 if (android::base::EndsWith(module_path, ".ko")) {
34 end -= 3;
35 }
36 if ((end - start) <= 1) {
37 LOG(ERROR) << "malformed module name: " << module_path;
38 return "";
39 }
40 std::string module_name = module_path.substr(start, end - start);
41 // module names can have '-', but their file names will have '_'
42 std::replace(module_name.begin(), module_name.end(), '-', '_');
43 return module_name;
44}
45
46} // namespace modprobe
47} // namespace android