create function dumpLog

Bug: 240530709
Test: adb bugreport
Change-Id: I036757f10f18f5dcf4faa6574a7280912348aef4
diff --git a/insmod/pixel_dump.cpp b/insmod/pixel_dump.cpp
index 5926dfc..db7fe3b 100644
--- a/insmod/pixel_dump.cpp
+++ b/insmod/pixel_dump.cpp
@@ -16,6 +16,8 @@
 #include <stdio.h>
 #include <string>
 #include <android-base/file.h>
+#include <fstream>
+#include <dump/pixel_dump.h>
 
 // Format title and content output.
 void dumpFileContent(const char* title, const char* file_path) {
@@ -35,3 +37,62 @@
     system(cmd);
     return;
 }
+
+std::string concatenatePath(const char* folder, const char* file){
+    std::string path = folder;
+    if(folder[strlen(folder)-1] == '/'){
+        path = path + file;
+    } else {
+        path = path + "/" + file;
+    }
+
+    printf("folder:%s, result:%s\n", folder, path.c_str());
+    return path;
+}
+
+// Copy stored log from individual folder to our dumpstate folder for
+// compressing.
+void dumpLogs(const char* SrcDir, const char* DestDir, int limit, const char* prefix) {
+
+    struct dirent **dirent_list = NULL;
+    int num_entries = scandir(SrcDir, &dirent_list, 0, (int (*)(const struct dirent **, const struct dirent **)) alphasort);
+    if (!dirent_list) {
+        printf("Unable to scan dir: %s.\n", SrcDir);
+        return;
+    } else if (num_entries <= 0) {
+        printf("No file is found.\n");
+        return;
+    }
+
+    if (access(DestDir, R_OK)) {
+        printf("Unable to find folder: %s\n", DestDir);
+        return;
+    }
+
+    int copiedFiles = 0;
+
+    for (int i = num_entries - 1; i >= 0; i--) {
+
+        if (0 != strncmp(dirent_list[i]->d_name, prefix, strlen(prefix))) {
+            continue;
+        }
+
+        if ((copiedFiles >= limit) && (limit != -1)) {
+            printf("Skipped %s\n", dirent_list[i]->d_name);
+            continue;
+        }
+
+        copiedFiles++;
+
+        std::ifstream src(concatenatePath(SrcDir, dirent_list[i]->d_name).c_str(), std::ios::binary);
+        std::ofstream dst(concatenatePath(DestDir, dirent_list[i]->d_name).c_str(), std::ios::binary);
+        dst << src.rdbuf();
+    }
+
+    while (num_entries--) {
+        free(dirent_list[num_entries]);
+    }
+
+    free(dirent_list);
+    return;
+}