Recent

Author Topic: TFileStream can't open a file from network drive in second app  (Read 1667 times)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
TFileStream can't open a file from network drive in second app
« on: December 15, 2020, 11:52:07 am »
TFileStream can't open a file from a network drive (SMB or AFP), if I already opened this file in another instance of the same app (or another Lazarus app) using TFileStream:

Code: Pascal  [Select][+][-]
  1. Result:=TFileStream.Create(UTF8Encode(FN), fmOpenRead or fmShareDenyWrite);

Code: Pascal  [Select][+][-]
  1. /Volumes/igor/test.mov

However I can open this file in another app on Mac (VLC player).

I correctly set access parameters, but it seems that TFileStream locks a file after openning and another app can't open it with TFileStream. This problem doesn't occur with a local file.

I attach a simple test app.

UPDATE. I tried to replace TFileStream with functions from FpOpen and these new functions work fine to read the same file from SMB or AFP network from two apps.

Lazarus 2.0.10 / FPC 3.2.0. macOS Big Sur / Catalina
« Last Edit: December 15, 2020, 06:50:33 pm by Igor Kokarev »

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TFileStream can't open a file from network drive in second app
« Reply #1 on: December 15, 2020, 01:47:11 pm »
What happens if you use fmShareDenyNone?

Code: Pascal  [Select][+][-]
  1. Result:=TFileStream.Create(UTF8Encode(FN), fmOpenRead or fmShareDenyNone);

(Of course in both Apps !!)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: TFileStream can't open a file from network drive in second app
« Reply #2 on: December 15, 2020, 02:12:23 pm »
Yes, we tried with fmShareDenyNone - the same result.

P.S. The app is the same (I run it twice in two Terminal app). Also I tried with two different apps - as soon I open a file with TFileStream from network drive, I cannot open it in a new TFileStream in another app. I observe this problem on 3 Macs (Catalina, Big Sur) with differente networks (WD Live with AFP protocol) and Windows 10 Pro network (Samba).
« Last Edit: December 15, 2020, 02:28:13 pm by Igor Kokarev »

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TFileStream can't open a file from network drive in second app
« Reply #3 on: December 15, 2020, 02:39:30 pm »
For me, your example works fine on Windows/Lazarus trunk.

On a Linux variant you could use smbstatus to see how the files are opened.
On Windows you could look in the computer management in Open Files to see how it's opened.

(I don't have an Apple so can't test it on that)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: TFileStream can't open a file from network drive in second app
« Reply #4 on: December 15, 2020, 03:03:05 pm »
This problem doesn't occur on Windows. I checked in macOS.

The problem can be reproduced with a very simple Lazarus code on any type of network - SMB or AFP.

But this problem doesn't occur with other apps (file managers, VLC, etc).

I don't understand why it happens.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: TFileStream can't open a file from network drive in second app
« Reply #5 on: December 15, 2020, 06:51:03 pm »
I tried to replace TFileStream with functions from FpOpen and these new functions work fine to read the same file from SMB or AFP network from two apps.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: TFileStream can't open a file from network drive in second app
« Reply #6 on: December 16, 2020, 09:25:12 am »
TFileStream.Create uses SysUtils.FileOpen which in turn uses FpOpen (see $fpcdir/rtl/unix/sysutils.pp at around line 419). What it also does is to lock the file (Unix based systems use advisory locking instead of mandatory locking as Windows does).
Maybe you can play around with that code a bit to find out what goes wrong. For that you could copy the code of FileOpen, FileOpenNoLocking and DoFileLocking (as well as any private constants, etc.) to your own program and try to use them (TFileStream.Create simply passes its arguments to FileOpen).

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: TFileStream can't open a file from network drive in second app
« Reply #7 on: December 16, 2020, 12:01:43 pm »
We didn't use fpflock()

Probably fpflock() doesn't work correctly for a file from a network drive in macOS?

We'll try to modify our code to check if use of fpflock() cause the same problem as TFileStream

 

TinyPortal © 2005-2018