FileOpen returns -1 as handle lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- MISV · 2015-07-17 17:51 I am debugging my app in Lazarus running on OSX. The file I am trying to open using "FileOpen(APath, fmOpenRead)" returns -1 Using FileExists(APath) returns true The file is located in a subdirectory to the app being debugged The file  is AFAIK not open in anything else, but the file was created using an earlier version of the executable. ---------------------------------------------------------------------- Leledumbo · 2015-07-17 17:59 Quote from: MISV on July 17, 2015, 07:51:27 pmUsing FileExists(APath) returns true Permission? What does: Code: [Select]SysErrorMessage(GetLastOSError)say? I have no idea whether it's implemented on OS X, but just try it. ---------------------------------------------------------------------- MISV · 2015-07-17 21:24 Yep, it gives "Permission denied", but why? The user account is the same? I am a noob at OSX, maybe the file is locked by mistake. I will try restart OSX and see if problem resolves itself. ---------------------------------------------------------------------- MISV · 2015-07-17 22:57 Did not help restarting OSX ---------------------------------------------------------------------- Leledumbo · 2015-07-18 13:09 Quote from: MISV on July 17, 2015, 11:24:07 pmYep, it gives "Permission denied", but why? The user account is the same? I am a noob at OSX, maybe the file is locked by mistake. I will try restart OSX and see if problem resolves itself. I mean, what's the file permission? use `ls -l ` to find out. AFAIK *nix systems don't do file locking for reading. ---------------------------------------------------------------------- MISV · 2015-07-21 17:08 Whatever the explanation is/was - it works now. The ls -l command just showed I was the owner of i (i..e my osx login name) I willreturn with more deailed info if/when this problem remerges ---------------------------------------------------------------------- MISV · 2015-11-01 17:45 Okay - problem has remerged In first run my program has save a file to /users/%name%/Library/Application Support/%company%/%program name%/projects/test/myxml.xml When I try use fileopen on the file I file a -1 file handle Right clicking on the file and choosing "Get info" I see he following in "Sharing and permissions": everyone: No Access And that's it ---------------------------------------------------------------------- molly · 2015-11-01 18:34 Assuming you are unfamiliar with access rights, i would like to point you to this blog post. In case someone else can provide something better then please do so. (found this as well, and might be a better guideline as the first link focuses on sharing, and not so on access rights). Quoteeveryone: No Access Which in my books means: nobody is able to read, write or do other things with that file you are trying to access. ---------------------------------------------------------------------- MISV · 2015-11-02 00:54 Yes... But how can the program create such a file in the first place using the same user account? (Create / Write mode) a file noone has access to? And a compilation later then no longer access? But I will read your links, but my question is not that I don't understand why I can't access a file where nothing has permission, but more how such a file can occur in the first place. ---------------------------------------------------------------------- molly · 2015-11-02 01:26 @MISV: Although i don't have any experience with Apple's file/directory/drive access and user rights There could be a multitude of situations in which such thing might occur. For example, it might be that the Lazarus IDE has development rights and that an invoked application from IDE 'inherits' those rights, creates the file in question (still using development rights). But when you as user invoke the application (using your user rights) that the file doesn't allow a user like you to access the file (e.g. you are not part of development). It might even be that Mac application / access control software requires you to use a special group for applications that allows to create files for other users to access. Some one more experienced with apple might shed a better (more educated) light on that. Above is just simple examples, in practice things can get more complicated. Windows related rant. If you use Windows with NTFS using limited access rights, then you can run into the same situation. File is created by me, but all files in that directory are owned by admin. As a result i can't do with my own created file as i wish. Therefor under Windows i use a special substituted drive that i (as user) own and have all access right to, so that i (the user) am allowed to do as i want. Even admin and windows services aren't allowed in there (latter for preventing DEP and firewall to to kick in). You can't sanely develop under Windows otherwise, unless you are suckered into using Windows default user directories *barf* ---------------------------------------------------------------------- Bart · 2015-11-02 22:54 Quote from: molly on November 02, 2015, 02:26:47 amIf you use Windows with NTFS using limited access rights, then you can run into the same situation. File is created by me, but all files in that directory are owned by admin. As a result i can't do with my own created file as i wish. Therefor under Windows i use a special substituted drive that i (as user) own and have all access right to, so that i (the user) am allowed to do as i want. Even admin and windows services aren't allowed in there (latter for preventing DEP and firewall to to kick in). You can't sanely develop under Windows otherwise, unless you are suckered into using Windows default user directories *barf* Excuse me? I develop under Win7 with a limited account without any problems. (Well, I gave myself full access to the lazarus folder.) Bart ---------------------------------------------------------------------- skiy1337lazarus · 2015-11-04 16:25 Please don't ban Molly and Thaddy -- even though they are C.I.A. Live and let live. ---------------------------------------------------------------------- Deepaak · 2015-11-04 16:51 Quote from: MISV on November 01, 2015, 06:45:13 pmOkay - problem has remerged In first run my program has save a file to /users/%name%/Library/Application Support/%company%/%program name%/projects/test/myxml.xml When I try use fileopen on the file I file a -1 file handle Right clicking on the file and choosing "Get info" I see he following in "Sharing and permissions": everyone: No Access And that's it Your file is already open. At first case the file is created succesfully but is not closed. So when opening next time your are getting invalid_handle (-1). Check your code. It is nothing related to permission. Its just simple coding error. ---------------------------------------------------------------------- molly · 2015-11-04 19:04 Quote from: Bart on November 02, 2015, 11:54:18 pmExcuse me? Hoi Bart, Well of course. I am not going to argue that when you give yourself full access rights (and ownership) to the Lazarus folder (and subfolders) that you can have lots of fun during development. So, i take it that your own projects folders reside in the lazarus folder as well ? Good for you, i am happy that such setup is working for you. Quite honestly i don't see anything different from what i ranted about. Or it must be that you refer to my need to restrict certain services because they kick in on every stand-alone execution of a project being tested. Things that clients bug you about when deploying for instance. fwiw: it's nothing Lazarus/FPC related, rather Windows related. We can of course argue about what protection each individual developer has running on his/her development system, but i do believe that's a little out of scope for these forums, don't you agree ? I'm more than confident there are still plenty around that simply use an admin (or equivalent) account for their windows development. Who am i to judge ? Most time it will simply show in the end-result(s). kind regards, Molly ---------------------------------------------------------------------- Bart · 2015-11-04 21:42 Quote from: molly on November 04, 2015, 08:04:48 pmSo, i take it that your own projects folders reside in the lazarus folder as well ? Good for you, i am happy that such setup is working for you. No, they are under my user folder. Quote from: molly on November 04, 2015, 08:04:48 pmWe can of course argue about what protection each individual developer has running on his/her development system, but i do believe that's a little out of scope for these forums, don't you agree ? I agree. (I must admit, I do like ranting about M$ from time to time.) Bart ---------------------------------------------------------------------- MISV · 2015-11-08 14:36 Quote from: Deepaak on November 04, 2015, 05:51:11 pmQuote from: MISV on November 01, 2015, 06:45:13 pmOkay - problem has remerged In first run my program has save a file to /users/%name%/Library/Application Support/%company%/%program name%/projects/test/myxml.xml When I try use fileopen on the file I file a -1 file handle Right clicking on the file and choosing "Get info" I see he following in "Sharing and permissions": everyone: No Access And that's it Your file is already open. At first case the file is created succesfully but is not closed. So when opening next time your are getting invalid_handle (-1). Check your code. It is nothing related to permission. Its just simple coding error. I doubt it, but i will check it. Reason is that I do close the files. The problem survives a reboot. And changing the permissions from no access to read/write solves the issue. ---------------------------------------------------------------------- MISV · 2015-11-11 13:44 More info - think I am close at pinpointing the error: 1) I am using (fmCreate or fmOpenWrite) to create file 2) For creating ifiles I use 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 TmpFH: THandle;TmpFH := FileCreate(FileName, fmCreate or fmOpenReadWrite) ---------------------------------------------------------------------- molly · 2015-11-11 15:43 Isn't fmCreate for filestreams only ? If not mistaken then you can't do what you just showed (at least it doesn't work for me). afaik you would need FileOpen() to open an existing file (with right read/write mode as you showed), and when use FileCreate() when you want to create a new file. You can't do that in one go, unless always overwriting existing file. FileOpen remark: QuoteRemark: Remark that you cannot open a file if it doesn't exist yet, i.e. it will not be created for you. If you want tp create a new file, or overwrite an old one, use the FileCreate function. Edit: What does the following small example do for you ? (please be explicit, paste output here) 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 program FileCreation; {$MODE OBJFPC}{$H+} uses  SysUtils; Const  Filename = 'testfile.dat'; Var  TestFile : THandle;  Data1    : Array[0..9] of byte = (10,9,8,7,6,5,4,3,2,1);  Data2    : Array[0..9] of byte;  i      : integer;begin  WriteLn('Test file creation');   if not FileExists(Filename) then  begin    WriteLn('file doesn''t exist');  WriteLn('creating file');    TestFile := FileCreate(Filename);    If TestFile = THandle(-1) then    begin      WriteLn('Something went wrong creating file ', filename);      WriteLn('Panic exit');      Exit;    end;     // new file means empty -> put some data into it    WriteLn('stuff some data into file');    FileWrite(TestFile, Data1, Length(Data1));  end  else  begin    WriteLn('file already exist');  WriteLn('opening existing file');    TestFile := FileOpen(Filename, fmOpenreadWrite);    If TestFile = THandle(-1) then    begin      WriteLn('Something went wrong opening file ', filename);      WriteLn('Panic exit');      Exit;    end;  end;     // read some data from file    WriteLn('Reading data from file');  FileSeek(TestFile, 0, fsFromBeginning);  FileRead(TestFile, Data2, length(data2));   // print data  Write('Read data reads:');  for i := Low(Data2) to High(Data2) do Write('  ', Data2[i]);  WriteLn;   FileClose(TestFile);   WriteLn('Done!');end.