1 /*
2 * Copyright 2022 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 #include <stdio.h>
17 #include <string>
18 #include <android-base/file.h>
19 #include <fstream>
20 #include <dump/pixel_dump.h>
21
22 // Format title and content output.
dumpFileContent(const char * title,const char * file_path)23 void dumpFileContent(const char* title, const char* file_path) {
24 std::string content;
25 printf("------ %s (%s) ------\n", title, file_path);
26 if (android::base::ReadFileToString(file_path, &content)) {
27 printf("%s\n", content.c_str());
28 } else {
29 printf("Unable to read %s\n", file_path);
30 }
31 return;
32 }
33
34 // Format title and command output.
runCommand(const char * title,const char * cmd)35 void runCommand(const char* title, const char* cmd) {
36 printf("------ %s (%s)------\n", title, cmd);
37 system(cmd);
38 return;
39 }
40
concatenatePath(const char * folder,const char * file)41 std::string concatenatePath(const char* folder, const char* file){
42 std::string path = folder;
43 if(folder[strlen(folder)-1] == '/'){
44 path = path + file;
45 } else {
46 path = path + "/" + file;
47 }
48
49 printf("folder:%s, result:%s\n", folder, path.c_str());
50 return path;
51 }
52
53 // Copy stored log from individual folder to our dumpstate folder for
54 // compressing.
dumpLogs(const char * SrcDir,const char * DestDir,int limit,const char * prefix)55 void dumpLogs(const char* SrcDir, const char* DestDir, int limit, const char* prefix) {
56
57 struct dirent **dirent_list = NULL;
58 int num_entries = scandir(SrcDir, &dirent_list, 0, (int (*)(const struct dirent **, const struct dirent **)) alphasort);
59 if (!dirent_list) {
60 printf("Unable to scan dir: %s.\n", SrcDir);
61 return;
62 } else if (num_entries <= 0) {
63 printf("No file is found.\n");
64 return;
65 }
66
67 if (access(DestDir, R_OK)) {
68 printf("Unable to find folder: %s\n", DestDir);
69 return;
70 }
71
72 int copiedFiles = 0;
73
74 for (int i = num_entries - 1; i >= 0; i--) {
75
76 if (0 != strncmp(dirent_list[i]->d_name, prefix, strlen(prefix))) {
77 continue;
78 }
79
80 if ((copiedFiles >= limit) && (limit != -1)) {
81 printf("Skipped %s\n", dirent_list[i]->d_name);
82 continue;
83 }
84
85 copiedFiles++;
86 copyFile(concatenatePath(SrcDir, dirent_list[i]->d_name).c_str(), concatenatePath(DestDir, dirent_list[i]->d_name).c_str());
87 }
88
89 while (num_entries--) {
90 free(dirent_list[num_entries]);
91 }
92
93 free(dirent_list);
94 return;
95 }
96
copyFile(const char * SrcDir,const char * DestDir)97 void copyFile(const char* SrcDir, const char* DestDir) {
98 std::ifstream src(SrcDir, std::ios::binary);
99 std::ofstream dst(DestDir, std::ios::binary);
100 dst << src.rdbuf();
101 src.close();
102 dst.close();
103 return;
104 }
105
106