Skip to content

Use shared read and other tidy ups #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions asApp/src/asVerify.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ int main(int argc,char **argv)
* Copy to temporary file.
* The .sav file is likely to be overwritten while we're using it.
*/
fp = fopen(filename,"r");
fp = openShared(filename,"r");
if (fp == NULL) {printf("Can't open %s\n", filename); return(-1);}
tempname = tmpnam(NULL);
ftmp = fopen(tempname,"w");
ftmp = openShared(tempname,"w");
if (ftmp == NULL) {printf("Can't open temp file \"%s\".\n", tempname); return(-1);}
while (!feof(fp) && (n=fread(s,1,BUF_SIZE,fp))) {
fwrite(s,1,n,ftmp);
Expand Down
28 changes: 14 additions & 14 deletions asApp/src/dbrestore.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ STATIC int myFileCopy(const char *source, const char *dest)

if (stat(source, &fileStat) == 0) size = (int)fileStat.st_size;
errno = 0;
if ((source_fd = fopen(source,"rb")) == NULL) {
errlogPrintf("save_restore:myFileCopy: Can't open file '%s'\n", source);
if ((source_fd = openShared(source,"rb")) == NULL) {
errlogPrintf("save_restore:myFileCopy: Can't open source file '%s': %s\n", source, strerror(errno));
/* if (errno) myPrintErrno("myFileCopy", __FILE__, __LINE__); */
if (++save_restoreIoErrors > save_restoreRemountThreshold)
save_restoreNFSOK = 0;
Expand All @@ -182,8 +182,8 @@ STATIC int myFileCopy(const char *source, const char *dest)
* to S_nfsLib_NFSERR_NOENT even though it succeeds. Probably this means
* a failed attempt was retried. (System calls never set errno to zero.)
*/
if ((dest_fd = fopen(dest,"wb")) == NULL) {
errlogPrintf("save_restore:myFileCopy: Can't open file '%s'\n", dest);
if ((dest_fd = openShared(dest,"wb")) == NULL) {
errlogPrintf("save_restore:myFileCopy: Can't open dest file '%s': %s\n", dest, strerror(errno));
/* if (errno) myPrintErrno("myFileCopy", __FILE__, __LINE__); */
fclose(source_fd);
return(ERROR);
Expand All @@ -196,12 +196,12 @@ STATIC int myFileCopy(const char *source, const char *dest)
}
errno = 0;
if (fclose(source_fd) != 0){
errlogPrintf("save_restore:myFileCopy: Error closing file '%s'\n", source);
errlogPrintf("save_restore:myFileCopy: Error closing source file '%s'\n", source);
/* if (errno) myPrintErrno("myFileCopy", __FILE__, __LINE__); */
}
errno = 0;
if (fclose(dest_fd) != 0){
errlogPrintf("save_restore:myFileCopy: Error closing file '%s'\n", dest);
errlogPrintf("save_restore:myFileCopy: Error closing dest file '%s'\n", dest);
/* if (errno) myPrintErrno("myFileCopy", __FILE__, __LINE__); */
}
errno = 0;
Expand Down Expand Up @@ -843,7 +843,7 @@ int reboot_restore(char *filename, initHookState init_state)
errlogSevPrintf(errlogInfo, "*** restoring from '%s' at initHookState %d (%s record/device init) ***\n",
fname, (int)init_state, pass ? "after" : "before");
if ((inp_fd = fopen_and_check(fname, &status)) == NULL) {
errlogSevPrintf(errlogInfo, "save_restore: Can't open save file.");
errlogSevPrintf(errlogInfo, "save_restore: Can't open save file '%s'.\n\n", filename);
if (pStatusVal) *pStatusVal = SR_STATUS_FAIL;
if (statusStr) strNcpy(statusStr, "Can't open save file.", STATUS_STR_LEN-1);
dbFinishEntry(pdbentry);
Expand Down Expand Up @@ -1163,8 +1163,8 @@ FILE *checkFile(const char *file)

if (save_restoreDebug >= 2) printf("checkFile: entry\n");

if ((inp_fd = fopen(file, "r")) == NULL) {
errlogSevPrintf(errlogInfo, "save_restore: Can't open file '%s'.\n", file);
if ((inp_fd = openShared(file, "r")) == NULL) {
errlogSevPrintf(errlogInfo, "save_restore: Can't open file '%s': %s.\n", file, strerror(errno));
return(0);
}

Expand Down Expand Up @@ -1298,7 +1298,7 @@ FILE *fopen_and_check(const char *fname, long *status)
backup_sequence_num = 0;
}

errlogSevPrintf(errlogInfo, "save_restore: Can't find a file to restore from...");
errlogSevPrintf(errlogInfo, "save_restore: Can't find a file to restore from...\n");
errlogSevPrintf(errlogInfo, "save_restore: ...last tried '%s'. I give up.\n", file);
printf("save_restore: **********************************\n\n");
return(0);
Expand Down Expand Up @@ -1470,8 +1470,8 @@ void makeAutosaveFileFromDbInfo(char *fileBaseName, char *info_name)
}
epicsSnprintf(fname, strlen(fileBaseName)+sizeof(".req"), "%s.req", fileBaseName);
}
if ((out_fd = fopen(fname,"w")) == NULL) {
errlogPrintf("save_restore:makeAutosaveFileFromDbInfo - unable to open file '%s'\n", fname);
if ((out_fd = openShared(fname,"w")) == NULL) {
errlogPrintf("save_restore:makeAutosaveFileFromDbInfo - unable to open file '%s': %s\n", fname, strerror(errno));
free(falloc);
return;
}
Expand Down Expand Up @@ -1537,7 +1537,7 @@ int eraseFile(const char *filename) {
printf("save_restore:eraseFile: macEnvExpand('%s') returned NULL\n", filename);
return(ERROR);
}
if ((fd = fopen(fname, "w")) != NULL) {
if ((fd = openShared(fname, "w")) != NULL) {
fclose(fd);
}
free(fname);
Expand All @@ -1554,7 +1554,7 @@ int appendToFile(const char *filename, const char *line) {
printf("save_restore:appendToFile: macEnvExpand('%s') returned NULL\n", filename);
return(ERROR);
}
if ((fd = fopen(fname, "a")) != NULL) {
if ((fd = openShared(fname, "a")) != NULL) {
fprintf(fd, "%s\n", line);
fclose(fd);
} else {
Expand Down
29 changes: 16 additions & 13 deletions asApp/src/save_restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@
#include "osdNfs.h" /* qiao: routine of os dependent code, for NFS */
#include "configMenuClient.h"

#define SET_FILE_PERMISSIONS 1

#ifdef _WIN32
#define SET_FILE_PERMISSIONS 0
#else
#define SET_FILE_PERMISSIONS 1
#endif

#ifdef vxWorks
Expand Down Expand Up @@ -669,7 +670,7 @@ int findConfigFiles(char *config, ELLLIST *configMenuList) {
strNcpy(pLI->name, thisname, strlen(thisname)+1);
if (save_restoreDebug) printf("findConfigFiles: found config file '%s'\n", pLI->name);
makeNfsPath(fullpath, saveRestoreFilePath, filename);
if ((fd = fopen(fullpath, "r"))) {
if ((fd = openShared(fullpath, "r"))) {
if (save_restoreDebug) printf("findConfigFiles: searching '%s' for description\n", fullpath);
found = 0;
while (!found && (bp=fgets(buffer, BUF_SIZE, fd))) {
Expand Down Expand Up @@ -1066,12 +1067,13 @@ STATIC int save_restore(void)

/* qiao: check the call back timeout if the save method is periodic or monitored */
if ((plist->save_method & PERIODIC) || (plist->save_method & MONITORED) == MONITORED) {
double tdiff;
if ((save_restoreCallbackTimeout > MIN_PERIOD) &&
(epicsTimeDiffInSeconds(&currTime, &plist->callback_time) > save_restoreCallbackTimeout)) {
( (tdiff = epicsTimeDiffInSeconds(&currTime, &plist->callback_time)) > save_restoreCallbackTimeout)) {
plist->save_state = plist->save_method;

if (save_restoreDebug > 1)
printf("save_restore: Callback time out of %s, force to save!\n", plist->reqFile);
printf("save_restore: Callback time out of %s %f > %d, force to save!\n", plist->reqFile, tdiff, save_restoreCallbackTimeout);
}
}

Expand Down Expand Up @@ -1683,7 +1685,7 @@ STATIC int check_file(char *file)
char tmpstr[20];
int file_state = BS_NONE;

if ((fd = fopen(file, "r")) != NULL) {
if ((fd = openShared(file, "r")) != NULL) {
if (fseek(fd, -7, SEEK_END)) {
printf("save_restore:check_file: seek failed\n");
file_state = BS_BAD;
Expand Down Expand Up @@ -1792,6 +1794,7 @@ STATIC int write_it(char *filename, struct chlist *plist)

/* open the file */
errno = 0;
/* WIN32 has SET_FILE_PERMISSIONS == 0 so we have not added an _sopen() section here */
#if SET_FILE_PERMISSIONS
/* Note: must truncate, else file retains old characters when its used length decreases. */
filedes = open(filename, O_RDWR | O_CREAT | O_TRUNC, file_permissions);
Expand All @@ -1818,7 +1821,7 @@ STATIC int write_it(char *filename, struct chlist *plist)
out_fd = fdopen(filedes, "w");
}
#else
if ((out_fd = fopen(filename,"w")) == NULL) {
if ((out_fd = openShared(filename,"w")) == NULL) {
printf("save_restore:write_it - unable to open file '%s' [%s]\n",
filename, datetime);
if (errno) myPrintErrno("write_it", __FILE__, __LINE__);
Expand Down Expand Up @@ -2067,8 +2070,8 @@ STATIC int write_save_file(struct chlist *plist, const char *configName, char *r
/* Ensure that backup is ok before we overwrite .sav file. */
backup_state = check_file(backup_file);
if (backup_state != BS_OK) {
printf("save_restore:write_save_file: Backup file (%s) bad or not found. Writing a new one. [%s]\n",
backup_file, datetime);
printf("save_restore:write_save_file: Backup file (%s) %s. Writing a new one. [%s]\n",
backup_file, (backup_state != BS_NONE ? "bad" : "not found"), datetime);
if (backup_state == BS_BAD) {
/* make a backup copy of the corrupted file */
strNcpy(tmpstr, backup_file, TMPSTRLEN);
Expand Down Expand Up @@ -2099,7 +2102,7 @@ STATIC int write_save_file(struct chlist *plist, const char *configName, char *r
char datetime[32];
FILE *test_fd;

if ((test_fd = fopen(save_file,"rb")) != NULL) {
if ((test_fd = openShared(save_file,"rb")) != NULL) {
fGetDateStr(datetime);
strNcpy(backup_file, save_file, NFS_PATH_LEN);
strncat(backup_file, "_", NFS_PATH_LEN-strlen(backup_file));
Expand Down Expand Up @@ -3341,8 +3344,8 @@ STATIC int do_manual_restore(char *filename, int file_type, char *macrostring)
status = SR_put_array_values(pchannel->name, pchannel->pArray, pchannel->curr_elements);
if (status) printf("do_manual_restore:SR_put_array_values() to '%s'failed.\n", pchannel->name);
}
if (status) num_errs++;
}
if (status) num_errs++;
if (ca_pend_io(1.0) != ECA_NORMAL) {
printf("save_restore:do_manual_restore: not all channels restored\n");
}
Expand All @@ -3367,7 +3370,7 @@ STATIC int do_manual_restore(char *filename, int file_type, char *macrostring)
if (file_type == FROM_SAVE_FILE) {
inp_fd = fopen_and_check(restoreFile, &status);
} else {
inp_fd = fopen(restoreFile, "r");
inp_fd = openShared(restoreFile, "r");
}
if (inp_fd == NULL) {
printf("save_restore:do_manual_restore: Can't open save file.");
Expand Down Expand Up @@ -3585,12 +3588,12 @@ int openReqFile(const char *reqFile, FILE **fpp)
/* try to find reqFile in every directory specified in reqFilePathList */
for (p = reqFilePathList; p; p = p->pnext) {
makeNfsPath(tmpfile, p->path, reqFile);
trial_fd = fopen(tmpfile, "r");
trial_fd = openShared(tmpfile, "r");
if (trial_fd) break;
}
} else {
/* try to find reqFile only in current working directory */
trial_fd = fopen(reqFile, "r");
trial_fd = openShared(reqFile, "r");
}
if (fpp) *fpp = trial_fd;
if (trial_fd) {
Expand Down
2 changes: 2 additions & 0 deletions asApp/src/save_restore.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ extern int eraseFile(const char *filename);
extern int appendToFile(const char *filename, const char *line);
extern float mySafeDoubleToFloat(double d);

extern FILE* openShared(const char* filename, const char* mode);

/* strncpy sucks (may copy extra characters, may not null-terminate) */
#define strNcpy(dest, src, N) { \
int ii; \
Expand Down
26 changes: 22 additions & 4 deletions asApp/src/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#include "save_restore.h"

#ifdef _WIN32
#include <share.h> /* for _fsopen */
#endif /* _WIN32 */

#ifndef PVNAME_STRINGSZ
#define PVNAME_STRINGSZ 61 /* includes terminating null */
#endif
Expand Down Expand Up @@ -49,14 +53,14 @@ int do_asVerify(char *fileName, int verbose, int debug, int write_restore_file,
long element_count=0, storageBytes=0, alloc_CA_buffer=0;


fp = fopen(fileName,"r");
fp = openShared(fileName,"r");
if (fp == NULL) {printf("asVerify: Can't open '%s'.\n", fileName); return(-1);}

if (write_restore_file) {
strcpy(trial_restoreFileName, restoreFileName);
strcat(trial_restoreFileName, "B");
if (debug) {printf("asVerify: restoreFileName '%s'.\n", restoreFileName);}
fr = fopen(trial_restoreFileName,"w");
fr = openShared(trial_restoreFileName,"w");
if (fr == NULL) {
printf("asVerify: Can't open restore_file '%s' for writing.\n", trial_restoreFileName);
write_restore_file = 0;
Expand Down Expand Up @@ -376,8 +380,8 @@ int do_asVerify(char *fileName, int verbose, int debug, int write_restore_file,
fclose(fr);
if (numPVsNotConnected < numPVs/2) {
/* copy trial restore file to real restore file */
fr = fopen(trial_restoreFileName,"r");
fr1 = fopen(restoreFileName,"w");
fr = openShared(trial_restoreFileName,"r");
fr1 = openShared(restoreFileName,"w");
while ((bp=fgets(s, BUF_SIZE, fr))) {
fputs(s, fr1);
}
Expand Down Expand Up @@ -576,3 +580,17 @@ long read_array(FILE *fp, char *PVname, char *value_string, short field_type, lo

return(status);
}

/* open a file with shared access */
FILE* openShared(const char* filename, const char* mode)
{
#ifdef _WIN32
if ( strchr(mode, 'r') && !strchr(mode, '+') ) {
return _fsopen(filename, mode, _SH_DENYNO); /* we are only reading, allow others all access */
} else {
return _fsopen(filename, mode, _SH_DENYWR); /* we may write, so deny others write access */
}
#else
return fopen(filename, mode);
#endif /* _WIN32 */
}