Skip to content

Commit f14554d

Browse files
added readonly flag option for hecdss open (#366)
* added readonly flag option for hecdss open * Allow user to choose access, null/empty check filename * update docstring * fix memory leak in hecdss.c
1 parent 10c308e commit f14554d

3 files changed

Lines changed: 46 additions & 5 deletions

File tree

heclib/hecdss/hecdss.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdlib.h>
55
#include <string.h>
66

7-
HECDSS_API const char *hec_dss_api_version() { return "0.4.0"; }
7+
HECDSS_API const char *hec_dss_api_version() { return "0.5.0"; }
88

99
#if defined(__GNUC__) || defined(__sun__)
1010
#define MIN(a, b) \
@@ -166,11 +166,31 @@ void hec_dss_array_copy_int(int *destination, const long destinationSize,
166166
}
167167
}
168168
HECDSS_API int hec_dss_open(const char *filename, dss_file **dss) {
169+
return hec_dss_open_ex(filename, dss, 0);
170+
}
171+
172+
/*
173+
* int access: read/write access to the file
174+
* 0 - GENERAL_ACCESS: Doesn't matter (no error if file doesn't have write permission)
175+
* 1 - READ_ACCESS: Read only (will not allow writing to file)
176+
* 2 - MULTI_USER_ACCESS: Read/Write permission with full mutil-user access
177+
* (usually slow, but necessary for multiple processes)
178+
* 3 - SINGLE_USER_ADVISORY_ACCESS: Read/Write permission with mutil-user advisory access
179+
* (throws an error if file is read only). Best (and default access)
180+
* 4 - EXCLUSIVE_ACCESS: Exclusive write (used for squeezing). Throws an error if not available.
181+
*/
182+
HECDSS_API int hec_dss_open_ex(const char *filename, dss_file **dss,
183+
int access) {
169184
dss_file *f = (dss_file *)malloc(sizeof(dss_file));
170185
if (f == NULL)
171186
return -1;
187+
// readonly can only be from 0 to 4
188+
if (access < 0 || access > 4) {
189+
free(f);
190+
return -1;
191+
}
172192

173-
int status = hec_dss_zopen(f->ifltab, filename);
193+
int status = zopenExtended(f->ifltab, filename, 0, access, 0, 0, 0);
174194
if (status != 0) {
175195
free(f);
176196
return status;

heclib/hecdss/hecdss.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
99
1010
This API is designed with perspective that the calling/client code is in charge
11-
of managing memory. The only exception is hec_dss_open(const char* filename,
12-
dss_file** dss). hec_dss_open allocates one internal structure that must be
13-
freed by calling hec_dss_close;
11+
of managing memory. The only exceptions are hec_dss_open(const char* filename,
12+
dss_file** dss) and hec_dss_open_ex(const char* filename, dss_file** dss,
13+
int readonly). Each allocates one internal structure that must be freed by
14+
calling hec_dss_close;
1415
1516
For reading data: The client passes in pre-allocated arrays, with the size,
1617
then the API copies data into those arrays
@@ -73,12 +74,26 @@ HECDSS_API int hec_dss_CONSTANT_MAX_PATH_SIZE();
7374

7475
/// <summary>
7576
/// use hec_dss_open to connect to a DSS file.
77+
/// Equivalent to hec_dss_open_ex(filename, dss, 0).
7678
/// </summary>
7779
/// <param name="filename">input: filename to open or create</param>
7880
/// <param name="dss">output: pointer to dss_file</param>
7981
/// <returns>zero if successful</returns>
8082
HECDSS_API int hec_dss_open(const char *filename, dss_file **dss);
8183

84+
/// <summary>
85+
/// use hec_dss_open_ex to connect to a DSS file, access.
86+
///
87+
/// </summary>
88+
/// <param name="filename">input: filename to open; also created if readonly is
89+
/// zero and the file does not exist</param>
90+
/// <param name="dss">output: pointer to dss_file</param>
91+
/// <param name="access">input: zero to open with read/write access
92+
/// (identical to hec_dss_open), non-zero to open read-only</param>
93+
/// <returns>zero if successful</returns>
94+
HECDSS_API int hec_dss_open_ex(const char *filename, dss_file **dss,
95+
int access);
96+
8297
/// <summary>
8398
/// use hec_dss_close to close the connection to a DSS file.
8499
/// This must be called when a program is done using a DSS file.

heclib/heclib_c/src/DssInterface/v6and7/zopenExtended.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
int zopenExtended(long long *ifltab, const char *dssFilename, int fileVersion,
1818
int access, int maxExpectedPathnames, int hashSize, int binSize)
1919
{
20+
21+
if (!dssFilename || strlen(dssFilename) <=0 ) {
22+
return zerrorProcessing(ifltab, DSS_FUNCTION_zopen_ID, zdssErrorCodes.NULL_FILENAME,
23+
0, 0, zdssErrorSeverity.INVALID_ARGUMENT, "", "");
24+
}
25+
2026
int version;
2127

2228
version = zgetFileVersion(dssFilename);

0 commit comments

Comments
 (0)