Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Fix/configurable storage paths (#55)
Browse files Browse the repository at this point in the history
* when writing files, create directories if they do not exist

* revert botched merge file

* modify tasks to include debug shared lib builds

* use FILENAME_MAX from stdio instead of PATH_MAX from linux/limits

* include header guard

* Fix windows support for mkdir
  • Loading branch information
AddressXception authored and echumley-msft committed Dec 5, 2019
1 parent 5ebe919 commit fb0fead
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 3 deletions.
38 changes: 37 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
},
{
"name": "Mac",
"includePath": [
Expand All @@ -8,13 +19,38 @@
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
"/System/Library/Frameworks",
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
},
{
"name": "Win64",
"includePath": [
"${workspaceFolder}/**",
"C:\\msys64\\mingw64\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"C:\\msys64\\mingw64\\include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"forcedInclude": []
}
],
"version": 4
Expand Down
29 changes: 29 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@
}
}
},
{
"label": "Build Shared Library (debug)",
"type": "shell",
"command": "cmake",
"group": "build",
"dependsOn": [ "CMake Build (debug)"],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"args": ["--build", "build"],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"label": "Build Shared Library (release)",
"type": "shell",
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ add_library(electionguard
${PROJECT_SOURCE_DIR}/src/electionguard/random_source.c
${PROJECT_SOURCE_DIR}/src/electionguard/trustee_state_rep.h
${PROJECT_SOURCE_DIR}/src/electionguard/file.c
${PROJECT_SOURCE_DIR}/src/electionguard/directory.c
${PROJECT_SOURCE_DIR}/src/electionguard/directory.h
${PROJECT_SOURCE_DIR}/include/electionguard/api/config.h
${PROJECT_SOURCE_DIR}/include/electionguard/api/create_election.h
${PROJECT_SOURCE_DIR}/include/electionguard/api/encrypt_ballot.h
Expand Down
4 changes: 2 additions & 2 deletions examples/api/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int main()
if (ok)
{
// Assigning an output_path fails if this folder doesn't already exist
char *output_path = "../"; // This outputs to the directy above the cwd.
char *output_path = "./ballots/"; // This outputs to the directy above the cwd.
char *output_prefix = "ballots-";
ok = API_RecordBallots(config.num_selections, current_cast_index, current_spoiled_index,
NUM_RANDOM_BALLOT_SELECTIONS, casted_ballot_ids, spoiled_ballot_ids, encrypted_ballots,
Expand Down Expand Up @@ -162,7 +162,7 @@ int main()
uint32_t tally_results[config.num_selections];
if (ok)
{
char *output_path = "../"; // This outputs to the directy above the cwd.
char *output_path = "./tallies/"; // This outputs to the directy above the cwd.
char *output_prefix = "tally-";
ok = API_TallyVotes(config, trustee_states, DECRYPTING_TRUSTEES,
ballots_filename, output_path, output_prefix, &tally_filename, tally_results);
Expand Down
5 changes: 5 additions & 0 deletions src/electionguard/api/record_ballots.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "api/filename.h"
#include "serialize/voting.h"
#include "voting/num_ballots.h"
#include "directory.h"

static bool initialize_coordinator(uint32_t num_selections);
static bool get_serialized_ballot_identifier(int64_t ballot_id, struct ballot_identifier *ballot_identifier);
Expand Down Expand Up @@ -206,6 +207,10 @@ bool export_ballots(char *export_path, char *filename_prefix, char **output_file
printf("API_RecordBallots :: generated unique filename for export at \"%s\"\n", *output_filename);
#endif

if (ok) {
ok = create_directory(export_path);
}

if (ok)
{
FILE *out = fopen(*output_filename, "w+");
Expand Down
5 changes: 5 additions & 0 deletions src/electionguard/api/tally_votes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "api/base_hash.h"
#include "api/filename.h"
#include "directory.h"

// Initialize
static bool initialize_coordinator(void);
Expand Down Expand Up @@ -267,6 +268,10 @@ bool export_tally_votes(char *export_path, char *filename_prefix,
printf("API_TALLYVOTES :: generated unique filename for export at \"%s\"\n", *output_filename);
#endif

if (ok) {
ok = create_directory(export_path);
}

if (ok)
{
FILE *out = fopen(*output_filename, "w+");
Expand Down
70 changes: 70 additions & 0 deletions src/electionguard/directory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@


#include <stdio.h> /* FILENAME_MAX */
#include <sys/stat.h> /* mkdir(2) */
#include <errno.h>

#include "directory.h"

#ifndef FILENAME_MAX
#define FILENAME_MAX=256
#endif

bool create_directory(const char *path)
{
/* Adapted from http://stackoverflow.com/a/2336245/119527 */
const size_t len = strlen(path);
char _path[FILENAME_MAX];
char *p;

errno = 0;

/* Copy string so its mutable */
if (len > sizeof(_path)-1) {
errno = ENAMETOOLONG;
return false;
}
strcpy(_path, path);

char *directory_separator;
#ifdef _WIN32
directory_separator = "\\";
#else
directory_separator = "/";
#endif

/* Iterate the string */
for (p = _path + 1; *p; p++) {
if (*p == directory_separator[0]) {
/* Temporarily truncate */
*p = '\0';

int mk_dir_res = -1;
#ifdef _WIN32
mk_dir_res = mkdir(_path);
#else
mk_dir_res = mkdir(_path, S_IRWXU);
#endif
if (mk_dir_res != 0) {
if (errno != EEXIST)
return false;
}

*p = directory_separator[0];
}
}

int mk_dir_res = -1;
#ifdef _WIN32
mk_dir_res = mkdir(_path);
#else
mk_dir_res = mkdir(_path, S_IRWXU);
#endif

if (mk_dir_res != 0) {
if (errno != EEXIST)
return false;
}

return true;
}
9 changes: 9 additions & 0 deletions src/electionguard/directory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __DIRECTORY_H__
#define __DIRECTORY_H__

#include <stdbool.h>
#include <string.h>

bool create_directory(const char *path);

#endif /* __DIRECTORY_H__ */
1 change: 1 addition & 0 deletions src/electionguard/voting/encrypter.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Voting_Encrypter_encrypt_ballot(Voting_Encrypter encrypter,

struct Voting_Encrypter_encrypt_ballot_r balotR;
balotR.status = VOTING_ENCRYPTER_SUCCESS;

// validate selection
if (!Validate_selections(selections, encrypter->num_selections, expected_num_selected))
balotR.status = VOTING_ENCRYPTER_SELECTION_ERROR;
Expand Down

0 comments on commit fb0fead

Please sign in to comment.