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

Commit fb0fead

Browse files
AddressXceptionechumley-msft
authored andcommitted
Fix/configurable storage paths (#55)
* 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
1 parent 5ebe919 commit fb0fead

9 files changed

Lines changed: 160 additions & 3 deletions

File tree

.vscode/c_cpp_properties.json

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [],
9+
"compilerPath": "/usr/bin/gcc",
10+
"cStandard": "c11",
11+
"cppStandard": "c++17",
12+
"intelliSenseMode": "clang-x64"
13+
},
314
{
415
"name": "Mac",
516
"includePath": [
@@ -8,13 +19,38 @@
819
"defines": [],
920
"macFrameworkPath": [
1021
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
11-
"/System/Library/Frameworks",
22+
"/System/Library/Frameworks",
1223
"/Library/Frameworks"
1324
],
1425
"compilerPath": "/usr/bin/clang",
1526
"cStandard": "c11",
1627
"cppStandard": "c++17",
1728
"intelliSenseMode": "${default}"
29+
},
30+
{
31+
"name": "Win64",
32+
"includePath": [
33+
"${workspaceFolder}/**",
34+
"C:\\msys64\\mingw64\\include"
35+
],
36+
"defines": [
37+
"_DEBUG",
38+
"UNICODE",
39+
"_UNICODE"
40+
],
41+
"windowsSdkVersion": "10.0.18362.0",
42+
"compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
43+
"cStandard": "c11",
44+
"cppStandard": "c++17",
45+
"intelliSenseMode": "gcc-x64",
46+
"browse": {
47+
"path": [
48+
"C:\\msys64\\mingw64\\include"
49+
],
50+
"limitSymbolsToIncludedHeaders": true,
51+
"databaseFilename": ""
52+
},
53+
"forcedInclude": []
1854
}
1955
],
2056
"version": 4

