Recent

Author Topic: Cann't remove faArchive flag from file on MacOS  (Read 1020 times)

Theo11

  • New member
  • *
  • Posts: 9
Cann't remove faArchive flag from file on MacOS
« on: April 11, 2025, 03:12:28 pm »
Hi,

Files are on a SD card (MS-DOS) formatted and are using the Archive (Secure flag).
I need to rename files, but I first need to remove the faArchive flag.
This my code:
Code: Pascal  [Select][+][-]
  1. aFileAttr := FileGetAttrUTF8(aOldName);
  2. if (aFileAttr and  faArchive ) >  0 then
  3. begin
  4.      aFileAttr := aFileAttr and (not faArchive);
  5.      FileSetAttrUTF8(aOldName, aFileAttr);
  6. end;
  7. if (RenameFileUTF8(aOldName, aNewName) = false) then
  8.                 res := QuestionDlg ('Abort purging tracks?', 'Something went wrong renaming track ' + ExtractFileNameOnly(aOldName),mtError, [mbOk, mbNo], 0)
  9. else
  10.     // file is renamed
  11.     FileSetAttrUTF8(aNewName, aFileAttr and faArchive);  [/size]  
     
Also coping the content to a directory on the Mac gives the same result.

How can I remove the faArchive flag?

Thanks,
Theo.

Theo11

  • New member
  • *
  • Posts: 9
Re: Cann't remove faArchive flag from file on MacOS
« Reply #1 on: April 11, 2025, 08:01:09 pm »
Hi,

I have written a small test program to analyse the problem:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, LazFileUtils;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. Var
  36.   F : Longint;
  37.   FStr : string;
  38. begin
  39.  FStr := 'Permissions: ';
  40.  Label1.Caption := 'File: ';
  41.  Label2.Caption :=  FStr;
  42.  with TOpenDialog.Create(Self) do
  43.  begin
  44.   if Execute then
  45.        begin
  46.           Filter := '*.*';
  47.           Label1.Caption := 'File: ' + FileName;
  48.           F := FileGetAttrUTF8(FileName);
  49.           If F<>-1 then
  50.           begin
  51.               If (F and faReadOnly)<>0 then
  52.                 FStr := FStr + '-ReadOnly-';
  53.               If (F and faHidden)<>0 then
  54.                  FStr := FStr + '-Hidden-';
  55.               If (F and faSysFile)<>0 then
  56.                 FStr := FStr + '-System File-';
  57.               If (F and faVolumeID)<>0 then
  58.                 FStr := FStr + '-disk label-';
  59.               If (F and faArchive)<>0 then
  60.                 FStr := FStr + '-archive-';
  61.               If (F and faDirectory)<>0 then
  62.                 FStr := FStr + '-directory-';
  63.               Label2.Caption := FStr;
  64.           end
  65.           else
  66.                Writeln ('Error reading attributes of ', Name);
  67.        end;
  68.   Free;
  69.  end;
  70. end;
  71. end.        
     

The result is for every file the same: -Archive, even Archive (Secure flag) is not set!
The FileGetAttrUTF8 procedure does not seem to work on MacOS!

How to get and set the file attribute on MacOS?

Theo.

Theo11

  • New member
  • *
  • Posts: 9
Re: Cann't remove faArchive flag from file on MacOS
« Reply #2 on: April 12, 2025, 01:27:26 pm »
Hi,

I have added the following to the code:
Code: Pascal  [Select][+][-]
  1.      end;
  2.   f := FileSetAttrUTF8(FileName, F and (not faArchive) and (not faReadOnly));
  3.   Free;                                                                      

FileSetAttrUTF8 is working on a cross compiled version on Win64, but still not on MacOS.

Did some extra steps on MacOS, added program to allow make changes to the file system and installed Xcode 15.4 because I am on macOS Sonoma 14.7.2.

But still no luck on MacOS  :o

Bart

  • Hero Member
  • *****
  • Posts: 5563
    • Bart en Mariska's Webstek
Re: Cann't remove faArchive flag from file on MacOS
« Reply #3 on: April 12, 2025, 02:19:18 pm »
Does faArchive have a meaning on Unix?
AFAIK, it's a Windows thingy.

Bart

TRon

  • Hero Member
  • *****
  • Posts: 4351
Re: Cann't remove faArchive flag from file on MacOS
« Reply #4 on: April 12, 2025, 04:32:29 pm »
Does faArchive have a meaning on Unix?
AFAIK, it's a Windows thingy.
It is actually a filesystem thingy (which might end up as an OS thingy) :)
Today is tomorrow's yesterday.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5959
  • Compiler Developer
Re: Cann't remove faArchive flag from file on MacOS
« Reply #5 on: April 12, 2025, 04:35:20 pm »
How can I remove the faArchive flag?

You can't. It's hardcoded in the RTL, so best simply ignore it. It's probably there to improve compatibility with code coming from Delphi and thus the Windows world.

Theo11

  • New member
  • *
  • Posts: 9
Re: Cann't remove faArchive flag from file on MacOS
« Reply #6 on: April 13, 2025, 11:43:25 am »
Hi

I cannot ignore this flag on MacOS.
If it's set the files are readonly, see attachment.

Any other ideas to tackle this?

Theo.

Theo11

  • New member
  • *
  • Posts: 9
Re: Cann't remove faArchive flag from file on MacOS
« Reply #7 on: April 13, 2025, 01:07:15 pm »
Hi,

Searched further on the internet.
It seems to be the Locked file attribute

In the terminal you can remove the lock:
- chflags nouchg file

Any API in free pascal or LCL ....?

If no API, then can I execute this from within my program:
- find . -flags uchg -exec chflags nouchg {} \;

Theo.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5959
  • Compiler Developer
Re: Cann't remove faArchive flag from file on MacOS
« Reply #8 on: April 16, 2025, 11:07:04 pm »
I cannot ignore this flag on MacOS.

Yes, you can, because this flag is added to the flags received by FileGetAttr itself on the fly, it's not extracted from the file system, so whatever flag you're seeing in that property dialog is not what faArchive represents.

Also FileSetAttr is no-op on Unix systems and always returns -1 (aka failure).

 

TinyPortal © 2005-2018