Forum > Unix
[SOLVED] FileOpen and fpOpen diff
claudioya:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---X:=FileOpen('/tmp/file.xxx',fmOpenRead); //or opened by external program F:=FileOpen('/tmp/file.xxx',fmOpenRead);WriteLn(F);// = -1, not opened.......
However:
fpOpen:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---X:=FileOpen('/tmp/file.xxx',fmOpenRead); //or opened by external program F:=FpOpen('/tmp/file.xxx',O_RDONLY);WriteLn(F);// > -1, opened and work fine.......
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?
Leledumbo:
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:
--- Quote from: claudioya on January 22, 2024, 11:18:13 pm ---- This is a bug?
- both functions ought work equal? Why?
--- End quote ---
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:
The crossplatform cognate of fileopen is https://www.freepascal.org/docs-html/rtl/sysutils/fileread.html
claudioya:
--- Quote from: Leledumbo 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.
--- End quote ---
--- Quote from: PascalDragon on January 23, 2024, 09:47:45 pm ---
--- Quote from: claudioya on January 22, 2024, 11:18:13 pm ---- This is a bug?
- both functions ought work equal? Why?
--- End quote ---
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.
--- End quote ---
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?
--- Quote from: marcov on January 23, 2024, 10:43:37 pm ---The crossplatform cognate of fileopen is https://www.freepascal.org/docs-html/rtl/sysutils/fileread.html
--- End quote ---
This problem is when file is opened, not when readed.
Navigation
[0] Message Index
[#] Next page