.vscode/tasks.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@
7979
}
8080
}
8181
},
82+
{
83+
"label": "Build Shared Library (debug)",
84+
"type": "shell",
85+
"command": "cmake",
86+
"group": "build",
87+
"dependsOn": [ "CMake Build (debug)"],
88+
"presentation": {
89+
"echo": true,
90+
"reveal": "always",
91+
"focus": false,
92+
"panel": "shared"
93+
},
94+
"args": ["--build", "build"],
95+
"problemMatcher": {
96+
"owner": "cpp",
97+
"fileLocation": [
98+
"relative",
99+
"${workspaceRoot}"
100+
],
101+
"pattern": {
102+
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
103+
"file": 1,
104+
"line": 2,
105+
"column": 3,
106+
"severity": 4,
107+
"message": 5
108+
}
109+
}
110+
},
82111
{
83112
"label": "Build Shared Library (release)",
84113
"type": "shell",

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ add_library(electionguard
6363
${PROJECT_SOURCE_DIR}/src/electionguard/random_source.c
6464
${PROJECT_SOURCE_DIR}/src/electionguard/trustee_state_rep.h
6565
${PROJECT_SOURCE_DIR}/src/electionguard/file.c
66+
${PROJECT_SOURCE_DIR}/src/electionguard/directory.c
67+
${PROJECT_SOURCE_DIR}/src/electionguard/directory.h
6668
${PROJECT_SOURCE_DIR}/include/electionguard/api/config.h
6769
${PROJECT_SOURCE_DIR}/include/electionguard/api/create_election.h
6870
${PROJECT_SOURCE_DIR}/include/electionguard/api/encrypt_ballot.h

examples/api/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int main()
131131
if (ok)
132132
{
133133
// Assigning an output_path fails if this folder doesn't already exist
134-
char *output_path = "../"; // This outputs to the directy above the cwd.
134+
char *output_path = "./ballots/"; // This outputs to the directy above the cwd.
135135
char *output_prefix = "ballots-";
136136
ok = API_RecordBallots(config.num_selections, current_cast_index, current_spoiled_index,
137137
NUM_RANDOM_BALLOT_SELECTIONS, casted_ballot_ids, spoiled_ballot_ids, encrypted_ballots,
@@ -162,7 +162,7 @@ int main()
162162
uint32_t tally_results[config.num_selections];
163163
if (ok)
164164
{
165-
char *output_path = "../"; // This outputs to the directy above the cwd.
165+
char *output_path = "./tallies/"; // This outputs to the directy above the cwd.
166166
char *output_prefix = "tally-";
167167
ok = API_TallyVotes(config, trustee_states, DECRYPTING_TRUSTEES,
168168
ballots_filename, output_path, output_prefix, &tally_filename, tally_results);

src/electionguard/api/record_ballots.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "api/filename.h"
77
#include "serialize/voting.h"
88
#include "voting/num_ballots.h"
9+
#include "directory.h"
910

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

210+
if (ok) {
211+
ok = create_directory(export_path);
212+
}
213+
209214
if (ok)
210215
{
211216
FILE *out = fopen(*output_filename, "w+");

src/electionguard/api/tally_votes.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "api/base_hash.h"
77
#include "api/filename.h"
8+
#include "directory.h"
89

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

271+
if (ok) {
272+
ok = create_directory(export_path);
273+
}
274+
270275
if (ok)
271276
{
272277
FILE *out = fopen(*output_filename, "w+");

src/electionguard/directory.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
3+
#include <stdio.h> /* FILENAME_MAX */
4+
#include <sys/stat.h> /* mkdir(2) */
5+
#include <errno.h>
6+
7+
#include "directory.h"
8+
9+
#ifndef FILENAME_MAX
10+
#define FILENAME_MAX=256
11+
#endif
12+
13+
bool create_directory(const char *path)
14+
{
15+
/* Adapted from http://stackoverflow.com/a/2336245/119527 */
16+
const size_t len = strlen(path);
17+
char _path[FILENAME_MAX];
18+
char *p;
19+
20+
errno = 0;
21+
22+
/* Copy string so its mutable */
23+
if (len > sizeof(_path)-1) {
24+
errno = ENAMETOOLONG;
25+
return false;
26+
}
27+
strcpy(_path, path);
28+
29+
char *directory_separator;
30+
#ifdef _WIN32
31+
directory_separator = "\\";
32+
#else
33+
directory_separator = "/";
34+
#endif
35+
36+
/* Iterate the string */
37+
for (p = _path + 1; *p; p++) {
38+
if (*p == directory_separator[0]) {
39+
/* Temporarily truncate */
40+
*p = '\0';
41+
42+
int mk_dir_res = -1;
43+
#ifdef _WIN32
44+
mk_dir_res = mkdir(_path);
45+
#else
46+
mk_dir_res = mkdir(_path, S_IRWXU);
47+
#endif
48+
if (mk_dir_res != 0) {
49+
if (errno != EEXIST)
50+
return false;
51+
}
52+
53+
*p = directory_separator[0];
54+
}
55+
}
56+
57+
int mk_dir_res = -1;
58+
#ifdef _WIN32
59+
mk_dir_res = mkdir(_path);
60+
#else
61+
mk_dir_res = mkdir(_path, S_IRWXU);
62+
#endif
63+
64+
if (mk_dir_res != 0) {
65+
if (errno != EEXIST)
66+
return false;
67+
}
68+
69+
return true;
70+
}

src/electionguard/directory.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __DIRECTORY_H__
2+
#define __DIRECTORY_H__
3+
4+
#include <stdbool.h>
5+
#include <string.h>
6+
7+
bool create_directory(const char *path);
8+
9+
#endif /* __DIRECTORY_H__ */

src/electionguard/voting/encrypter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Voting_Encrypter_encrypt_ballot(Voting_Encrypter encrypter,
182182

183183
struct Voting_Encrypter_encrypt_ballot_r balotR;
184184
balotR.status = VOTING_ENCRYPTER_SUCCESS;
185+
185186
// validate selection
186187
if (!Validate_selections(selections, encrypter->num_selections, expected_num_selected))
187188
balotR.status = VOTING_ENCRYPTER_SELECTION_ERROR;

0 commit comments

Comments
 (0)