Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#writeStream does not clear old file contents, should it? #12

Open
LinqLover opened this issue Nov 21, 2023 · 2 comments
Open

#writeStream does not clear old file contents, should it? #12

LinqLover opened this issue Nov 21, 2023 · 2 comments

Comments

@LinqLover
Copy link
Contributor

ref := FileSystem disk workingDirectory / 'fstest.txt'.
ref writeStreamDo: [:stream |
	stream nextPutAll: 'abcde'].
ref contents. "'abcde'"
ref writeStreamDo: [:stream |
	stream nextPutAll: 'XYZ'].
ref contents "'XYZde'"

I would have expected just 'XYZ', analogously to FileStream class>>#forceNewFileNamed:.

But, is this a bug or intended behavior?

This issue can be avoided by sending assureDeleted to the reference prior to opening it for the second write. But this sounds less convenient than Squeak's built-in protocols ... And (to me) this was quite surprising and cost me a lot of time to debug. 😅

LinqLover added a commit to LinqLover/SqueakByExample-english that referenced this issue Nov 21, 2023
@j4yk
Copy link
Collaborator

j4yk commented Nov 22, 2023

You can also send #truncate at the right moment. Hard to say whether it should do that on its own or not without a specificaton... Can you please compare what Pharo's implementation does? So far I usually looked there to find out what was intended. It has the additional benefit that it stays at least a bit consistent across dialects...

@LinqLover
Copy link
Contributor Author

Pharo seems to work the same way. Funnily, both in Squeak and Pharo, MemoryFileSystem always replaces the contents indeed, so it is basically undefined behavior ... Anyway, truncate is a good pointer, thank you. Wondering which is more efficient and idiomatic (also in terms of preserving file permissions/attributes etc.), truncate or ensureDeleted ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants