Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 2 | * Copyright (C) ARM Limited 2010-2015. All rights reserved. |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 9 | #include "LocalCapture.h" |
| 10 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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> |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 17 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 18 | #include "SessionData.h" |
| 19 | #include "Logging.h" |
| 20 | #include "OlyUtility.h" |
| 21 | #include "EventsXML.h" |
| 22 | |
| 23 | LocalCapture::LocalCapture() {} |
| 24 | |
| 25 | LocalCapture::~LocalCapture() {} |
| 26 | |
| 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)) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 30 | logg->logError("Unable to create directory %s", gSessionData->mAPCDir); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 31 | handleException(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void LocalCapture::write(char* string) { |
| 36 | char file[PATH_MAX]; |
| 37 | |
| 38 | // Set full path |
| 39 | snprintf(file, PATH_MAX, "%s/session.xml", gSessionData->mAPCDir); |
| 40 | |
| 41 | // Write the file |
| 42 | if (util->writeToDisk(file, string) < 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 43 | logg->logError("Error writing %s\nPlease verify the path.", file); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 44 | handleException(); |
| 45 | } |
| 46 | |
| 47 | // Write events XML |
| 48 | EventsXML eventsXML; |
| 49 | eventsXML.write(gSessionData->mAPCDir); |
| 50 | } |
| 51 | |
| 52 | char* LocalCapture::createUniqueDirectory(const char* initialPath, const char* ending) { |
| 53 | char* output; |
| 54 | char path[PATH_MAX]; |
| 55 | |
| 56 | // Ensure the path is an absolute path, i.e. starts with a slash |
| 57 | if (initialPath == 0 || strlen(initialPath) == 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 58 | logg->logError("Missing -o command line option required for a local capture."); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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 | } |
| 70 | |
| 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 | } |
| 75 | |
| 76 | output = strdup(path); |
| 77 | |
| 78 | return output; |
| 79 | } |
| 80 | |
| 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; |
| 111 | } |
| 112 | |
| 113 | void LocalCapture::copyImages(ImageLinkList* ptr) { |
| 114 | char dstfilename[PATH_MAX]; |
| 115 | |
| 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 | } |
| 128 | |
| 129 | ptr = ptr->next; |
| 130 | } |
| 131 | } |