public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Pipes Again. (was: cmp (or echo) bug?)
@ 2016-01-02 11:58 Houder
  2016-01-02 12:17 ` Pipes Again. (was: cmp (or echo) bug?) -- typo corrected Houder
  2016-01-03 22:47 ` Pipes Again. (was: cmp (or echo) bug?) -- more correction Houder
  0 siblings, 2 replies; 3+ messages in thread
From: Houder @ 2016-01-02 11:58 UTC (permalink / raw)
  To: cygwin

Hi Corinna,

As reported by David BalaÃ…ic in "cmp (or echo) bug?" (December 25, 2015)

     https://cygwin.com/ml/cygwin/2015-12/msg00310.html

execution of

     cmp <(echo foo) <(echo bar)

from bash (note: bash!) fails (nearly always; however the rate of 
failure
may depend on specific conditions).

Failure means that "cmp" reports the two inputs as equal (wrong!).

Although at least one different reason for the failure has been 
suggested
here, I like to propose that the reason for the failure is a deficiency 
in
Cygwin itself (yes, pipes again).
(yes, I think it is Cygwin, not bash, not cmp ...)

As noted by David, "cmp" does not fail on Linux ...

As "diff" (and "stat") did NOT fail, I decided to inspect the source 
code
of "cmp" (and "diff", "stat" ...).

cmp tries to be smart (as does diff): before it actually compares the 
two
inputs, it takes a shortcut by comparing both the device (st_dev) and 
the
i-node (st_ino) of the files specified as arguments.

cmp uses fstat() to obtain device and i-node ... diff and stat use 
stat()
and lstat().

Replacing fstat() by stat() in cmp, makes cmp behave as it should!

Next I started to compare Cygwin and Linux ... (using customized code).

Basically, my customized code (t_stat.c) reads as follows:

     struct stat sb[2];
     int fd[2];

     // process the two arguments - like cmp does
     for (int f = 0; f < 2; f++)
     {
         // drop O_BINARY in case of Linux
         // replace O_RDONLY by O_RDWR for ./tstat <(cat > a) <(cat > b)
         fd[f] = open(argv[1 + f], O_RDONLY | O_BINARY); // as cmp does
         if (fd[f] < 0)
             errExit("open");
         if ( fstat(fd[f], sb + f) != 0 ) // as cmp does
             errExit("fstat");

         // appears to increase the failure rate to "always"
         close(fd[f]); // ... NOT present in cmp
     }
     printf("... arg = %s\n", argv[1]);
     displayStatInfo(sb);
     printf("... arg = %s\n", argv[2]);
     displayStatInfo(sb + 1);
     printf("fd[0] = %u, fd[1] = %u\n", fd[0], fd[1]);

Linux shows:
@@ ./t_stat <(echo foo) <(echo bar)
... arg = /dev/fd/63
File type:                FIFO or pipe
Device containing i-node: (8)   major=0   minor=8
I-node number:            5bc8 - decimal: 23496
... arg = /dev/fd/62
File type:                FIFO or pipe
Device containing i-node: (8)   major=0   minor=8
I-node number:            5bca - decimal: 23498
fd[0] = 3, fd[1] = 3

  - Linux always shows the same value for st_dev; that is, also if 
fstat() is
    replaced by stat() ...
  - Linux always shows different values for both st_ino-s (same call); 
values
    that are different from the ones in subsequent calls

Cygwin shows:
@@ ./t_stat <(echo foo) <(echo bar)
... arg = /dev/fd/63
File type:                FIFO or pipe
Device containing i-node: (c6)   major=0   minor=198
I-node number:            0 - decimal: 0
... arg = /dev/fd/62
File type:                FIFO or pipe
Device containing i-node: (c6)   major=0   minor=198
I-node number:            0 - decimal: 0
fd[0] = 3, fd[1] = 3

And sometimes, especially in case close(fd[f]) is NOT present ...

@@ ./t_stat <(echo foo) <(echo bar)
... arg = /dev/fd/63
File type:                FIFO or pipe
Device containing i-node: (c6)   major=0   minor=198
I-node number:            0 - decimal: 0
... arg = /dev/fd/62
File type:                FIFO or pipe
Device containing i-node: (c6)   major=0   minor=198
I-node number:            5c443bd7b7e540 - decimal: 25970721670292800
fd[0] = 3, fd[1] = 4

  - Cygwin shows 198 for st_dev in case fstat() is used
  - Cygwin shows 199 for st_dev in case stat() is used
  - Cygwin shows 197 for st_dev in case fstat() is used and in case the 
