Hello,

I've recently written to pgsql-ports about a problem with PG7.0 on NT (Subj: [PORTS] initdb problem on NT with 7.0).  Since nobody helped me, I had to find out the reson.  The difference between NT and Linux (for instance) is that "open( path, O_RDWR );" opens a file in text mode.  So sometime less block can be read than required.

I suggest a following patch.  BTW the situation appeared before, see hba.c, pqcomm.c and others.
Mon, 6 Mar 2000 12:06:27 +0600
"Alexei Zakharov" <A.S.Zakharov@inp.nsk.su>
xlog.c.patch for cygwin port.
Hi,

On Mon, 6 Mar 2000 12:06:27 +0600
"Alexei Zakharov" <A.S.Zakharov@inp.nsk.su> wrote:

> I suggest a following patch.  BTW the situation appeared before, 
> see hba.c, pqcomm.c and others.

I propose another patch, against src/include/port/win.h.
If this patch is applied, #ifdef with open() such as following is no
more needed.

--
#ifndef __CYGWIN__
  fd=open(path,flags,mode);
#else
  fd=open(path,flags | O_BINARY,mode);
#endif
--

Comments?

--
Yutaka tanida / S34 Co., Ltd.
tanida@s34.co.jp (Office)
yutaka@marin.or.jp(Private, or if you *HATE* Microsoft Outlook)



*** win.h.orig	Thu Feb 10 17:00:14 2000
--- win.h	Tue Mar 07 14:07:21 2000
***************
*** 10,15 ****
--- 10,35 ----
  #define USE_POSIX_TIME
  #define HAVE_INT_TIMEZONE		/* has int _timezone */
  
+ /* open() must use with O_BINARY flag */
+ 
+ #include<stdarg.h>
+ #include<sys/fcntl.h>
+ 
+ static __inline int pg_cygwin_open(const char *pathname,int flags,...) {
+     va_list va;
+ 	mode_t mode;
+ 	if(flags | O_CREAT) {
+         va_start(va,flags);
+         mode=va_arg(va,int);
+         va_end(va);
+     	return open(pathname,flags | O_BINARY,mode);
+ 	}else{
+     	return open(pathname,flags | O_BINARY);
+ 	}
+ }
+ 
+ #define open pg_cygwin_open
+ 
  #include <cygwin/version.h>
  #if (CYGWIN_VERSION_API_MAJOR >= 0) && (CYGWIN_VERSION_API_MINOR >= 8)
  #define sys_nerr _sys_nerr


Tue, 07 Mar 2000 14:25:22 +0900
yutaka tanida <yutaka@marin.or.jp>
Re: [HACKERS] xlog.c.patch for cygwin port.
Applied.

[Charset Windows-1252 unsupported, skipping...]

[Attachment, skipping...]


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Tue, 7 Mar 2000 18:48:55 -0500 (EST)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: [HACKERS] xlog.c.patch for cygwin port.
This looks interesting.  We could remove some of our ifwin cruft.

---------------------------------------------------------------------------


On Mon, 6 Mar 2000 12:06:27 +0600
"Alexei Zakharov" <A.S.Zakharov@inp.nsk.su> wrote:

> I suggest a following patch.  BTW the situation appeared before, 
> see hba.c, pqcomm.c and others.

I propose another patch, against src/include/port/win.h.
If this patch is applied, #ifdef with open() such as following is no
more needed.

--
#ifndef __CYGWIN__
  fd=open(path,flags,mode);
#else
  fd=open(path,flags | O_BINARY,mode);
#endif
--

Comments?

--
Yutaka tanida / S34 Co., Ltd.
tanida@s34.co.jp (Office)
yutaka@marin.or.jp(Private, or if you *HATE* Microsoft Outlook)



*** win.h.orig	Thu Feb 10 17:00:14 2000
--- win.h	Tue Mar 07 14:07:21 2000
***************
*** 10,15 ****
--- 10,35 ----
  #define USE_POSIX_TIME
  #define HAVE_INT_TIMEZONE		/* has int _timezone */
  
+ /* open() must use with O_BINARY flag */
+ 
+ #include<stdarg.h>
+ #include<sys/fcntl.h>
+ 
+ static __inline int pg_cygwin_open(const char *pathname,int flags,...) {
+     va_list va;
+ 	mode_t mode;
+ 	if(flags | O_CREAT) {
+         va_start(va,flags);
+         mode=va_arg(va,int);
+         va_end(va);
+     	return open(pathname,flags | O_BINARY,mode);
+ 	}else{
+     	return open(pathname,flags | O_BINARY);
+ 	}
+ }
+ 
+ #define open pg_cygwin_open
+ 
  #include <cygwin/version.h>
  #if (CYGWIN_VERSION_API_MAJOR >= 0) && (CYGWIN_VERSION_API_MINOR >= 8)
  #define sys_nerr _sys_nerr


************


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Tue, 7 Mar 2000 19:13:06 -0500 (EST)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: [HACKERS] xlog.c.patch for cygwin port.
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> This looks interesting.  We could remove some of our ifwin cruft.

