Recent

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

han

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

  • New member
  • *
  • Posts: 6
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: 65
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1057
  • Former Delphi 1-7, 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 r63871, FPC v3.3.1 r47164, macOS 10.14.6, Xcode 11.3.1
o Lazarus v2.1.0 r64160, FPC v3.3.1 Nov 27 21:16:31, macOS 11.0.1 (aarch64), Xcode 12.2
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (PD VM)

han

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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1057
  • Former Delphi 1-7, 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 r63871, FPC v3.3.1 r47164, macOS 10.14.6, Xcode 11.3.1
o Lazarus v2.1.0 r64160, FPC v3.3.1 Nov 27 21:16:31, macOS 11.0.1 (aarch64), Xcode 12.2
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (PD VM)

 

TinyPortal © 2005-2018