diff options
author | Manoj Srivastava <srivasta@debian.org> | 2009-05-01 13:53:05 -0500 |
---|---|---|
committer | Clint Adams <schizo@debian.org> | 2009-05-01 17:56:39 -0400 |
commit | 965886b62fddbf98cfced65e923e5d89990bfa08 (patch) | |
tree | 48709c730238a5ca79f3c05452341af95be7c1c3 /tempfile.c | |
parent | 60139002e13ba98ef48ddcf324c57f44507c6336 (diff) | |
download | debianutils-965886b62fddbf98cfced65e923e5d89990bfa08.tar.gz |
tempfile with a suffix no longer needs hardlinks
"tempfile -s" did not work on FAT filesystems, since the implementaion
required hardlinks. With this commit, we add the suffix to the output of
tempnam early, and create the suffixed file, instead f creating the
unsuffixed file, harlinking to the suffixed file, and unlinking.
Signed-Off-By: Manoj Srivastava <srivasta@debian.org>
Signed-off-by: Manoj Srivastava <srivasta@debian.org>
Diffstat (limited to 'tempfile.c')
-rw-r--r-- | tempfile.c | 40 |
1 files changed, 16 insertions, 24 deletions
@@ -59,7 +59,7 @@ parsemode (const char *in, mode_t *out) int main (int argc, char **argv) { - char *name=0, *dir=0, *pfx=0, *sfx=0; + char *name=0, *dir=0, *pfx=0, *sfx=0, *filename=0; mode_t mode = 0600; int fd, optc; struct option long_options[] = { @@ -117,41 +117,33 @@ main (int argc, char **argv) for (;;) { if (!(name = tempnam(dir, pfx))) syserror("tempnam"); - if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, mode)) < 0) { + if(sfx) { + char *namesfx; + if (!(namesfx = (char *)malloc(strlen(name) + strlen(sfx) + 1))) + syserror("malloc"); + strcpy(namesfx, name); + strcat(namesfx, sfx); + filename = namesfx; + } + else + filename = name; + + if ((fd = open(filename, O_RDWR | O_CREAT | O_EXCL, mode)) < 0) { if (errno == EEXIST) { free(name); + free(filename); continue; } syserror("open"); } - if (sfx) { - char *namesfx; - if (!(namesfx = malloc(strlen(name) + strlen(sfx) + 1))) - syserror("malloc"); - strcpy(namesfx, name); - strcat(namesfx, sfx); - if (link(name, namesfx) < 0) { - if (errno == EEXIST) { - if (unlink(name) < 0) - syserror("unlink"); - free(name); - free(namesfx); - continue; - } - syserror("link"); - } - if (unlink(name) < 0) - syserror("unlink"); - free(name); - name = namesfx; - } break; } } if (close(fd)) syserror("close"); - puts(name); + puts(filename); + free(filename); free(name); exit(0); } |