gator: Version 5.14
[android-sdk/arm-ds5-gator.git] / daemon / LocalCapture.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2013. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include "LocalCapture.h"
16 #include "SessionData.h"
17 #include "Logging.h"
18 #include "OlyUtility.h"
20 LocalCapture::LocalCapture() {}
22 LocalCapture::~LocalCapture() {}
24 void LocalCapture::createAPCDirectory(char* target_path) {
25         gSessionData->mAPCDir = createUniqueDirectory(target_path, ".apc");
26         if ((removeDirAndAllContents(gSessionData->mAPCDir) != 0 || mkdir(gSessionData->mAPCDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)) {
27                 logg->logError(__FILE__, __LINE__, "Unable to create directory %s", gSessionData->mAPCDir);
28                 handleException();
29         }
30 }
32 void LocalCapture::write(char* string) {
33         char file[PATH_MAX];
35         // Set full path
36         snprintf(file, PATH_MAX, "%s/session.xml", gSessionData->mAPCDir);
38         // Write the file
39         if (util->writeToDisk(file, string) < 0) {
40                 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
41                 handleException();
42         }
43 }
45 char* LocalCapture::createUniqueDirectory(const char* initialPath, const char* ending) {
46         char* output;
47         char path[PATH_MAX];
49         // Ensure the path is an absolute path, i.e. starts with a slash
50         if (initialPath == 0 || strlen(initialPath) == 0) {
51                 logg->logError(__FILE__, __LINE__, "Missing -o command line option required for a local capture.");
52                 handleException();
53         } else if (initialPath[0] != '/') {
54                 if (getcwd(path, PATH_MAX) == 0) {
55                         logg->logMessage("Unable to retrieve the current working directory");
56                 }
57                 strncat(path, "/", PATH_MAX - strlen(path) - 1);
58                 strncat(path, initialPath, PATH_MAX - strlen(path) - 1);
59         } else {
60                 strncpy(path, initialPath, PATH_MAX);
61                 path[PATH_MAX - 1] = 0; // strncpy does not guarantee a null-terminated string
62         }
64         // Add ending if it is not already there
65         if (strcmp(&path[strlen(path) - strlen(ending)], ending) != 0) {
66                 strncat(path, ending, PATH_MAX - strlen(path) - 1);
67         }
69         output = strdup(path);
71         return output;
72 }
74 int LocalCapture::removeDirAndAllContents(char* path) {
75         int error = 0;
76         struct stat mFileInfo;
77         // Does the path exist?
78         if (stat(path, &mFileInfo) == 0) {
79                 // Is it a directory?
80                 if (mFileInfo.st_mode & S_IFDIR) {
81                         DIR * dir = opendir(path);
82                         dirent* entry = readdir(dir);
83                         while (entry) {
84                                 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
85                                         char* newpath = (char*)malloc(strlen(path) + strlen(entry->d_name) + 2);
86                                         sprintf(newpath, "%s/%s", path, entry->d_name);
87                                         error = removeDirAndAllContents(newpath);
88                                         free(newpath);
89                                         if (error) {
90                                                 break;
91                                         }
92                                 }
93                                 entry = readdir(dir);
94                         }
95                         closedir(dir);
96                         if (error == 0) {
97                                 error = rmdir(path);
98                         }
99                 } else {
100                         error = remove(path);
101                 }
102         }
103         return error;
106 void LocalCapture::copyImages(ImageLinkList* ptr) {
107         char dstfilename[PATH_MAX];
109         while (ptr) {
110                 strncpy(dstfilename, gSessionData->mAPCDir, PATH_MAX);
111                 dstfilename[PATH_MAX - 1] = 0; // strncpy does not guarantee a null-terminated string
112                 if (gSessionData->mAPCDir[strlen(gSessionData->mAPCDir) - 1] != '/') {
113                         strncat(dstfilename, "/", PATH_MAX - strlen(dstfilename) - 1);
114                 }
115                 strncat(dstfilename, util->getFilePart(ptr->path), PATH_MAX - strlen(dstfilename) - 1);
116                 if (util->copyFile(ptr->path, dstfilename)) {
117                         logg->logMessage("copied file %s to %s", ptr->path, dstfilename);
118                 } else {
119                         logg->logMessage("copy of file %s to %s failed", ptr->path, dstfilename);
120                 }
122                 ptr = ptr->next;
123         }