Unix VM file locking?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Unix VM file locking?

Andreas.Raab
 
Hi -

Had a fun morning this morning with some customer trouble which was
partly caused by the Unix VM not using write locks. E.g., the Unix VM
will happily do things like:

f1 := FileStream newFileNamed: 'foo.bar'.
f1 nextPutAll: 'Hello World'.
f2 := FileStream oldFileNamed: 'foo.bar'.
f2 nextPutAll: 'Oh, noes'.

etc. Is there an easy way to patch the Unix file code to avoid this,
e.g., behave like Windows in such that it would disallow opening the
same file for multiple writes?

Cheers,
   - Andreas
Reply | Threaded
Open this post in threaded view
|

Re: Unix VM file locking?

David T. Lewis
 
On Fri, Jan 25, 2008 at 05:12:39PM -0800, Andreas Raab wrote:

> Colin Putney wrote:
> >
> >On 25-Jan-08, at 10:38 AM, Andreas Raab wrote:
> >
> >>etc. Is there an easy way to patch the Unix file code to avoid this,
> >>e.g., behave like Windows in such that it would disallow opening the
> >>same file for multiple writes?
> >
> >I've been bitten by this as well, but in reverse. Code that works great
> >on Unix is totally broken on Windows because you can't open the same
> >file for writing twice.
>
> Easy to change. In sqWin32FilePrims.c function sqFileOpen() modify:
>
>   h = CreateFileW(win32Path,
>         writeFlag ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_READ,
>         writeFlag ? FILE_SHARE_READ : (FILE_SHARE_READ | FILE_SHARE_WRITE),
>       "... etc ..."
>
> to read:
>
>   h = CreateFileW(win32Path,
>         writeFlag ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_READ,
>         FILE_SHARE_READ | FILE_SHARE_WRITE,
>       "... etc ..."
>
> So what's the equivalent change for Unix?

The Unix VM is opening the file with fopen(), which does not provide
such a control. However, you could use open() to open a file descriptor
with the O_EXCL and O_CREAT flags, then pass that file descriptor to
fdopen() to get the (FILE *) handle. I have not tested this, but I
think it would do what you are looking for here.

Dave

p.s. The Windows VM uses lower level HANDLE rather than the (FILE *)
used on other VMs. It would be interesting to know which approach
produces the best overall Squeak performance for applications that
do significant file I/O.