command
    reads as follows:
@@ ./t_stat >(cat > a) >(cat > b) # yes, the opposite case - had to try

  - Cygwin nearly always shows ZERO for both st_ino-s (same call); 
however, if
    one of the st_ino-s is NOT zero, Cygwin always shows the same value:
I-node number:            5c443bd7b7e540 - decimal: 25970721670292800

Bottom-line:

  - instrumenting cmp with the same "diagnostics", yields the same result
  - cmp fails on Cygwin, because Cygwin returns both st_ino-s as equal 
(zero).

My reason for posting this, is to help others in case they stumble over 
this
weird behaviour of Cygwin.

Regards,
Henri

=====

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Pipes Again. (was: cmp (or echo) bug?) -- typo corrected
  2016-01-02 11:58 Pipes Again. (was: cmp (or echo) bug?) Houder
@ 2016-01-02 12:17 ` Houder
  2016-01-03 22:47 ` Pipes Again. (was: cmp (or echo) bug?) -- more correction Houder
  1 sibling, 0 replies; 3+ messages in thread
From: Houder @ 2016-01-02 12:17 UTC (permalink / raw)
  To: cygwin

On 2016-01-02 12:58, Houder wrote:

>         // replace O_RDONLY by O_RDWR for ./tstat <(cat > a) <(cat > b)

should be:

// replace O_RDONLY by O_RDWR for ./t_stat >(cat > a) >(cat > b)

Henri

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Pipes Again. (was: cmp (or echo) bug?) -- more correction
  2016-01-02 11:58 Pipes Again. (was: cmp (or echo) bug?) Houder
  2016-01-02 12:17 ` Pipes Again. (was: cmp (or echo) bug?) -- typo corrected Houder
@ 2016-01-03 22:47 ` Houder
  1 sibling, 0 replies; 3+ messages in thread
From: Houder @ 2016-01-03 22:47 UTC (permalink / raw)
  To: cygwin

On 2016-01-02 12:58, Houder wrote:

> Cygwin shows:
> @@ ./t_stat <(echo foo) <(echo bar)
> ... arg = /dev/fd/63
> File type:                FIFO or pipe
> Device containing i-node: (c6)   major=0   minor=198
> I-node number:            0 - decimal: 0
> ... arg = /dev/fd/62
> File type:                FIFO or pipe
> Device containing i-node: (c6)   major=0   minor=198
> I-node number:            0 - decimal: 0
> fd[0] = 3, fd[1] = 3
> 
> And sometimes, especially in case close(fd[f]) is NOT present ...
> 
> @@ ./t_stat <(echo foo) <(echo bar)
> ... arg = /dev/fd/63
> File type:                FIFO or pipe
> Device containing i-node: (c6)   major=0   minor=198
> I-node number:            0 - decimal: 0
> ... arg = /dev/fd/62
> File type:                FIFO or pipe
> Device containing i-node: (c6)   major=0   minor=198
> I-node number:            5c443bd7b7e540 - decimal: 25970721670292800
> fd[0] = 3, fd[1] = 4
> 
>  - Cygwin shows 198 for st_dev in case fstat() is used
>  - Cygwin shows 199 for st_dev in case stat() is used
>  - Cygwin shows 197 for st_dev in case fstat() is used and in case the 
> command
>    reads as follows:
> @@ ./t_stat >(cat > a) >(cat > b) # yes, the opposite case - had to try
> 
>  - Cygwin nearly always shows ZERO for both st_ino-s (same call); 
> however, if
>    one of the st_ino-s is NOT zero, Cygwin always shows the same value:
> I-node number:            5c443bd7b7e540 - decimal: 25970721670292800

Should be read:

  - Cygwin nearly always shows ZERO ... etc., etc. in case of fstat() 
!!!!!
  - Cygwin always shows different values for both st_ino-s (same call) in 
case of
    stat(); values that are different from the ones in subsequent calls

Sorry,
Henri

> Bottom-line:
> 
>  - instrumenting cmp with the same "diagnostics", yields the same 
> result
>  - cmp fails on Cygwin, because Cygwin returns both st_ino-s as equal 
> (zero).
> 
> My reason for posting this, is to help others in case they stumble over 
> this
> weird behaviour of Cygwin.

=====

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-01-03 22:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-02 11:58 Pipes Again. (was: cmp (or echo) bug?) Houder
2016-01-02 12:17 ` Pipes Again. (was: cmp (or echo) bug?) -- typo corrected Houder
2016-01-03 22:47 ` Pipes Again. (was: cmp (or echo) bug?) -- more correction Houder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).