1 /*
2 * Copyright (C) 2020 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 "src/profiling/symbolizer/filesystem.h"
18
19 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
20
21 #include <Windows.h>
22
23 namespace perfetto {
24 namespace profiling {
25
WalkDirectories(std::vector<std::string> dirs,FileCallback fn)26 bool WalkDirectories(std::vector<std::string> dirs, FileCallback fn) {
27 std::vector<std::string> sub_dirs;
28 for (const std::string& dir : dirs) {
29 WIN32_FIND_DATAA file;
30 HANDLE fh = FindFirstFileA((dir + "\\*").c_str(), &file);
31 if (fh != INVALID_HANDLE_VALUE) {
32 do {
33 std::string file_path = dir + "\\" + file.cFileName;
34 if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
35 if (strcmp(file.cFileName, ".") != 0 &&
36 strcmp(file.cFileName, "..") != 0) {
37 sub_dirs.push_back(file_path);
38 }
39 } else {
40 ULARGE_INTEGER size;
41 size.HighPart = file.nFileSizeHigh;
42 size.LowPart = file.nFileSizeLow;
43 fn(file_path.c_str(), size.QuadPart);
44 }
45 } while (FindNextFileA(fh, &file));
46 }
47 CloseHandle(fh);
48 }
49 if (!sub_dirs.empty()) {
50 WalkDirectories(sub_dirs, fn);
51 }
52 return true;
53 }
54
GetFileSize(const std::string & file_path)55 size_t GetFileSize(const std::string& file_path) {
56 HANDLE file =
57 CreateFileA(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
58 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
59 if (file == INVALID_HANDLE_VALUE) {
60 PERFETTO_PLOG("Failed to get file size %s", file_path.c_str());
61 return 0;
62 }
63 LARGE_INTEGER file_size;
64 file_size.QuadPart = 0;
65 if (!GetFileSizeEx(file, &file_size)) {
66 PERFETTO_PLOG("Failed to get file size %s", file_path.c_str());
67 }
68 CloseHandle(file);
69 return static_cast<size_t>(file_size.QuadPart);
70 }
71
72 } // namespace profiling
73 } // namespace perfetto
74
75 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
76