Closed Bug 265938 Opened 20 years ago Closed 20 years ago

seek() method of fileoutputstream works incorrectly

Categories

(Core :: XPCOM, defect)

x86
Windows 2000
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: surkov, Assigned: dougt)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040616
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040616

When I initialize output stream as PR_APPEND flag then method seek() don't
change current position of stream (i check it by method tell()) but write()
method of stream writes at the end of stream.
I think more correct behaviour is seek() method fails or current position is
changed in reality.


Reproducible: Always
Steps to Reproduce:
var file=Components.classes["@mozilla.org/file/local;1"].createInstance()
	.QueryInterface(Components.interfaces.nsILocalFile);	
file.initWithPath(path);
var
stream=Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance()
	.QueryInterface(Components.interfaces.nsIFileOutputStream);
stream.init(file, 0x04|0x08|0x10, null, null);

stream.write("one", 3);

var seeker=stream.QueryInterface(Components.interfaces.nsISeekableStream);
seeker.seek(seeker.NS_SEEK_SET, 0);

stream.write("two", 3);
Actual Results:  
File contains string "onetwo".

Expected Results:  
1. file should contain a string "two" or
2. seeker.seek() method should fail.
hm... the mozilla code looks like:
    nsInt64 cnt = PR_Seek64(mFD, offset, (PRSeekWhence)whence);
    if (cnt == nsInt64(-1)) {
        return NS_ErrorAccordingToNSPR();

maybe this is an nspr bug? cc'ing wchang.
Summary: seek() method of nsISeekableStream interface works incorrect → seek() method of fileoutputstream works incorrectly
The definition of PR_APPEND is that "The file pointer
is set to the end of the file prior to each write."
(See http://www.mozilla.org/projects/nspr/reference/html/priofnc.html#17649.)
So your test program produces the expected output.

Could you add a tell() method call after the seek()?
I don't understand whether the method seek() changes
the current position of stream or not.
I agree I get expected result. Method seek() changes current position of stream
becasue method tell() shows changed position. I guess position of stream is
changed by method seek() automatically before writing in method write() of
stream opened with flag PR_APPEND.

But I think behaviour of seek() is involved and unintelligible in this case.
Method seek() should not give the impression that position of a stream was changed.
This is the correct statement:

  Method seek() changes the position of a stream,
  regardless of flag PR_APPEND.

However,

  the position of stream is changed by method write()
  automatically before writing to stream opened with
  flag PR_APPEND.

In other words, if a stream is opened with flag
PR_APPEND, both method seek() and method write()
change the position of stream.
Status: UNCONFIRMED → RESOLVED
Closed: 20 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.