windows file locking

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

windows file locking

Stefan Aeschbacher
Hi

I need to open a file and keep it open for writing (a log file). Another process has
to read from this file. On windows the second process (e.g. tail -f) can not open the file.
How can I open a file without this locking?

regards

Stefan

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: windows file locking

Krasimir Angelov-2
Hi Stefan,

Haskell I/O systems implements single writer-multiple readers file locking. See

http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#8

Cheers,
  Krasimir

On 10/11/06, Stefan Aeschbacher <[hidden email]> wrote:

> Hi
>
> I need to open a file and keep it open for writing (a log file). Another
> process has
> to read from this file. On windows the second process (e.g. tail -f) can not
> open the file.
> How can I open a file without this locking?
>
> regards
>
> Stefan
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: windows file locking

Esa Ilari Vuokko
In reply to this post by Stefan Aeschbacher
On 10/11/06, Stefan Aeschbacher <[hidden email]> wrote:
> Hi
>
> I need to open a file and keep it open for writing (a log file). Another
> process has
> to read from this file. On windows the second process (e.g. tail -f) can not
> open the file.
> How can I open a file without this locking?

Using base-package's IO-system, that is not possible as far as I know.
 The flag you need to pass CreateFile (api call which is used to open
practically all files) is FILE_SHARE_READ.

You can find these in Win32-package, but files opened via raw api
bindings don't work well together with base-packages IO-system (ie,
you need to read and write using rawish api calls.)

Best regards,
Esa
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: windows file locking

Bulat Ziganshin-2
Hello Esa,

Wednesday, October 11, 2006, 2:13:49 PM, you wrote:

> Using base-package's IO-system, that is not possible as far as I know.
>  The flag you need to pass CreateFile (api call which is used to open
> practically all files) is FILE_SHARE_READ.

> You can find these in Win32-package, but files opened via raw api
> bindings don't work well together with base-packages IO-system (ie,
> you need to read and write using rawish api calls.)

as usual, Streams library may be used to solve this problem, although
this will need some additional programming. please write me if
win32read/win32write API is inappropriate dor your needs

--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: windows file locking

Stefan Aeschbacher
Hi

So far the windows API seems to work for me. Currently I'm still struggling to not
write garbage to the file but the shared access works now. Does anyone have an
example how to use it (e.g. the implementation of hPutStr on windows or something,
i did not find it in the ghc source code)?

I tried to use Streams before but without the windows API.

regards

Stefan


2006/10/11, Bulat Ziganshin <[hidden email]>:
Hello Esa,

Wednesday, October 11, 2006, 2:13:49 PM, you wrote:

> Using base-package's IO-system, that is not possible as far as I know.
>  The flag you need to pass CreateFile (api call which is used to open
> practically all files) is FILE_SHARE_READ.

> You can find these in Win32-package, but files opened via raw api
> bindings don't work well together with base-packages IO-system (ie,
> you need to read and write using rawish api calls.)

as usual, Streams library may be used to solve this problem, although
this will need some additional programming. please write me if
win32read/win32write API is inappropriate dor your needs

--
Best regards,
Bulat                            mailto:[hidden email]



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[4]: windows file locking

Bulat Ziganshin-2
In reply to this post by Bulat Ziganshin-2
Hello Stefan,

Wednesday, October 11, 2006, 4:53:19 PM, you wrote:

> So far the windows API seems to work for me. Currently I'm still struggling to not
> write garbage to the file but the shared access works now. Does anyone have an
> example how to use it (e.g. the implementation of hPutStr on windows or something,
> i did not find it in the ghc source code)?

> I tried to use Streams before but without the windows API.

sorry, i'm a bit to fool you. there is a way to apply Handle API to
HANDLE. you should just

1) open file using win32 API and get HANDLE
2) convert this HANDLE to FD using _open_osfhandle from win32 api
3) open Handle over FD using fdToHandle function from GHC.Handle module

