Ingen beskrivning

FIRInstanceIDBackupExcludedPlist.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRInstanceIDBackupExcludedPlist.h"
  17. #import "FIRInstanceIDLogger.h"
  18. @interface FIRInstanceIDBackupExcludedPlist ()
  19. @property(nonatomic, readwrite, copy) NSString *fileName;
  20. @property(nonatomic, readwrite, copy) NSString *subDirectoryName;
  21. @property(nonatomic, readwrite, strong) NSDictionary *cachedPlistContents;
  22. @end
  23. @implementation FIRInstanceIDBackupExcludedPlist
  24. - (instancetype)initWithFileName:(NSString *)fileName subDirectory:(NSString *)subDirectory {
  25. self = [super init];
  26. if (self) {
  27. _fileName = [fileName copy];
  28. _subDirectoryName = [subDirectory copy];
  29. }
  30. return self;
  31. }
  32. - (BOOL)writeDictionary:(NSDictionary *)dict error:(NSError **)error {
  33. NSString *path = [self plistPathInDirectory];
  34. if (![dict writeToFile:path atomically:YES]) {
  35. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeBackupExcludedPlist000,
  36. @"Failed to write to %@.plist", self.fileName);
  37. return NO;
  38. }
  39. // Successfully wrote contents -- change the in-memory contents
  40. self.cachedPlistContents = [dict copy];
  41. NSURL *URL = [NSURL fileURLWithPath:path];
  42. if (error) {
  43. *error = nil;
  44. }
  45. NSDictionary *preferences = [URL resourceValuesForKeys:@[ NSURLIsExcludedFromBackupKey ]
  46. error:error];
  47. if ([preferences[NSURLIsExcludedFromBackupKey] boolValue]) {
  48. return YES;
  49. }
  50. BOOL success = [URL setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:error];
  51. if (!success) {
  52. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeBackupExcludedPlist001,
  53. @"Error excluding %@ from backup, %@", [URL lastPathComponent],
  54. error ? *error : @"");
  55. }
  56. return success;
  57. }
  58. - (BOOL)deleteFile:(NSError **)error {
  59. BOOL success = YES;
  60. NSString *path = [self plistPathInDirectory];
  61. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  62. success = [[NSFileManager defaultManager] removeItemAtPath:path error:error];
  63. }
  64. // remove the in-memory contents
  65. self.cachedPlistContents = nil;
  66. return success;
  67. }
  68. - (NSDictionary *)contentAsDictionary {
  69. if (!self.cachedPlistContents) {
  70. NSString *path = [self plistPathInDirectory];
  71. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  72. self.cachedPlistContents = [[NSDictionary alloc] initWithContentsOfFile:path];
  73. }
  74. }
  75. return self.cachedPlistContents;
  76. }
  77. - (BOOL)doesFileExist {
  78. NSString *path = [self plistPathInDirectory];
  79. return [[NSFileManager defaultManager] fileExistsAtPath:path];
  80. }
  81. #pragma mark - Private
  82. - (NSString *)plistPathInDirectory {
  83. NSArray *directoryPaths;
  84. NSString *plistNameWithExtension = [NSString stringWithFormat:@"%@.plist", self.fileName];
  85. directoryPaths =
  86. NSSearchPathForDirectoriesInDomains([self supportedDirectory], NSUserDomainMask, YES);
  87. NSArray *components = @[ directoryPaths.lastObject, _subDirectoryName, plistNameWithExtension ];
  88. return [NSString pathWithComponents:components];
  89. }
  90. - (NSSearchPathDirectory)supportedDirectory {
  91. #if TARGET_OS_TV
  92. return NSCachesDirectory;
  93. #else
  94. return NSApplicationSupportDirectory;
  95. #endif
  96. }
  97. @end