On Wed 12. of December 2007 12:08:43 Jim Meyering wrote:
> Radek Brich <[EMAIL PROTECTED]> wrote:
> > On Tue 11. of December 2007 13:55:53 you wrote:
> >> Radek Brich <[EMAIL PROTECTED]> ha escrit:
> >> > Although it'd need some cleaning, I'd like to hear what
> >> > you think about the idea?
> >>
> >> Generally speaking, looks nice, except that fstat may fail (e.g. if
> >> stdout is closed) in which case st_stdout would contain garbage.
>
> ...
>
> > +  struct stat st_buf;
> > +  ino_t stdout_ino = fstat(STDOUT_FILENO, &st_buf) == -1 ?
> > +                     0 : st_buf.st_ino;
> > +  ino_t archive_ino = stat(archive_name_array[0], &st_buf) == -1 ?
> > +                     0 : st_buf.st_ino;
> > +  bool archive_is_stdout = (archive_ino != 0 && archive_ino ==
> > stdout_ino);
>
> You'll want to compare st_dev, as well.
> Otherwise, there's a small risk of a false positive with
> a file on a different partition, but with the same inode number.
>
> You can use the SAME_INODE macro from gnulib's lib/same-inode.h,
> since tar already has that file.

Yes, thanks, you are right.

I also tried to make it look less alien in the rest of the code.

-- 
           Radek Brich
   /__\    Base OS/core services
 ~~~~~~~~  Red Hat, Brno, Czech Rep.
diff -ur tar-1.19/lib/system.h tar-1.19-stderrlist/lib/system.h
--- tar-1.19/lib/system.h	2007-06-27 15:49:45.000000000 +0200
+++ tar-1.19-stderrlist/lib/system.h	2007-12-12 13:11:14.000000000 +0100
@@ -448,6 +448,7 @@
 #include <savedir.h>
 #include <unlocked-io.h>
 #include <xalloc.h>
+#include <same-inode.h>
 
 #include <gettext.h>
 #define _(msgid) gettext (msgid)
diff -ur tar-1.19/src/buffer.c tar-1.19-stderrlist/src/buffer.c
--- tar-1.19/src/buffer.c	2007-08-26 10:56:55.000000000 +0200
+++ tar-1.19-stderrlist/src/buffer.c	2007-12-12 13:13:40.000000000 +0100
@@ -448,6 +448,7 @@
 _open_archive (enum access_mode wanted_access)
 {
   int backed_up_flag = 0;
+  bool archive_is_stdout;
 
   if (record_size == 0)
     FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
@@ -469,6 +470,8 @@
 
   records_read = 0;
 
+  archive_is_stdout = sys_name_is_stdout (archive_name_array[0]);
+
   if (use_compress_program_option)
     {
       switch (wanted_access)
@@ -490,10 +493,10 @@
 
       if (!index_file_name
 	  && wanted_access == ACCESS_WRITE
-	  && strcmp (archive_name_array[0], "-") == 0)
+	  && (strcmp (archive_name_array[0], "-") == 0 || archive_is_stdout))
 	stdlis = stderr;
     }
-  else if (strcmp (archive_name_array[0], "-") == 0)
+  else if (strcmp (archive_name_array[0], "-") == 0 || archive_is_stdout)
     {
       read_full_records = true; /* could be a pipe, be safe */
       if (verify_option)
diff -ur tar-1.19/src/common.h tar-1.19-stderrlist/src/common.h
--- tar-1.19/src/common.h	2007-09-26 23:42:25.000000000 +0200
+++ tar-1.19-stderrlist/src/common.h	2007-12-12 13:01:13.000000000 +0100
@@ -702,6 +702,7 @@
 bool sys_compare_uid (struct stat *a, struct stat *b);
 bool sys_compare_gid (struct stat *a, struct stat *b);
 bool sys_file_is_archive (struct tar_stat_info *p);
+bool sys_name_is_stdout (char *name);
 bool sys_compare_links (struct stat *link_data, struct stat *stat_data);
 int sys_truncate (int fd);
 pid_t sys_child_open_for_compress (void);
diff -ur tar-1.19/src/system.c tar-1.19-stderrlist/src/system.c
--- tar-1.19/src/system.c	2007-08-26 10:56:55.000000000 +0200
+++ tar-1.19-stderrlist/src/system.c	2007-12-12 12:59:53.000000000 +0100
@@ -37,6 +37,12 @@
   return false;
 }
 
+bool
+sys_name_is_stdout (char *name)
+{
+  return false;
+}
+
 void
 sys_save_archive_dev_ino (void)
 {
@@ -133,6 +139,16 @@
   return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
 }
 
+bool
+sys_name_is_stdout (char *name)
+{
+  struct stat st_stdout, st_name;
+  if (!fstat(STDOUT_FILENO, &st_stdout) && !stat(name, &st_name))
+    return SAME_INODE(st_stdout, st_name);
+  else
+    return false;
+}
+
 /* Save archive file inode and device numbers */
 void
 sys_save_archive_dev_ino (void)

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to