Exemple #1
0
boolByte copyFileToDirectory(const CharString fileAbsolutePath, const CharString directoryAbsolutePath) {
  boolByte result = false;
  CharString fileOutPath = newCharStringWithCapacity(kCharStringLengthLong);
  CharString fileBasename = NULL;
  FILE *input = NULL;
  FILE *output = NULL;
  char ch;

  fileBasename = newCharStringWithCString(getFileBasename(fileAbsolutePath->data));
  buildAbsolutePath(directoryAbsolutePath, fileBasename, NULL, fileOutPath);
  input = fopen(fileAbsolutePath->data, "rb");
  if(input != NULL) {
    output = fopen(fileOutPath->data, "wb");
    if(output != NULL) {
      while(fread(&ch, 1, 1, input) == 1) {
        fwrite(&ch, 1, 1, output);
      }
      result = true;
    }
  }

  if(input != NULL) {
    fclose(input);
  }
  if(output != NULL) {
    fclose(output);
  }
  freeCharString(fileOutPath);
  freeCharString(fileBasename);
  return result;
}
static int _testBuildAbsolutePathNullFile(void) {
  CharString d = getCurrentDirectory();
  CharString out = newCharString();

  buildAbsolutePath(d, NULL, NULL, out);
  assert(charStringIsEmpty(out));

  freeCharString(d);
  freeCharString(out);
  return 0;
}
static int _testBuildAbsolutePathNullPath(void) {
  CharString f = newCharString();
  CharString out = newCharString();

  buildAbsolutePath(NULL, f, NULL, out);
  assert(charStringIsEmpty(out));

  freeCharString(f);
  freeCharString(out);
  return 0;
}
Exemple #4
0
void errorReporterRemapPath(ErrorReporter self, CharString path) {
  CharString basename = newCharString();
  CharString outString = newCharStringWithCapacity(path->capacity);

  charStringCopyCString(basename, getFileBasename(path->data));
  buildAbsolutePath(self->reportDirPath, basename, NULL, outString);
  charStringCopy(path, outString);

  freeCharString(basename);
  freeCharString(outString);
}
static int _testBuildAbsolutePathEmptyPath(void) {
  CharString d = newCharString();
  CharString f = newCharStringWithCString(TEST_FILENAME);
  CharString out = newCharString();

  buildAbsolutePath(d, f, NULL, out);
  assert(charStringIsEmpty(out));

  freeCharString(d);
  freeCharString(f);
  freeCharString(out);
  return 0;
}
static int _testBuildAbsolutePathWithFileExtensionTwice(void) {
  CharString d = newCharStringWithCString(ROOT_DIRECTORY);
  CharString out = newCharString();
  CharString f = newCharStringWithCString(TEST_FILENAME);
  CharString expected = newCharString();

  snprintf(expected->data, expected->capacity, "%s%c%s", ROOT_DIRECTORY, PATH_DELIMITER, TEST_FILENAME);
  buildAbsolutePath(d, f, "txt", out);
  assertCharStringEquals(out, expected->data);

  freeCharString(d);
  freeCharString(out);
  freeCharString(f);
  freeCharString(expected);
  return 0;
}
Exemple #7
0
void errorReporterClose(ErrorReporter self) {
#if HAVE_LIBARCHIVE
  struct archive* outArchive;
  CharString outputFilename = newCharString();
  LinkedList reportContents = newLinkedList();
#endif

  // Always do this, just in case
  flushErrorLog();

#if HAVE_LIBARCHIVE
  // In case any part of the error report causes a segfault, this function will
  // be called recursively. A mutex would really be a better solution here, but
  // this will also work just fine.
  if(!self->completed) {
    self->completed = true;
    buildAbsolutePath(self->desktopPath, self->reportName, "tar.gz", outputFilename);
    listDirectory(self->reportDirPath->data, reportContents);
    if(self != NULL) {
      outArchive = archive_write_new();
      archive_write_set_compression_gzip(outArchive);
      archive_write_set_format_pax_restricted(outArchive);
      archive_write_open_filename(outArchive, outputFilename->data);

      linkedListForeach(reportContents, _remapFileToErrorReportRelativePath, self);
      chdir(self->desktopPath->data);
      linkedListForeach(reportContents, _addFileToArchive, outArchive);

      archive_write_close(outArchive);
      archive_write_free(outArchive);
    }
    // Remove original error report
    removeDirectory(self->reportDirPath);
  }
#endif

  printf("\n=== Error report complete ===\n");
  printf("Created error report at %s\n", self->reportDirPath->data);
#if HAVE_LIBARCHIVE
  printf("Please email the report to: %s\n", SUPPORT_EMAIL);
#else
  printf("Please compress and email the report to: %s\n", SUPPORT_EMAIL);
#endif
  printf("Thanks!\n");
}
static CharString _fileUtilitiesMakeTempDir(void) {
  CharString tempDirName = newCharString();
#if UNIX
  snprintf(tempDirName->data, tempDirName->capacity, "/tmp/mrswatsontest-XXXXXX");
  mktemp(tempDirName->data);
#elif WINDOWS
  CharString systemTempDir = newCharString();
  CharString randomDirName = newCharString();
  snprintf(randomDirName->data, randomDirName->capacity, "mrswatsontest-%d", rand());
  GetTempPathA(systemTempDir->capacity, systemTempDir->data);
  buildAbsolutePath(systemTempDir, randomDirName, NULL, tempDirName);
  freeCharString(systemTempDir);
  freeCharString(randomDirName);
#endif
  if(!makeDirectory(tempDirName)) {
    fprintf(stderr, "WARNING: Could not make temporary directory\n");
    return NULL;
  }
  return tempDirName;
}
Exemple #9
0
void buildAbsolutePath(const CharString directory, const CharString file, const char* fileExtension, CharString outString) {
  const char* extension;
  CharString absoluteDirectory;

  if(directory == NULL || charStringIsEmpty(directory)) {
    logWarn("Attempt to build absolute path with empty directory");
    return;
  }
  if(file == NULL || charStringIsEmpty(file)) {
    logWarn("Attempt to build absolute path with empty file");
    return;
  }

  absoluteDirectory = newCharString();
  if(isAbsolutePath(directory)) {
    charStringCopy(absoluteDirectory, directory);
  }
  else {
    convertRelativePathToAbsolute(directory, absoluteDirectory);
  }

  if(fileExtension != NULL) {
    // Ignore attempts to append the same extension as is already on the file
    extension = getFileExtension(file->data);
    if(extension != NULL && !strncasecmp(extension, fileExtension, strlen(extension))) {
      buildAbsolutePath(absoluteDirectory, file, NULL, outString);
    }
    else {
      snprintf(outString->data, outString->length, "%s%c%s.%s",
        absoluteDirectory->data, PATH_DELIMITER, file->data, fileExtension);
    }
  }
  else {
    snprintf(outString->data, outString->length, "%s%c%s",
      absoluteDirectory->data, PATH_DELIMITER, file->data);
  }

  freeCharString(absoluteDirectory);
}
Exemple #10
0
static int _testListDirectory(void) {
  CharString tempDir = _fileUtilitiesMakeTempDir();
  CharString tempFile = newCharString();
  CharString filename;
  CharString testFilename = newCharStringWithCString(TEST_FILENAME);
  LinkedList l;
  FILE *f;

  buildAbsolutePath(tempDir, testFilename, NULL, tempFile);
  f = fopen(tempFile->data, "w");
  assertNotNull(f);
  fclose(f);
  l = listDirectory(tempDir);
  assertIntEquals(linkedListLength(l), 1);
  filename = (CharString)l->item;
  assertCharStringEquals(filename, TEST_FILENAME);

  removeDirectory(tempDir);
  freeCharString(tempDir);
  freeCharString(tempFile);
  freeCharString(testFilename);
  freeLinkedListAndItems(l, (LinkedListFreeItemFunc)freeCharString);
  return 0;
}