mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-22 16:18:28 -05:00
ACL: less error prone error handling in ACLSaveToFile().
This commit is contained in:
parent
2d3cad684c
commit
2bea3929d0
29
src/acl.c
29
src/acl.c
@ -1268,7 +1268,9 @@ sds ACLLoadFromFile(const char *filename) {
|
||||
* When C_ERR is returned a log is produced with hints about the issue. */
|
||||
int ACLSaveToFile(const char *filename) {
|
||||
sds acl = sdsempty();
|
||||
int fd;
|
||||
int fd = -1;
|
||||
sds tmpfilename = NULL;
|
||||
int retval = C_ERR;
|
||||
|
||||
/* Let's generate an SDS string containing the new version of the
|
||||
* ACL file. */
|
||||
@ -1291,40 +1293,37 @@ int ACLSaveToFile(const char *filename) {
|
||||
raxStop(&ri);
|
||||
|
||||
/* Create a temp file with the new content. */
|
||||
sds tmpfilename = sdsnew(filename);
|
||||
tmpfilename = sdsnew(filename);
|
||||
tmpfilename = sdscatfmt(tmpfilename,".tmp-%i-%I",
|
||||
(int)getpid(),(int)mstime());
|
||||
if ((fd = open(tmpfilename,O_WRONLY|O_CREAT,0644)) == -1) {
|
||||
serverLog(LL_WARNING,"Opening temp ACL file for ACL SAVE: %s",
|
||||
strerror(errno));
|
||||
sdsfree(tmpfilename);
|
||||
sdsfree(acl);
|
||||
return C_ERR;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Write it. */
|
||||
if (write(fd,acl,sdslen(acl)) != (ssize_t)sdslen(acl)) {
|
||||
serverLog(LL_WARNING,"Writing ACL file for ACL SAVE: %s",
|
||||
strerror(errno));
|
||||
close(fd);
|
||||
unlink(tmpfilename);
|
||||
sdsfree(tmpfilename);
|
||||
sdsfree(acl);
|
||||
return C_ERR;
|
||||
goto cleanup;
|
||||
}
|
||||
close(fd);
|
||||
sdsfree(acl);
|
||||
close(fd); fd = -1;
|
||||
|
||||
/* Let's replace the new file with the old one. */
|
||||
if (rename(tmpfilename,filename) == -1) {
|
||||
serverLog(LL_WARNING,"Renaming ACL file for ACL SAVE: %s",
|
||||
strerror(errno));
|
||||
unlink(tmpfilename);
|
||||
sdsfree(tmpfilename);
|
||||
return C_ERR;
|
||||
goto cleanup;
|
||||
}
|
||||
sdsfree(tmpfilename); tmpfilename = NULL;
|
||||
retval = C_OK; /* If we reached this point, everything is fine. */
|
||||
|
||||
cleanup:
|
||||
if (fd != -1) close(fd);
|
||||
if (tmpfilename) unlink(tmpfilename);
|
||||
sdsfree(tmpfilename);
|
||||
sdsfree(acl);
|
||||
return C_OK;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user