Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
2bea3929
Commit
2bea3929
authored
Feb 22, 2019
by
antirez
Browse files
ACL: less error prone error handling in ACLSaveToFile().
parent
2d3cad68
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/acl.c
View file @
2bea3929
...
@@ -1268,7 +1268,9 @@ sds ACLLoadFromFile(const char *filename) {
...
@@ -1268,7 +1268,9 @@ sds ACLLoadFromFile(const char *filename) {
* When C_ERR is returned a log is produced with hints about the issue. */
* When C_ERR is returned a log is produced with hints about the issue. */
int
ACLSaveToFile
(
const
char
*
filename
)
{
int
ACLSaveToFile
(
const
char
*
filename
)
{
sds
acl
=
sdsempty
();
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
/* Let's generate an SDS string containing the new version of the
* ACL file. */
* ACL file. */
...
@@ -1291,40 +1293,37 @@ int ACLSaveToFile(const char *filename) {
...
@@ -1291,40 +1293,37 @@ int ACLSaveToFile(const char *filename) {
raxStop
(
&
ri
);
raxStop
(
&
ri
);
/* Create a temp file with the new content. */
/* Create a temp file with the new content. */
sds
tmpfilename
=
sdsnew
(
filename
);
tmpfilename
=
sdsnew
(
filename
);
tmpfilename
=
sdscatfmt
(
tmpfilename
,
".tmp-%i-%I"
,
tmpfilename
=
sdscatfmt
(
tmpfilename
,
".tmp-%i-%I"
,
(
int
)
getpid
(),(
int
)
mstime
());
(
int
)
getpid
(),(
int
)
mstime
());
if
((
fd
=
open
(
tmpfilename
,
O_WRONLY
|
O_CREAT
,
0644
))
==
-
1
)
{
if
((
fd
=
open
(
tmpfilename
,
O_WRONLY
|
O_CREAT
,
0644
))
==
-
1
)
{
serverLog
(
LL_WARNING
,
"Opening temp ACL file for ACL SAVE: %s"
,
serverLog
(
LL_WARNING
,
"Opening temp ACL file for ACL SAVE: %s"
,
strerror
(
errno
));
strerror
(
errno
));
sdsfree
(
tmpfilename
);
goto
cleanup
;
sdsfree
(
acl
);
return
C_ERR
;
}
}
/* Write it. */
/* Write it. */
if
(
write
(
fd
,
acl
,
sdslen
(
acl
))
!=
(
ssize_t
)
sdslen
(
acl
))
{
if
(
write
(
fd
,
acl
,
sdslen
(
acl
))
!=
(
ssize_t
)
sdslen
(
acl
))
{
serverLog
(
LL_WARNING
,
"Writing ACL file for ACL SAVE: %s"
,
serverLog
(
LL_WARNING
,
"Writing ACL file for ACL SAVE: %s"
,
strerror
(
errno
));
strerror
(
errno
));
close
(
fd
);
goto
cleanup
;
unlink
(
tmpfilename
);
sdsfree
(
tmpfilename
);
sdsfree
(
acl
);
return
C_ERR
;
}
}
close
(
fd
);
close
(
fd
);
fd
=
-
1
;
sdsfree
(
acl
);
/* Let's replace the new file with the old one. */
/* Let's replace the new file with the old one. */
if
(
rename
(
tmpfilename
,
filename
)
==
-
1
)
{
if
(
rename
(
tmpfilename
,
filename
)
==
-
1
)
{
serverLog
(
LL_WARNING
,
"Renaming ACL file for ACL SAVE: %s"
,
serverLog
(
LL_WARNING
,
"Renaming ACL file for ACL SAVE: %s"
,
strerror
(
errno
));
strerror
(
errno
));
unlink
(
tmpfilename
);
goto
cleanup
;
sdsfree
(
tmpfilename
);
return
C_ERR
;
}
}
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
(
tmpfilename
);
sdsfree
(
acl
);
return
C_OK
;
return
C_OK
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment