Recent

Author Topic: [SOLVED] FileOpen and fpOpen diff  (Read 3542 times)

claudioya

  • New member
  • *
  • Posts: 7
[SOLVED] FileOpen and fpOpen diff
« on: January 22, 2024, 11:18:13 pm »
Hello,

I found a diff using FileOpen(), standard and cross-platform function, and fpRead(), the only Unix function for same porpouse. While FileOpen() not read a file opened by same or other application, fpRead() yes.

Example:

FileOpen:
Code: Pascal  [Select][+][-]
  1. X:=FileOpen('/tmp/file.xxx',fmOpenRead);   //or opened by external program
  2.  
  3. F:=FileOpen('/tmp/file.xxx',fmOpenRead);
  4. WriteLn(F);// = -1, not opened
  5. .......
  6.  

However:

fpOpen:
Code: Pascal  [Select][+][-]
  1. X:=FileOpen('/tmp/file.xxx',fmOpenRead);   //or opened by external program
  2.  
  3. F:=FpOpen('/tmp/file.xxx',O_RDONLY);
  4. WriteLn(F);// > -1, opened and work fine
  5. .......
  6.  

Note: FileCopy() use FileOpen() function internally (on Linux), in case that the file to be copied is opened, function not work.

- This is a bug?
- both functions ought work equal? Why?
« Last Edit: January 27, 2024, 11:10:19 pm by claudioya »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8768
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FileOpen and fpOpen diff
« Reply #1 on: January 23, 2024, 06:02:44 am »
FileOpen internally uses fpOpen, as you can read here yourself. As I don't know the state of that particular file, it may fail on DoFileLocking instead, where it tries to lock the file for exclusive access.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5649
  • Compiler Developer
Re: FileOpen and fpOpen diff
« Reply #2 on: January 23, 2024, 09:47:45 pm »
- This is a bug?
- both functions ought work equal? Why?


The POSIX open function (which is provided by FpOpen) does not deal with locking, however the FileOpen API requires handling of locking. However on *nix locking is advisory, not mandatory like it is on Windows, thus you can't open a file that's been opened with FileOpen with another FileOpen, but with FpOpen directly this locking mechanism isn't involved. If you want to have shared access to files using FileOpen you need to pass fmShareDenyNone as flag to the FileOpen function.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11732
  • FPC developer.
Re: FileOpen and fpOpen diff
« Reply #3 on: January 23, 2024, 10:43:37 pm »

claudioya

  • New member
  • *
  • Posts: 7
Re: FileOpen and fpOpen diff
« Reply #4 on: January 24, 2024, 12:28:57 am »
FileOpen internally uses fpOpen, as you can read here yourself. As I don't know the state of that particular file, it may fail on DoFileLocking instead, where it tries to lock the file for exclusive access.

- This is a bug?
- both functions ought work equal? Why?


The POSIX open function (which is provided by FpOpen) does not deal with locking, however the FileOpen API requires handling of locking. However on *nix locking is advisory, not mandatory like it is on Windows, thus you can't open a file that's been opened with FileOpen with another FileOpen, but with FpOpen directly this locking mechanism isn't involved. If you want to have shared access to files using FileOpen you need to pass fmShareDenyNone as flag to the FileOpen function.

I understand, using "fmShareDenyNone" fails using into second function call if not used in first. But, if use this in first call work fine. This problem is when external application lock this file, CopyFile() will be useless. Other applications don't have this problem.

In mi opinion, FileOpen should not lock file by default, allowing copy or read file opened by other process. You consider report this?



The crossplatform cognate of fileopen is https://www.freepascal.org/docs-html/rtl/sysutils/fileread.html

This problem is when file is opened, not when readed.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5649
  • Compiler Developer
Re: FileOpen and fpOpen diff
« Reply #5 on: January 27, 2024, 05:51:38 pm »
I understand, using "fmShareDenyNone" fails using into second function call if not used in first. But, if use this in first call work fine. This problem is when external application lock this file, CopyFile() will be useless. Other applications don't have this problem.

It doesn't matter what other applications do. FileOpen is an Object Pascal function that needs to behave according to the rules laid down for that function.

In mi opinion, FileOpen should not lock file by default, allowing copy or read file opened by other process. You consider report this?

There is no need to report this, because this is by design. The default behavior without any explicit ttShareDenyNone is to open the file exclusively. That's how the function supposed to behave no matter the platform.

claudioya

  • New member
  • *
  • Posts: 7
Re: [SOLVED] FileOpen and fpOpen diff
« Reply #6 on: January 27, 2024, 11:12:12 pm »
I understand, using "fmShareDenyNone" fails using into second function call if not used in first. But, if use this in first call work fine. This problem is when external application lock this file, CopyFile() will be useless. Other applications don't have this problem.

It doesn't matter what other applications do. FileOpen is an Object Pascal function that needs to behave according to the rules laid down for that function.

In mi opinion, FileOpen should not lock file by default, allowing copy or read file opened by other process. You consider report this?

There is no need to report this, because this is by design. The default behavior without any explicit ttShareDenyNone is to open the file exclusively. That's how the function supposed to behave no matter the platform.

i understand this. Thank you.

 

TinyPortal © 2005-2018