2 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
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.
9 #include "LocalCapture.h"
12 #include <sys/types.h>
18 #include "SessionData.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);
35 void LocalCapture::write(char* string) {
39 snprintf(file, PATH_MAX, "%s/session.xml", gSessionData->mAPCDir);
42 if (util->writeToDisk(file, string) < 0) {
43 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
49 eventsXML.write(gSessionData->mAPCDir);
52 char* LocalCapture::createUniqueDirectory(const char* initialPath, const char* ending) {
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.");
60 } else if (initialPath[0] != '/') {
61 if (getcwd(path, PATH_MAX) == 0) {
62 logg->logMessage("Unable to retrieve the current working directory");
64 strncat(path, "/", PATH_MAX - strlen(path) - 1);
65 strncat(path, initialPath, PATH_MAX - strlen(path) - 1);
67 strncpy(path, initialPath, PATH_MAX);
68 path[PATH_MAX - 1] = 0; // strncpy does not guarantee a null-terminated string
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);
76 output = strdup(path);
81 int LocalCapture::removeDirAndAllContents(char* path) {
83 struct stat mFileInfo;
84 // Does the path exist?
85 if (stat(path, &mFileInfo) == 0) {
87 if (mFileInfo.st_mode & S_IFDIR) {
88 DIR * dir = opendir(path);
89 dirent* entry = readdir(dir);
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);
100 entry = readdir(dir);
107 error = remove(path);
113 void LocalCapture::copyImages(ImageLinkList* ptr) {
114 char dstfilename[PATH_MAX];
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);
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);
126 logg->logMessage("copy of file %s to %s failed", ptr->path, dstfilename);