blob: 3235a34ae9c718f76db3cc426d43cc9e7325b6e4 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
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 */
8
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"
19#include "EventsXML.h"
20
21LocalCapture::LocalCapture() {}
22
23LocalCapture::~LocalCapture() {}
24
25void LocalCapture::createAPCDirectory(char* target_path) {
26 gSessionData->mAPCDir = createUniqueDirectory(target_path, ".apc");
27 if ((removeDirAndAllContents(gSessionData->mAPCDir) != 0 || mkdir(gSessionData->mAPCDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)) {
28 logg->logError(__FILE__, __LINE__, "Unable to create directory %s", gSessionData->mAPCDir);
29 handleException();
30 }
31}
32
33void LocalCapture::write(char* string) {
34 char file[PATH_MAX];
35
36 // Set full path
37 snprintf(file, PATH_MAX, "%s/session.xml", gSessionData->mAPCDir);
38
39 // Write the file
40 if (util->writeToDisk(file, string) < 0) {
41 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
42 handleException();
43 }
44
45 // Write events XML
46 EventsXML eventsXML;
47 eventsXML.write(gSessionData->mAPCDir);
48}
49
50char* LocalCapture::createUniqueDirectory(const char* initialPath, const char* ending) {
51 char* output;
52 char path[PATH_MAX];
53
54 // Ensure the path is an absolute path, i.e. starts with a slash
55 if (initialPath == 0 || strlen(initialPath) == 0) {
56 logg->logError(__FILE__, __LINE__, "Missing -o command line option required for a local capture.");
57 handleException();
58 } else if (initialPath[0] != '/') {
59 if (getcwd(path, PATH_MAX) == 0) {
60 logg->logMessage("Unable to retrieve the current working directory");
61 }
62 strncat(path, "/", PATH_MAX - strlen(path) - 1);
63 strncat(path, initialPath, PATH_MAX - strlen(path) - 1);
64 } else {
65 strncpy(path, initialPath, PATH_MAX);
66 path[PATH_MAX - 1] = 0; // strncpy does not guarantee a null-terminated string
67 }
68
69 // Add ending if it is not already there
70 if (strcmp(&path[strlen(path) - strlen(ending)], ending) != 0) {
71 strncat(path, ending, PATH_MAX - strlen(path) - 1);
72 }
73
74 output = strdup(path);
75
76 return output;
77}
78
79int LocalCapture::removeDirAndAllContents(char* path) {
80 int error = 0;
81 struct stat mFileInfo;
82 // Does the path exist?
83 if (stat(path, &mFileInfo) == 0) {
84 // Is it a directory?
85 if (mFileInfo.st_mode & S_IFDIR) {
86 DIR * dir = opendir(path);
87 dirent* entry = readdir(dir);
88 while (entry) {
89 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
90 char* newpath = (char*)malloc(strlen(path) + strlen(entry->d_name) + 2);
91 sprintf(newpath, "%s/%s", path, entry->d_name);
92 error = removeDirAndAllContents(newpath);
93 free(newpath);
94 if (error) {
95 break;
96 }
97 }
98 entry = readdir(dir);
99 }
100 closedir(dir);
101 if (error == 0) {
102 error = rmdir(path);
103 }
104 } else {
105 error = remove(path);
106 }
107 }
108 return error;
109}
110
111void LocalCapture::copyImages(ImageLinkList* ptr) {
112 char dstfilename[PATH_MAX];
113
114 while (ptr) {
115 strncpy(dstfilename, gSessionData->mAPCDir, PATH_MAX);
116 dstfilename[PATH_MAX - 1] = 0; // strncpy does not guarantee a null-terminated string
117 if (gSessionData->mAPCDir[strlen(gSessionData->mAPCDir) - 1] != '/') {
118 strncat(dstfilename, "/", PATH_MAX - strlen(dstfilename) - 1);
119 }
120 strncat(dstfilename, util->getFilePart(ptr->path), PATH_MAX - strlen(dstfilename) - 1);
121 if (util->copyFile(ptr->path, dstfilename)) {
122 logg->logMessage("copied file %s to %s", ptr->path, dstfilename);
123 } else {
124 logg->logMessage("copy of file %s to %s failed", ptr->path, dstfilename);
125 }
126
127 ptr = ptr->next;
128 }
129}