alternatively, you can use Streams with the same mechanism:

1) open file using win32 API and get HANDLE
2) convert this HANDLE to FD using _open_osfhandle from win32 api

  // it's in C language:
  fd = _open_osfhandle (someHANDLE, _O_TEXT | _O_WRONLY);

3) use bufferBlockStream to make full-featured Stream from this FD:

  do stream <- bufferBlockStream fd




it is from MS docs:

SUMMARY

There are multiple types of file handles that can be opened using the Win32
API and the C Run-time:

   Returned Type  File Creation API      API Set
   ---------------------------------------------
   HANDLE         CreateFile()           Win32
   HFILE          OpenFile()/_lcreat()   Win32
   int            _creat()/_open()       C Run-time
   FILE *         fopen()                C Run-time

In general, these file I/O "families" are incompatible with each other. On

some implementations of the Win32 application programming interfaces
(APIs), the OpenFile()/_lcreat() family of file I/O APIs are implemented as
"wrappers" around the CreateFile() family of file I/O APIs, meaning that
OpenFile(), _lcreat(), and _lopen() end up calling CreateFile(), returning
the handle returned by CreateFile(), and do not maintain any state
information about the file themselves. However, this is an implementation
detail only and is NOT a design feature.

NOTE: You cannot count on this being true on other implementations of the
Win32 APIs. Win32 file I/O APIs may be written using different methods on
other platforms, so reliance on this implementation detail may cause your
application to fail.

The rule to follow is to use one family of file I/O APIs and stick with
them--do not open a file with _lopen() and read from it with ReadFile(),
for example. This kind of incorrect use of the file I/O APIs can easily be
caught by the compiler, because the file types (HFILE and HANDLE

respectively) are incompatible with each other and the compiler will warn
you (at warning level /w3 or higher) when you have incorrectly passed one
type of file handle to a file I/O API that is expecting another, such as
passing an HFILE type to ReadFile(HANDLE, ...) in the above example.


MORE INFORMATION

Compatibility
-------------

The OpenFile() family of file I/O functions is provided only for
compatibility with earlier versions of Windows. New Win32-based
applications should use the CreateFile() family of file I/O APIs, which
provide added functionality that the earlier file I/O APIs do not provide.

Each of the two families of C Run-time file I/O APIs are incompatible with
any of the other file I/O families. It is incorrect to open a file handle
with one of the C Run-time file I/O APIs and operate on that file handle

with any other family of file I/O APIs, nor can a C Run-time file I/O
family operate on file handles opened by any other file I/O family.

_get_osfhandle()
----------------

For the C Run-time unbuffered I/O family of APIs [_open(), and so forth],
it is possible to extract the operating system handle that is associated
with that C run-time handle via the _get_osfhandle() C Run-time API. The
operating system handle is the handle stored in a C Run-time internal
structure associated with that C Run-time file handle. This operating

system handle is the handle that is returned from an operating system call
made by the C Run-time to open a file [CreateFile() in this case] when you
call one of the C Run-time unbuffered I/O APIs [_open(), _creat(),
_sopen(), and so forth].

The _get_osfhandle() C Run-time call is provided for informational purposes
only. Problems may occur if you read or write to the file using the
operating system handle returned from _get_osfhandle(); for these reasons
we recommend that you do not use the returned handle to read or write to

the file.

_open_osfhandle()
-----------------

It is also possible to construct a C Run-time unbuffered file I/O handle
from an operating system handle [a CreateFile() handle] with the
_open_osfhandle() C Run-time API. In this case, the C Run-time uses the
existing operating system handle that you pass in rather than opening the
file itself. It is possible to use the original operating system handle to
read or write to the file, but it is very important that you use only the

original handle or the returned C Run-time handle to access the file, but
not both, because the C Run-time maintains state information that will not
be updated if you use the operating system handle to read or write to the
file.




--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe