Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Set backup exclusion attribute on cache directory - #5578

Closed
dapetcu21 wants to merge 1 commit into
mapbox:masterfrom
dapetcu21:patch-2
Closed

dapetcu21 wants to merge 1 commit into
mapbox:masterfrom
dapetcu21:patch-2

Conversation

@dapetcu21

Copy link
Copy Markdown

As explained in #5577, it's safer to set NSURLIsExcludedFromBackupKey to the cache directory instead of the cache file. This must be done before anything is created/moved into the directory so as to prevent iCloud backup from kicking in at any point (during the legacy db move, for example).

As explained in mapbox#5577, it's safer to set NSURLIsExcludedFromBackupKey to the cache directory instead of the cache file. This must be done before anything is created/moved into the directory so as to prevent iCloud backup from kicking in at any point (during the legacy db move, for example).
@1ec5 1ec5 added bug iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS offline labels Jul 6, 2016
@1ec5

1ec5 commented Jul 6, 2016

Copy link
Copy Markdown
Contributor

@friedbunny, v3.3.0 is only a few days away, but I think we should cherry-pick this change into the release branch, given that #5124 was speculative and @dapetcu21 has provided verification steps in #5577 (comment).

@1ec5 1ec5 added this to the ios-v3.3.0 milestone Jul 6, 2016
@1ec5

1ec5 commented Jul 6, 2016

Copy link
Copy Markdown
Contributor

So there's one problem with this PR: it assumes the developer won't need to put anything else in the application's subfolder in Application Support. I don't think we can make that assumption. Perhaps we need to move the cache yet again to a subfolder within that folder that we can set the exclusion flag on.

/cc @jfirebaugh

@friedbunny

friedbunny commented Jul 6, 2016 •

Copy link
Copy Markdown
Contributor

This behavior has changed since v3.2.0 — see #5124. We were indeed failing to set the NSURLIsExcludedFromBackupKey flag on the first launch because we tried to do so before cache.db existed. #5124 moved the flagging step to immediately after cache.db is created, which should be created synchronously.

You can verify that cache.db exists on the first launch and that we’re setting the flag correctly in v3.3.0:

NSAssert(![[NSFileManager defaultManager] fileExistsAtPath:cachePath], @"Cache database file shouldn't already exist.");

_mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String);

NSAssert([[NSFileManager defaultManager] fileExistsAtPath:cachePath], @"Cache database file should exist.");

// Avoid backing up the offline cache onto iCloud, because it can be
// redownloaded. Ideally, we’d even put the ambient cache in Caches, so
// it can be reclaimed by the system when disk space runs low. But
// unfortunately it has to live in the same file as offline resources.
[cacheURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:NULL];

NSError *error = nil;
NSNumber *exclusionFlag = nil;
[cacheURL getResourceValue:&exclusionFlag forKey:NSURLIsExcludedFromBackupKey error:&error];
NSAssert(exclusionFlag && [exclusionFlag boolValue], @"Backup exclusion flag should be set for cache database.");

We don't set the backup exclusion flag on the directory because it may interfere with legitimately backup-able files that the developer also stores there. If we were to set the flag on a directory, it would need to be one that we’re certain to own exclusively — i.e., a new subdirectory and not the generic ~/Library/Application Support/tld.app.bundle.id that we use now.

@friedbunny

Copy link
Copy Markdown
Contributor

I should also say: thanks for helping out, @dapetcu21. 🙇

If you’re still seeing the backup exclusion flag not being set properly on the master or release-ios-v3.3.0 branches (as opposed to the v3.2.0 release build), then we’ll have to go ahead and move cache.db into a subdirectory and set the flag on that subdirectory.

@dapetcu21

Copy link
Copy Markdown
Author

Wait. I'm using a precompiled v3.2.0. I didn't check with master or v3.3.0. I agree with all the above. Setting it on the dir might not be such a great idea after all since other apps might use that directory.

When submitting this PR I made 2 assumptions:

a) That the code didn't change between my build and master (which is wrong)
b) That initializing DefaultFileSource didn't create the file synchronously. (which, again, might be wrong)

So if b) is wrong, I guess it should work and we should't merge this.

@1ec5

1ec5 commented Jul 6, 2016

Copy link
Copy Markdown
Contributor

Ah, sorry for the confusion then. I just assumed a PR made against master would fix a bug in master, my bad.

I don’t know whether DefaultFileSource creates the file synchronously or asynchronously. If you merely call -[MGLOfflineStorage sharedOfflineStorage] to exercise this code but don’t initialize an MGLMapView or refer to the shared offline storage’s packs property until later, does the cache.db file get created right away?

@friedbunny

friedbunny commented Jul 6, 2016 •

Copy link
Copy Markdown
Contributor

does the cache.db file get created right away?

Yes, in my observation it is created immediately via the call to new mbgl::DefaultFileSource() here.

Frankly, we’re not completely confident in when exactly cache.db is being created, but @kkaefer speculated that the SQLite database having PRAGMA synchronous = FULL; enabled by default (as it is) should cause the database to be written to disk immediately.

a) That the code didn't change between my build and master (which is wrong)

Apologies for the confusion, @dapetcu21 — between releases we merge changes into master, then create a short-lived branch for pre-releases and the eventual final release (currently: release-ios-v3.3.0) before eventually merging that back to master.

I’m going to close this pull request and #5577, but we’d quite appreciate if you could help to verify the fix for this issue by giving the current v3.3.0 beta release a try. Thanks for time you've put into this, @dapetcu21.

@friedbunny friedbunny closed this Jul 6, 2016
friedbunny added a commit that referenced this pull request Jul 7, 2016
We cannot guarantee that the offline/ambient tile cache (cache.db) will be created in a timely manner, so it's safer to set the backup exclusion key on its containing directory.

But, because we placed cache.db in the base of the app's Application Support directory (where backups are expected to be allowed), we need to move cache.db into a directory that we control and can exclude from backups with impunity.

This is an amalgam of #5578 (directory exclusion) and #5585 (refactored URL methods, but without the new database size method).

Also:
* Prefix mapbox subdirectory with dot, for extra safety
* Update changelog entry with new ticket, less speculation
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS offline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants