Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac Sandbox: testing whether a file is accessible

Does anybody know whether there's a way of finding out whether a particular file system location is accessible under the sandbox?

I want to test whether a particular file is accessible under the normal Powerbox rules; that is has already been added to the power box using the open/ save dialog, etc.

Can I do this before triggering a sandbox exception?

Can I catch a sandbox exception?

Best regards,

Frank

like image 697
Frank R. Avatar asked May 09 '12 09:05

Frank R.


People also ask

Does Mac Have a sandbox?

The App Sandbox is Apple's access control technology that application developers must adopt to distribute their apps through the Mac App Store.

What is kernel sandbox?

Android sandbox This kernel ensures security between apps and the system at the process level. Moreover, because the sandbox is in the kernel, its security model extends to native code and OS applications, and all software above the kernel, including OS libraries, application runtime and application framework.


2 Answers

A few things. Always use fileSystemRepresentation when you need a path string. Also, R_OK is adequate if you just want to know if there is a hole in the sandbox for the specified path.

-(BOOL)isAccessibleFromSandbox:(NSString*)path
{
    return( access( path.fileSystemRepresentation, R_OK) == 0 );
}
like image 199
December Avatar answered Oct 01 '22 00:10

December


You can use the OS access() system call for a quick and simple test, from man access:

#include <unistd.h>

int access(const char *path, int amode);

The access() function checks the accessibility of the file named by path for the access permissions indicated by amode. The value of amode is the bitwise inclusive OR of the access permissions to be checked (R_OK for read permission, W_OK for write permission and X_OK for execute/search permission) or the existence test, F_OK. All components of the pathname path are checked for access permissions (including F_OK).

If path cannot be found or if any of the desired access modes would not be granted, then a -1 value is returned and the global integer variable errno is set to indicate the error. Otherwise, a 0 value is returned.

You could pretty this up for Objective-C using something like:

typedef enum
{
   ReadAccess = R_OK,
   WriteAccess = W_OK,
   ExecuteAccess = X_OK,
   PathExists = F_OK
} AccessKind;


BOOL isPathAccessible(NSString *path, AccessKind mode)
{
   return access([path UTF8String], mode) == 0;
}
like image 22
CRD Avatar answered Oct 01 '22 01:10

CRD