dos2unix: do not destroy symlinks and mode of the file being converted.

This commit is contained in:
Denis Vlasenko 2008-06-14 04:28:41 +00:00
parent 21765fa063
commit a1767a1f5d
2 changed files with 18 additions and 16 deletions

View File

@ -24,24 +24,27 @@ static void convert(char *fn, int conv_type)
{ {
FILE *in, *out; FILE *in, *out;
int i; int i;
char *name_buf = name_buf; /* for compiler */ char *temp_fn = temp_fn; /* for compiler */
char *resolved_fn = resolved_fn;
in = stdin; in = stdin;
out = stdout; out = stdout;
if (fn != NULL) { if (fn != NULL) {
in = xfopen(fn, "r"); struct stat st;
/*
The file is then created with mode read/write and resolved_fn = xmalloc_follow_symlinks(fn);
permissions 0666 for glibc 2.0.6 and earlier or if (resolved_fn == NULL)
0600 for glibc 2.0.7 and later. bb_simple_perror_msg_and_die(fn);
*/ in = xfopen(resolved_fn, "r");
name_buf = xasprintf("%sXXXXXX", fn); fstat(fileno(in), &st);
i = mkstemp(name_buf);
temp_fn = xasprintf("%sXXXXXX", resolved_fn);
i = mkstemp(temp_fn);
if (i == -1 if (i == -1
|| fchmod(i, 0600) == -1 || fchmod(i, st.st_mode) == -1
|| !(out = fdopen(i, "w+")) || !(out = fdopen(i, "w+"))
) { ) {
bb_perror_nomsg_and_die(); bb_simple_perror_msg_and_die(temp_fn);
} }
} }
@ -56,12 +59,12 @@ static void convert(char *fn, int conv_type)
if (fn != NULL) { if (fn != NULL) {
if (fclose(in) < 0 || fclose(out) < 0) { if (fclose(in) < 0 || fclose(out) < 0) {
unlink(name_buf); unlink(temp_fn);
bb_perror_nomsg_and_die(); bb_perror_nomsg_and_die();
} }
// TODO: destroys symlinks. See how passwd handles this xrename(temp_fn, resolved_fn);
xrename(name_buf, fn); free(temp_fn);
free(name_buf); free(resolved_fn);
} }
} }

View File

@ -54,7 +54,6 @@ char *xmalloc_follow_symlinks(const char *path)
goto jump_in; goto jump_in;
while (1) { while (1) {
linkpath = xmalloc_readlink(buf); linkpath = xmalloc_readlink(buf);
if (!linkpath) { if (!linkpath) {
/* not a symlink, or doesn't exist */ /* not a symlink, or doesn't exist */