This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

File truncation


While testing I/O under win32 I discovered how FileDescriptor performs
truncation.  Currently there is no explicit flag for truncation, and it
isn't done consistently.

Specifically, the Posix FileDescriptor::open method truncates when WRITE
is set but READ and APPEND are not:

           O_RDONLY  O_WRONLY  O_RDWR    O_TRUNC   O_APPEND  O_EXCL
READ           X
WRITE                    X                   X
READ|WRITE                        X
WRITE|APPEND             X                             X
WRITE|EXCL               X                                      X

This happens to work because no other combination of flags is ever used
in java.io.  But the implicit truncate isn't clear (at least to me) and
new ports are likely to get it wrong (as win32 did).  Moreover, somebody
might sometime use FileDescriptor from native code in a way that it
isn't yet used, and get unintended side effects.

I propose adding a new flag to FileDescriptor.java:

  static final int TRUNC = 16;

and set it explicitly in FileOutputStream:

  fd = new FileDescriptor (path, (append
	? FileDescriptor.APPEND : FileDescriptor.TRUNC)
	| FileDescriptor.WRITE);

Any objections?

--
Jeff Sturm
jsturm@sigma6.com

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]