1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 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 <cstdarg> 16 #include "securec.h" 17 #include "usb_util.h" 18 #include "log.h" 19 20 #ifdef HOST_MINGW 21 #include <windows.h> 22 #endif 23 using namespace Hdc; 24 constexpr auto USB_FFS_BASE = "/dev/usb-ffs/"; 25 GetDevPath(const std::string & path)26std::string GetDevPath(const std::string &path) 27 { 28 DIR *dir = ::opendir(path.c_str()); 29 if (dir == nullptr) { 30 WRITE_LOG(LOG_WARN, "%s: cannot open devpath: errno: %d\n", path.c_str(), errno); 31 return ""; 32 } 33 34 std::string res = USB_FFS_BASE; 35 std::string node; 36 int count = 0; 37 struct dirent *entry = nullptr; 38 while ((entry = ::readdir(dir))) { 39 if (*entry->d_name == '.') { 40 continue; 41 } 42 node = entry->d_name; 43 ++count; 44 } 45 if (count > 1) { 46 res += "hdc"; 47 } else { 48 res += node; 49 } 50 ::closedir(dir); 51 return res; 52 } 53 54