FIRCLSRolloutsPersistenceManager.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #include "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
  16. #include "Crashlytics/Crashlytics/Components/FIRCLSUserLogging.h"
  17. #import "Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
  18. #import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
  19. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  20. #if SWIFT_PACKAGE
  21. @import FirebaseCrashlyticsSwift;
  22. #else // Swift Package Manager
  23. #import <FirebaseCrashlytics/FirebaseCrashlytics-Swift.h>
  24. #endif // CocoaPods
  25. @interface FIRCLSRolloutsPersistenceManager : NSObject <FIRCLSPersistenceLog>
  26. @property(nonatomic, readonly) FIRCLSFileManager *fileManager;
  27. @end
  28. @implementation FIRCLSRolloutsPersistenceManager
  29. - (instancetype)initWithFileManager:(FIRCLSFileManager *)fileManager {
  30. self = [super init];
  31. if (!self) {
  32. return nil;
  33. }
  34. _fileManager = fileManager;
  35. return self;
  36. }
  37. - (void)updateRolloutsStateToPersistenceWithRollouts:(NSData *_Nonnull)rollouts
  38. reportID:(NSString *_Nonnull)reportID {
  39. NSString *rolloutsPath = [[[_fileManager activePath] stringByAppendingPathComponent:reportID]
  40. stringByAppendingPathComponent:FIRCLSReportRolloutsFile];
  41. if (![_fileManager fileExistsAtPath:rolloutsPath]) {
  42. if (![_fileManager createFileAtPath:rolloutsPath contents:nil attributes:nil]) {
  43. FIRCLSDebugLog(@"Could not create rollouts.clsrecord file. Error was code: %d - message: %s",
  44. errno, strerror(errno));
  45. }
  46. }
  47. NSFileHandle *rolloutsFile = [NSFileHandle fileHandleForUpdatingAtPath:rolloutsPath];
  48. dispatch_sync(FIRCLSGetLoggingQueue(), ^{
  49. [rolloutsFile seekToEndOfFile];
  50. [rolloutsFile writeData:rollouts];
  51. NSData *newLineData = [@"\n" dataUsingEncoding:NSUTF8StringEncoding];
  52. [rolloutsFile writeData:newLineData];
  53. });
  54. }
  55. - (void)debugLogWithMessage:(NSString *_Nonnull)message {
  56. FIRCLSDebugLog(message);
  57. }
  58. @end