I have been thinking for quite some time that most of the CYGWIN32
ifdefs represent very poor programming.  Instead of zillions of

#ifndef __CYGWIN32__
	fd = open(filename, O_RDONLY, 0666);
#else
	fd = open(filename, O_RDONLY | O_BINARY, 0666);
#endif

we should have in one include file something like

#ifndef __CYGWIN32__
#define  OPEN_FLAGS_READ   O_RDONLY
#define  OPEN_FLAGS_WRITE  O_WRONLY
// etc for the combinations we need
#else
#define  OPEN_FLAGS_READ   (O_RDONLY | O_BINARY)
#define  OPEN_FLAGS_WRITE  (O_WRONLY | O_BINARY)
// etc
#endif

and then the body of the code would have

	fd = open(filename, OPEN_FLAGS_READ, 0666);

and no ifdef.  This would also provide a single place to tweak open()
flags for other platforms, whereas the existing method is exactly zero
help for any non-CYGWIN platform that wants to add O_BINARY ...

			regards, tom lane

Tue, 07 Mar 2000 20:01:16 -0500
Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] xlog.c.patch for cygwin port. 

Sounds like a *great* bug fix to me ... if you "have better things to do",
I can tackle it ...

On Tue, 7 Mar 2000, Tom Lane wrote:

> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > This looks interesting.  We could remove some of our ifwin cruft.
> 
> I have been thinking for quite some time that most of the CYGWIN32
> ifdefs represent very poor programming.  Instead of zillions of
> 
> #ifndef __CYGWIN32__
> 	fd = open(filename, O_RDONLY, 0666);
> #else
> 	fd = open(filename, O_RDONLY | O_BINARY, 0666);
> #endif
> 
> we should have in one include file something like
> 
> #ifndef __CYGWIN32__
> #define  OPEN_FLAGS_READ   O_RDONLY
> #define  OPEN_FLAGS_WRITE  O_WRONLY
> // etc for the combinations we need
> #else
> #define  OPEN_FLAGS_READ   (O_RDONLY | O_BINARY)
> #define  OPEN_FLAGS_WRITE  (O_WRONLY | O_BINARY)
> // etc
> #endif
> 
> and then the body of the code would have
> 
> 	fd = open(filename, OPEN_FLAGS_READ, 0666);
> 
> and no ifdef.  This would also provide a single place to tweak open()
> flags for other platforms, whereas the existing method is exactly zero
> help for any non-CYGWIN platform that wants to add O_BINARY ...
> 
> 			regards, tom lane
> 
> ************
> 

Marc G. Fournier                   ICQ#7615664               IRC Nick: Scrappy
Systems Administrator @ hub.org 
primary: scrappy@hub.org           secondary: scrappy@{freebsd|postgresql}.org 


Tue, 7 Mar 2000 21:19:09 -0400 (AST)
The Hermit Hacker <scrappy@hub.org>
Re: [HACKERS] xlog.c.patch for cygwin port. 
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > This looks interesting.  We could remove some of our ifwin cruft.
> 
> I have been thinking for quite some time that most of the CYGWIN32
> ifdefs represent very poor programming.  Instead of zillions of
> 
> #ifndef __CYGWIN32__
> 	fd = open(filename, O_RDONLY, 0666);
> #else
> 	fd = open(filename, O_RDONLY | O_BINARY, 0666);
> #endif
> 
> we should have in one include file something like

Do we ever assign a function pointer for open() anywhere.  If so, the
define will not work without some kind of wrapper, right?

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Tue, 7 Mar 2000 20:50:07 -0500 (EST)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: [HACKERS] xlog.c.patch for cygwin port.
>> Bruce Momjian <pgman@candle.pha.pa.us> writes:
>> > This looks interesting.  We could remove some of our ifwin cruft.
>> 
>> I have been thinking for quite some time that most of the CYGWIN32
>> ifdefs represent very poor programming.  Instead of zillions of
>> 
>> #ifndef __CYGWIN32__
>> 	fd = open(filename, O_RDONLY, 0666);
>> #else
>> 	fd = open(filename, O_RDONLY | O_BINARY, 0666);
>> #endif
>> 
>> we should have in one include file something like
>
>Do we ever assign a function pointer for open() anywhere.  If so, the
>define will not work without some kind of wrapper, right?

Since the only difference seems to be "O_RDONLY" vs "O_RDONLY | O_BINARY",
why not do the #define on that?

At least in this case it works.
-- 
D. Jay Newman                   ! For the pleasure and the profit it derives
jay@sprucegrove.com              ! I arrange things, like furniture, and
http://www.sprucegrove.com/~jay/   ! daffodils, and ...lives.  -- Hello Dolly

Tue, 7 Mar 2000 21:48:28 -0500 (EST)
"D. Jay Newman" <jay@sprucegrove.com>
Re: [HACKERS] xlog.c.patch for cygwin port.
> >> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> >> > This looks interesting.  We could remove some of our ifwin cruft.
> >> 
> >> I have been thinking for quite some time that most of the CYGWIN32
> >> ifdefs represent very poor programming.  Instead of zillions of
> >> 
> >> #ifndef __CYGWIN32__
> >> 	fd = open(filename, O_RDONLY, 0666);
> >> #else
> >> 	fd = open(filename, O_RDONLY | O_BINARY, 0666);
> >> #endif
> >> 
> >> we should have in one include file something like
> >
> >Do we ever assign a function pointer for open() anywhere.  If so, the
> >define will not work without some kind of wrapper, right?
> 
> Since the only difference seems to be "O_RDONLY" vs "O_RDONLY | O_BINARY",
> why not do the #define on that?
> 
> At least in this case it works.

BTW, why do we call open() directory here? Why not VFD interface?
--
Tatsuo Ishii

Wed, 08 Mar 2000 12:03:11 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
Re: [HACKERS] xlog.c.patch for cygwin port.
On Tue, 7 Mar 2000, Bruce Momjian wrote:

> > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > This looks interesting.  We could remove some of our ifwin cruft.
> > 
> > I have been thinking for quite some time that most of the CYGWIN32
> > ifdefs represent very poor programming.  Instead of zillions of
> > 
> > #ifndef __CYGWIN32__
> > 	fd = open(filename, O_RDONLY, 0666);
> > #else
> > 	fd = open(filename, O_RDONLY | O_BINARY, 0666);
> > #endif
> > 
> > we should have in one include file something like
> 
> Do we ever assign a function pointer for open() anywhere.  If so, the
> define will not work without some kind of wrapper, right?

Okay, I'm lost ... if we "#define OPEN_FLAGS .." and not the open itself,
why would we need some kind of wrapper?



Wed, 8 Mar 2000 01:42:06 -0400 (AST)
The Hermit Hacker <scrappy@hub.org>
Re: [HACKERS] xlog.c.patch for cygwin port.
> On Tue, 7 Mar 2000, Bruce Momjian wrote:
> 
> > > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > > This looks interesting.  We could remove some of our ifwin cruft.
> > > 
> > > I have been thinking for quite some time that most of the CYGWIN32
> > > ifdefs represent very poor programming.  Instead of zillions of
> > > 
> > > #ifndef __CYGWIN32__
> > > 	fd = open(filename, O_RDONLY, 0666);
> > > #else
> > > 	fd = open(filename, O_RDONLY | O_BINARY, 0666);
> > > #endif
> > > 
> > > we should have in one include file something like
> > 
> > Do we ever assign a function pointer for open() anywhere.  If so, the
> > define will not work without some kind of wrapper, right?
> 
> Okay, I'm lost ... if we "#define OPEN_FLAGS .." and not the open itself,
> why would we need some kind of wrapper?

No, the original person was refining open().  I�think defining the flags
is much better.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Wed, 8 Mar 2000 00:50:15 -0500 (EST)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: [HACKERS] xlog.c.patch for cygwin port.
On Wed, 8 Mar 2000, Bruce Momjian wrote:

> > On Tue, 7 Mar 2000, Bruce Momjian wrote:
> > 
> > > > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > > > This looks interesting.  We could remove some of our ifwin cruft.
> > > > 
> > > > I have been thinking for quite some time that most of the CYGWIN32
> > > > ifdefs represent very poor programming.  Instead of zillions of
> > > > 
> > > > #ifndef __CYGWIN32__
> > > > 	fd = open(filename, O_RDONLY, 0666);
> > > > #else
> > > > 	fd = open(filename, O_RDONLY | O_BINARY, 0666);
> > > > #endif
> > > > 
> > > > we should have in one include file something like
> > > 
> > > Do we ever assign a function pointer for open() anywhere.  If so, the
> > > define will not work without some kind of wrapper, right?
> > 
> > Okay, I'm lost ... if we "#define OPEN_FLAGS .." and not the open itself,
> > why would we need some kind of wrapper?
> 
> No, the original person was refining open().  I�think defining the flags
> is much better.

	Ah, okay, knew I was missing something :)  



Wed, 8 Mar 2000 02:07:28 -0400 (AST)
The Hermit Hacker <scrappy@hub.org>
Re: [HACKERS] xlog.c.patch for cygwin port.
We have resolved all the open()/binary flag calls.



-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Fri, 9 Jun 2000 12:03:17 -0400 (EDT)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: xlog.c.patch for cygwin port.
We have resolved all the O_BINARY calls for 7.1.  Thanks.


> On Mon, 6 Mar 2000 12:06:27 +0600
> "Alexei Zakharov" <A.S.Zakharov@inp.nsk.su> wrote:
> 
> > I suggest a following patch.  BTW the situation appeared before, 
> > see hba.c, pqcomm.c and others.
> 
> I propose another patch, against src/include/port/win.h.
> If this patch is applied, #ifdef with open() such as following is no
> more needed.
> 
> --
> #ifndef __CYGWIN__
>   fd=open(path,flags,mode);
> #else
>   fd=open(path,flags | O_BINARY,mode);
> #endif

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Fri, 9 Jun 2000 12:04:10 -0400 (EDT)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: xlog.c.patch for cygwin port.