summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-23 18:53:00 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-23 18:53:00 +0000
commit683bc60673f111397aea0f363c90a24f099026dc (patch)
tree64008096a52a1ad332fbf8db71b30357014de17a /util.c
parent5180e08bf7c272453663480f8d9b93fe707a7021 (diff)
downloadruby-683bc60673f111397aea0f363c90a24f099026dc.tar.gz
ruby-683bc60673f111397aea0f363c90a24f099026dc.tar.xz
ruby-683bc60673f111397aea0f363c90a24f099026dc.zip
* util.c (valid_filename): use O_EXCL to get rid of clobbering
existing files in race conditions. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@15586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'util.c')
-rw-r--r--util.c14
1 files changed, 5 insertions, 9 deletions
diff --git a/util.c b/util.c
index 8a00aedf6..136ff1b21 100644
--- a/util.c
+++ b/util.c
@@ -346,22 +346,18 @@ valid_filename(const char *s)
int fd;
/*
- // if the file exists, then it's a valid filename!
- */
-
- if (_access(s, 0) == 0) {
- return 1;
- }
-
- /*
// It doesn't exist, so see if we can open it.
*/
- if ((fd = _open(s, O_CREAT, 0666)) >= 0) {
+ if ((fd = _open(s, O_CREAT|O_EXCL, 0666)) >= 0) {
_close(fd);
_unlink(s); /* don't leave it laying around */
return 1;
}
+ else if (errno == EEXIST) {
+ /* if the file exists, then it's a valid filename! */
+ return 1;
+ }
return 0;
}
#endif