]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/arm-ds5-gator.git/blob - daemon/LocalCapture.cpp
Merge branch 'master' into android
[android-sdk/arm-ds5-gator.git] / daemon / LocalCapture.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2014. 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 "LocalCapture.h"
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <dirent.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <unistd.h>
18 #include "SessionData.h"
19 #include "Logging.h"
20 #include "OlyUtility.h"
21 #include "EventsXML.h"
23 LocalCapture::LocalCapture() {}
25 LocalCapture::~LocalCapture() {}
27 void LocalCapture::createAPCDirectory(char* target_path) {
28         gSessionData->mAPCDir = createUniqueDirectory(target_path, ".apc");
29         if ((removeDirAndAllContents(gSessionData->mAPCDir) != 0 || mkdir(gSessionData->mAPCDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)) {
30                 logg->logError(__FILE__, __LINE__, "Unable to create directory %s", gSessionData->mAPCDir);
31                 handleException();
32         }
33 }
35 void LocalCapture::write(char* string) {
36         char file[PATH_MAX];
38         // Set full path
39         snprintf(file, PATH_MAX, "%s/session.xml", gSessionData->mAPCDir);
41         // Write the file
42         if (util->writeToDisk(file, string) < 0) {
43                 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
44                 handleException();
45         }
47         // Write events XML
48         EventsXML eventsXML;
49         eventsXML.write(gSessionData->mAPCDir);
50 }
52 char* LocalCapture::createUniqueDirectory(const char* initialPath, const char* ending) {
53         char* output;
54         char path[PATH_MAX];
56         // Ensure the path is an absolute path, i.e. starts with a slash
57         if (initialPath == 0 || strlen(initialPath) == 0) {
58                 logg->logError(__FILE__, __LINE__, "Missing -o command line option required for a local capture.");
59                 handleException();
60         } else if (initialPath[0] != '/') {
61                 if (getcwd(path, PATH_MAX) == 0) {
62                         logg->logMessage("Unable to retrieve the current working directory");
63                 }
64                 strncat(path, "/", PATH_MAX - strlen(path) - 1);
65                 strncat(path, initialPath, PATH_MAX - strlen(path) - 1);
66         } else {
67                 strncpy(path, initialPath, PATH_MAX);
68                 path[PATH_MAX - 1] = 0; // strncpy does not guarantee a null-terminated string
69         }
71         // Add ending if it is not already there
72         if (strcmp(&path[strlen(path) - strlen(ending)], ending) != 0) {
73                 strncat(path, ending, PATH_MAX - strlen(path) - 1);
74         }
76         output = strdup(path);
78         return output;
79 }
81 int LocalCapture::removeDirAndAllContents(char* path) {
82         int error = 0;
83         struct stat mFileInfo;
84         // Does the path exist?
85         if (stat(path, &mFileInfo) == 0) {
86                 // Is it a directory?
87                 if (mFileInfo.st_mode & S_IFDIR) {
88                         DIR * dir = opendir(path);
89                         dirent* entry = readdir(dir);
90                         while (entry) {
91                                 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
92                                         char* newpath = (char*)malloc(strlen(path) + strlen(entry->d_name) + 2);
93                                         sprintf(newpath, "%s/%s", path, entry->d_name);
94                                         error = removeDirAndAllContents(newpath);
95                                         free(newpath);
96                                         if (error) {
97                                                 break;
98                                         }
99                                 }
100                                 entry = readdir(dir);
101                         }
102                         closedir(dir);
103                         if (error == 0) {
104                                 error = rmdir(path);
105                         }
106                 } else {
107                         error = remove(path);
108                 }
109         }
110         return error;
113 void LocalCapture::copyImages(ImageLinkList* ptr) {
114         char dstfilename[PATH_MAX];
116         while (ptr) {
117                 strncpy(dstfilename, gSessionData->mAPCDir, PATH_MAX);
118                 dstfilename[PATH_MAX - 1] = 0; // strncpy does not guarantee a null-terminated string
119                 if (gSessionData->mAPCDir[strlen(gSessionData->mAPCDir) - 1] != '/') {
120                         strncat(dstfilename, "/", PATH_MAX - strlen(dstfilename) - 1);
121                 }
122                 strncat(dstfilename, util->getFilePart(ptr->path), PATH_MAX - strlen(dstfilename) - 1);
123                 if (util->copyFile(ptr->path, dstfilename)) {
124                         logg->logMessage("copied file %s to %s", ptr->path, dstfilename);
125                 } else {
126                         logg->logMessage("copy of file %s to %s failed", ptr->path, dstfilename);
127                 }
129                 ptr = ptr->next;
130         }