Hi,

We found out that the following simple command fails on Solaris with:

 cat srcfile.txt | /usr/gnu/bin/cp /dev/stdin dstfile.txt
 cp: skipping file '/dev/stdin', as it was replaced while being copied

I found that problem is with SAME_INODE macro. It accepts two structures, one from stat and another from fstat function. On Solaris, each of these can return a different thing. While stat returns information about the /dev/fd/0 file itself (linked by /dev/stdin), fstat knows much more from the file descriptor and returns info about the pipe that is being used. That results in SAME_INODE failing.

This happens in both Coreutils 8.30 and 8.31 (both intel and sparc) but it looks like it was seen first in 8.16.

The easiest fix to this issue I came up with is to disable SAME_INODE validation for special devices and pipes (as they won't be moved anyway). Patch is attached as well as small program that can show stat and fstat differences on Solaris.

Thanks
Jakub

--- src/copy.c
+++ src/copy.c
@@ -1050,11 +1050,18 @@ copy_reg (char const *src_name, char con
      saved ones obtained via a previous call to stat.  */
   if (! SAME_INODE (*src_sb, src_open_sb))
     {
-      error (0, 0,
-             _("skipping file %s, as it was replaced while being copied"),
-             quoteaf (src_name));
-      return_val = false;
-      goto close_src_desc;
+      /* Stat and fstat returns different results on Solaris for devices
+         like /dev/stdin. Confirm that this is not the case.  */
+      mode_t file_type = src_open_sb.st_mode & S_IFMT;
+      if (!(file_type == S_IFBLK || file_type == S_IFCHR ||
+	         file_type == S_IFIFO || file_type == S_IFSOCK))
+        {
+          error (0, 0,
+                 _("skipping file %s, as it was replaced while being copied"),
+                 quoteaf (src_name));
+          return_val = false;
+          goto close_src_desc;
+        }
     }
 
   /* The semantics of the following open calls are mandated
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() {

  int source_desc;
  struct stat sb;
  char* src_name = "/dev/stdin";

  source_desc = open(src_name, (O_RDONLY));
  if (source_desc < 0) {
    printf("cannot open %s for reading\n", src_name);
    return 1;
  }

  if (fstat (source_desc, &sb) != 0) {
    printf("cannot fstat %s\n", src_name);
    return 2;
  }

  printf("fstat results:\n");
  printf("fd: %d\n", source_desc);
  printf("ino: %lu, dev: %lu\n", sb.st_ino, sb.st_dev);

  /* Get the same info from stat */
  if (stat(src_name, &sb) != 0) {
    printf("cannot fstat %s\n", src_name);
    return 3;
  }

  printf("Stat results:\n");
  printf("ino: %lu, dev: %lu\n", sb.st_ino, sb.st_dev);

  return 0;
}

Reply via email to