@@ -361,16 +361,38 @@ def __create(self, path):
361
361
362
362
self .logger .info ("TCC.db file was expected at '{}' but was not found. Creating new TCC.db file..." .format (path ))
363
363
364
- # Make sure our directory tree exists.
365
- local_created = False
366
- if not os .path .exists (os .path .dirname (path )):
367
- os .makedirs (os .path .dirname (path ), int ('700' , 8 ))
368
- # If the user isn't root and we're adjusting their local database,
369
- # we'll fix some permissions.
364
+ # Make sure our directory tree exists properly.
365
+ chown_database = False
366
+ # Check for existence of
367
+ # .../Library/Application Support/com.apple.TCC/TCC.db
368
+ if not os .path .isfile (path ):
369
+ database_dir = os .path .dirname (path )
370
+ app_support_dir = os .path .dirname (database_dir )
371
+ # Let's only bother with user permissions if this isn't a folder
372
+ # owned by root.
370
373
if self .user != 'root' and path == self .local_path :
371
- local_created = True
372
- database_dir = os .path .dirname (self .local_path )
373
- os .chown (database_dir , uid , gid )
374
+ # We'll have to modify the permissions on 'TCC.db' later.
375
+ chown_database = True
376
+ # Check existence of
377
+ # .../Library/Application Support/com.apple.TCC/
378
+ if not os .path .isdir (database_dir ):
379
+ # Check existence of
380
+ # .../Library/Application Support/
381
+ if not os .path .isdir (app_support_dir ):
382
+ # Check that the 'Library' folder exists.
383
+ if not os .path .isdir (os .path .dirname (app_support_dir )):
384
+ # There's no 'Library' folder? Something isn't right.
385
+ raise RuntimeError ("No 'Library' directory found for database: {}" .format (path ))
386
+ # Create and chown 'Application Support' folder.
387
+ os .mkdir (app_support_dir , int ('700' , 8 ))
388
+ os .chown (app_support_dir , uid , gid )
389
+ # Create and chown the 'com.apple.TCC' folder.
390
+ os .mkdir (database_dir , int ('700' , 8 ))
391
+ os .chown (database_dir , uid , gid )
392
+ else :
393
+ # We're not dealing with a specific user's directory, so just
394
+ # make the parent directories as needed and ignore permissions.
395
+ os .makedirs (os .path .dirname (path ), int ('700' , 8 ))
374
396
375
397
# Form an SQL connection with the file.
376
398
connection = sqlite3 .connect (path )
@@ -448,7 +470,7 @@ def __create(self, path):
448
470
self .logger .info ("TCC.db file created successfully." )
449
471
450
472
# The local database was created, so make sure permissions are set.
451
- if local_created :
473
+ if chown_database :
452
474
os .chown (self .local_path , uid , gid )
453
475
454
476
def __enter__ (self ):
0 commit comments