Lazarus

Installation => macOS / Mac OS X => Topic started by: han on October 23, 2019, 10:55:13 am

Title: GetappConfigDir without a check on the existence of .config directory
Post by: han on October 23, 2019, 10:55:13 am
One of my program users didn't have a  ~/.config directory. So my code failed to create ~/.config/program since it tried to make a directory and sub directory in one step.

In Linux and MacOS you have to make the path in two steps. So ~/.config and ~/.config/program

My original code
Code: [Select]
  user_path:=GetAppConfigDir(false);{get user path for app config}
  if load_settings(user_path+'program.cfg')=false then
  begin
    if DirectoryExists(user_path)=false then createdir(user_path);
    ....
  end;

My improved code:


My original code
Code: [Select]
  user_path:=GetAppConfigDir(false);{get user path for app config}
  if load_settings(user_path+'program.cfg')=false then
  begin
    {$ifdef mswindows}
    {$else} {unix}
    s:=expandfilename('~/.config');
    if DirectoryExists(s)=false then createdir(s); {required since in Linux and MacOS you can't create ./config and .config/astap in one step}
    {$endif}
             
    if DirectoryExists(user_path)=false then createdir(user_path);
   ....
  end;

Is there a better way?
Title: Re: GetappConfigDir without a check on the existence of .config directory
Post by: Serge Smirnov on October 23, 2019, 11:23:28 am
The best way is to use ForceDirectories(user_path)
Title: Re: GetappConfigDir without a check on the existence of .config directory
Post by: han on October 23, 2019, 02:02:08 pm
Thanks, that is just where I was looking for :)

I got an other tip for GetAppConfigDirUTF8(false,true) where the true calls ForceDirectoriesUtf8, but that requires the unit lazfileutils making the executable a little larger.
Title: Re: GetappConfigDir without a check on the existence of .config directory
Post by: trev on October 24, 2019, 06:15:14 am
I'm not surprised that your application didn't find a .config directory because that is not the proper place to store configuration data on macOS.

The correct place is (from the wiki):
https://wiki.lazarus.freepascal.org/Multiplatform_Programming_Guide#Proper_macOS_file_locations
Title: Re: GetappConfigDir without a check on the existence of .config directory
Post by: han on October 24, 2019, 05:37:09 pm
Now it's gets interesting.

When I look in my virtual machine running MacOS, I see configuration directories from Lazarus, FPC and Lhelp.

/users/username/.config/lazarus
/users/username/.config/fpc
/users/username/.config/lhelp

Secondly .config is reported by GetAppConfigDir

If it is not the correct configuration directory, why then the above?  Myself i uses only MacOS to compile only, so I have very limited experience with it.

Han


Later,

I have read it again:
Using .config files in the User directory are a violation of the programming guidelines. See below for one solution.

but it is confusing. Looks like a Linux convention which is practical but not according the guideline.
Title: Re: GetappConfigDir without a check on the existence of .config directory
Post by: trev on November 01, 2019, 12:24:12 am
> Secondly .config is reported by GetAppConfigDir
>
> If it is not the correct configuration directory, why then the above?  Myself i uses only MacOS to
> compile only, so I have very limited experience with it.

It is a bug in GetAppConfigDir  that does not seem like it is ever going to be fixed. See BugTracker: https://bugs.freepascal.org/view.php?id=20706 where it was logged as a bug in 2011!

TinyPortal © 2005-2018