[SOLVED] FileOpen and fpOpen diff lazarus / 66-Unix ---------------------------------------------------------------------- claudioya · 2024-01-22 22:18 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2024-01-23 05:02 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 · 2024-01-23 20:47 Quote from: claudioya on January 22, 2024, 11:18:13 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 · 2024-01-23 21:43 The crossplatform cognate of fileopen is https://www.freepascal.org/docs-html/rtl/sysutils/fileread.html ---------------------------------------------------------------------- claudioya · 2024-01-23 23:28 Quote from: Leledumbo on January 23, 2024, 06:02:44 amFileOpen 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. Quote from: PascalDragon on January 23, 2024, 09:47:45 pmQuote from: claudioya on January 22, 2024, 11:18:13 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. 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 pmThe 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 · 2024-01-27 16:51 Quote from: claudioya on January 24, 2024, 12:28:57 amI 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. Quote from: claudioya on January 24, 2024, 12:28:57 amIn 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 · 2024-01-27 22:12 Quote from: PascalDragon on January 27, 2024, 05:51:38 pmQuote from: claudioya on January 24, 2024, 12:28:57 amI 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. Quote from: claudioya on January 24, 2024, 12:28:57 amIn 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.