<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hi All,&nbsp;</div><div><br></div><div>I've tested a fix for this on OS X and Linux. The fix simply adds an extra attempt to open the file after the "w+b" attempt using this code:</div><div><br></div><div><div><font class="Apple-style-span" face="Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (getFile(f) == NULL) {</font></div><div><font class="Apple-style-span" face="Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setFile(f, fopen(cFileName, "wb"));</font></div><div><font class="Apple-style-span" face="Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</font></div></div><div><br></div><div>If the previous attempts to open the file failed (because the file exists with write-only permission, no read permission) then the above will actually open the file write-only. Adding the extra attempt preserves existing behaviour for the success paths, so the change should be transparent to existing code.</div><div><br></div><div>BTW, AFAICT the same issue exists in Pharo.</div><div><br></div><div>Thoughts?</div><div><br></div><div>Doug</div><div><br></div><br><div><div>On Jun 1, 2014, at 19:44 , Douglas McPherson wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>It turns out #forceNewFileNamed: does not work either.</div><div><br></div><div>I believe the immediate cause of the problem is in the function you pointed me at: <font class="Apple-style-span" face="Courier">sqFileOpen()</font> in&nbsp;<font class="Apple-style-span" face="Courier">platforms/Cross/plugins/FilePlugin/sqFilePluginBasicPrims.c:</font></div><div><br></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">        </span>if (writeFlag) {</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                </span>/* First try to open an existing file read/write: */</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                </span>setFile(f, fopen(cFileName, "r+b"));</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                </span>if (getFile(f) == NULL) {</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                        </span>/* Previous call fails if file does not exist. In that case,</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                        </span> &nbsp; try opening it in write mode to create a new, empty file.</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                        </span>*/</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                        </span>setFile(f, fopen(cFileName, "w+b"));</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                        </span>if (getFile(f) != NULL) {</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                        </span> &nbsp; &nbsp;char type[4],creator[4];</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                                </span>dir_GetMacFileTypeAndCreator(sqFileName, sqFileNameSize, type, creator);</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                                </span>if (strncmp(type,"BINA",4) == 0 || strncmp(type,"????",4) == 0 || *(int *)type == 0 )&nbsp;</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                                </span> &nbsp; &nbsp;dir_SetMacFileTypeAndCreator(sqFileName, sqFileNameSize,"TEXT","R*ch");<span class="Apple-tab-span" style="white-space:pre">        </span></font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                        </span>}</font></div><div><font class="Apple-style-span" face="Courier"><span class="Apple-tab-span" style="white-space:pre">                </span>}</font></div><div><br></div><div>The first call to fopen() indeed fails if the file does not exist. But it also fails if the file exists but does not have both read and write "r+b" permission. So in that case the intent, as you mentioned, is to try to open in write-only but "w+b" is not the write-only mode, it is also a read and write mode, see [1]. So this fopen() attempt also fails if the file has write-only permission. A fix may be to use mode "wb" instead of "w+b", but I'm not sure if there are unintended consequences to doing this.</div><div><br></div><div>[1] <a href="http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdio.h/fopen">http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdio.h/fopen</a>&nbsp; &nbsp;</div><br><div><div>On May 31, 2014, at 14:45 , Douglas McPherson wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="content-type" content="text/html; charset=utf-8"><div dir="auto"><div>Ahh, thanks. I'll try it out when back in front of my machine.</div><div><br></div><div>Thanks,</div><div>Doug<br><br>Sent from my iPhone</div><div><br>On May 31, 2014, at 14:23, Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Hi Doug,<br><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, May 31, 2014 at 10:00 AM, Douglas McPherson <span dir="ltr">&lt;<a href="mailto:djm1329@san.rr.com" target="_blank">djm1329@san.rr.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">Is there a way to open a write-only file in Squeak? On both Mac and Linux attempting to open such a file fails.</div>
</blockquote><div><br></div><div>Squeak has forceNewFileNamed: &amp; forceNewFileNamed:do:. &nbsp;If you look at sqFileOpen in&nbsp;platforms/Cross/plugins/FilePlugin/sqFilePluginBasicPrims.c you'll see it first attempts to open read-write and if that fails, opens write-only. &nbsp;So it should work.</div>
<div><br></div><div>HTH</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">
<div>For example, if you create a write-only file, i.e. in a shell, after cd to default directory<br></div><div><br></div><div><font face="Courier">echo nada &gt; foo</font></div><div><font face="Courier">chmod 200 foo</font></div>
<div><br></div><div>Then from Squeak</div><div><br></div><div><font face="Courier">StandardFileStream fileNamed: 'foo'</font></div><div><br></div><div>returns nil.</div><div><br></div><div>If the file is given read permission (i.e. <font face="Courier">chmod 600 foo</font>) then Squeak can open it.</div>
<div><br></div><div>Anyone know a workaround?</div><div><br></div><div>Thanks,</div><div>Doug</div></div></blockquote></div><br clear="all"><div><br></div>-- <br>best,<div>Eliot</div>
</div></div>
</div></blockquote><blockquote type="cite"><div><span></span><br></div></blockquote></div><br></blockquote></div><br></div><br></blockquote></div><br></body></html>