Recent

Author Topic: GetappConfigDir without a check on the existence of .config directory  (Read 509 times)

han

  • Jr. Member
  • **
  • Posts: 63
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?

Serge Smirnov

  • Newbie
  • Posts: 2
Re: GetappConfigDir without a check on the existence of .config directory
« Reply #1 on: October 23, 2019, 11:23:28 am »
The best way is to use ForceDirectories(user_path)

han

  • Jr. Member
  • **
  • Posts: 63
Re: GetappConfigDir without a check on the existence of .config directory
« Reply #2 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.

trev

  • Full Member
  • ***
  • Posts: 220
  • Former Delphi 7 and Delphi 10.2 User
Re: GetappConfigDir without a check on the existence of .config directory
« Reply #3 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
o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

han

  • Jr. Member
  • **
  • Posts: 63
Re: GetappConfigDir without a check on the existence of .config directory
« Reply #4 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.
« Last Edit: October 24, 2019, 11:09:22 pm by han »

trev

  • Full Member
  • ***
  • Posts: 220
  • Former Delphi 7 and Delphi 10.2 User
Re: GetappConfigDir without a check on the existence of .config directory
« Reply #5 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!

o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)