mirror of
git://git.sv.gnu.org/coreutils.git
synced 2026-02-16 12:22:01 +02:00
(full_write): Remove unused decl.
(open_maybe_create): Remove. (touch): Don't record whether the file was created; this isn't possible to do reliably and portably without race conditions. If the file was created and if amtime_now is nonzero, this change means we'll have to do another utime system call, but that's no more harmful than the previous version of this code.
This commit is contained in:
62
src/touch.c
62
src/touch.c
@@ -40,8 +40,6 @@
|
||||
time_t time ();
|
||||
#endif
|
||||
|
||||
int full_write ();
|
||||
|
||||
/* Bitmasks for `change_times'. */
|
||||
#define CH_ATIME 1
|
||||
#define CH_MTIME 2
|
||||
@@ -102,45 +100,6 @@ static int const time_masks[] =
|
||||
CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
|
||||
};
|
||||
|
||||
/* Open FILE, possibly creating it. Set *FILE_CREATED to nonzero if the
|
||||
open creates it, or to zero if the open call opened an existing file.
|
||||
Return the result of the open call. Be careful to avoid race conditions. */
|
||||
|
||||
static int
|
||||
open_maybe_create (const char *file, int *file_created)
|
||||
{
|
||||
int fd;
|
||||
|
||||
*file_created = 0;
|
||||
while (1)
|
||||
{
|
||||
/* First, see if we can create a new FILE. */
|
||||
fd = open (file, O_WRONLY | O_CREAT | O_EXCL,
|
||||
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||
if (fd != -1)
|
||||
*file_created = 1;
|
||||
|
||||
/* If the open succeeded or if it failed for any reason other
|
||||
than the existence of FILE, then we're done. Some systems
|
||||
(solaris) set errno to EINVAL when FILE is a directory. */
|
||||
if (fd != -1 || (errno != EEXIST && errno != EINVAL))
|
||||
break;
|
||||
|
||||
/* The first open failed because FILE already exists.
|
||||
Now try to open it for writing. */
|
||||
fd = open (file, O_WRONLY);
|
||||
|
||||
/* If the open succeeded or if it failed for any reason other
|
||||
than the absence of FILE, then we're done. */
|
||||
/* The 2nd open can fail if FILE was unlinked between the two
|
||||
open calls. When that happens, just iterate. */
|
||||
if (fd != -1 || errno != ENOENT)
|
||||
break;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* Update the time of file FILE according to the options given.
|
||||
Return 0 if successful, 1 if an error occurs. */
|
||||
|
||||
@@ -150,7 +109,6 @@ touch (const char *file)
|
||||
int status;
|
||||
struct stat sbuf;
|
||||
int fd;
|
||||
int file_created;
|
||||
|
||||
if (no_create)
|
||||
{
|
||||
@@ -161,12 +119,12 @@ touch (const char *file)
|
||||
/* FILE doesn't exist. So we're done. */
|
||||
return 0;
|
||||
}
|
||||
file_created = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Try to open FILE, creating it if necessary. */
|
||||
fd = open_maybe_create (file, &file_created);
|
||||
fd = open (file, O_WRONLY | O_CREAT,
|
||||
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||
}
|
||||
|
||||
/* Don't fail if the open failed because FILE is a directory. */
|
||||
@@ -179,21 +137,7 @@ touch (const char *file)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (amtime_now)
|
||||
{
|
||||
if (file_created)
|
||||
{
|
||||
if (close (fd) < 0)
|
||||
{
|
||||
error (0, errno, "%s", file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* We've just created the file with the current times. */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (! amtime_now)
|
||||
{
|
||||
/* We're setting only one of the time values. stat the target to get
|
||||
the other one. If we have the file descriptor already, use fstat,
|
||||
|
||||
Reference in New Issue
Block a user