Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the file hidden?

Tags:

macos

cocoa

How can I determine whether a certain path points to a hidden file/folder?

NSString *file = @"/my/file/some.where";
BOOL fileIsHidden = // <-- what do I do here?

I know that hidden files are prefixed by a period. This is not the only criteria for a file to be hidden. I've read somewhere that there's a .hidden file that also configures what files are hidden.

Is there a Cocoa/Carbon way to find this out easily without rewriting all this logic and gathering information from various sources?

EDIT: the kLSItemInfoIsInvisible check seems to work for some files. It doesn't seem to hide:

/dev
/etc
/tmp
/var

All these are hidden by Finder by default.

like image 613
rein Avatar asked Dec 07 '22 06:12

rein


1 Answers

As the poster pointed out, it doesn't seem to work on /etc and /var and what not, so I modified the method.

It now takes a "isFile" boolean, YES means its a file, NO means a directory

BOOL isInvisible(NSString *str, BOOL isFile){
        CFURLRef inURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)str, kCFURLPOSIXPathStyle, isFile);
        LSItemInfoRecord itemInfo;
        LSCopyItemInfoForURL(inURL, kLSRequestAllFlags, &itemInfo);

        BOOL isInvisible = itemInfo.flags & kLSItemInfoIsInvisible;
        return (isInvisible != 0);
    }

    int main(){
           NSLog(@"%d",isInvisible(@"/etc",NO)); // => 1
           NSLog(@"%d",isInvisible(@"/Users",NO)); // => 0
           NSLog(@"%d",isInvisible(@"/mach_kernel",YES)); // => 1

    }

It seems to work on everything now!

like image 179
micmoo Avatar answered Jan 05 '23 04:01

micmoo