Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/*
2
 * Copyright 2020 Google LLC
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
 
17
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORDirectorySizeTracker.h"
18
 
19
@interface GDTCORDirectorySizeTracker ()
20
 
21
/** The observed directory path. */
22
@property(nonatomic, readonly) NSString *directoryPath;
23
 
24
/** The cached content size of the observed directory. */
25
@property(nonatomic, nullable) NSNumber *cachedSizeBytes;
26
 
27
@end
28
 
29
@implementation GDTCORDirectorySizeTracker
30
 
31
- (instancetype)initWithDirectoryPath:(NSString *)path {
32
  self = [super init];
33
  if (self) {
34
    _directoryPath = path;
35
  }
36
  return self;
37
}
38
 
39
- (GDTCORStorageSizeBytes)directoryContentSize {
40
  if (self.cachedSizeBytes == nil) {
41
    self.cachedSizeBytes = @([self calculateDirectoryContentSize]);
42
  }
43
 
44
  return self.cachedSizeBytes.unsignedLongLongValue;
45
}
46
 
47
- (void)fileWasAddedAtPath:(NSString *)path withSize:(GDTCORStorageSizeBytes)fileSize {
48
  if (![path hasPrefix:self.directoryPath]) {
49
    // Ignore because the file is not inside the directory.
50
    return;
51
  }
52
 
53
  self.cachedSizeBytes = @([self directoryContentSize] + fileSize);
54
}
55
 
56
- (void)fileWasRemovedAtPath:(NSString *)path withSize:(GDTCORStorageSizeBytes)fileSize {
57
  if (![path hasPrefix:self.directoryPath]) {
58
    // Ignore because the file is not inside the directory.
59
    return;
60
  }
61
 
62
  self.cachedSizeBytes = @([self directoryContentSize] - fileSize);
63
}
64
 
65
- (void)resetCachedSize {
66
  self.cachedSizeBytes = nil;
67
}
68
 
69
- (GDTCORStorageSizeBytes)calculateDirectoryContentSize {
70
  NSArray *prefetchedProperties = @[ NSURLIsRegularFileKey, NSURLFileSizeKey ];
71
  uint64_t totalBytes = 0;
72
  NSURL *directoryURL = [NSURL fileURLWithPath:self.directoryPath];
73
 
74
  NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]
75
                 enumeratorAtURL:directoryURL
76
      includingPropertiesForKeys:prefetchedProperties
77
                         options:NSDirectoryEnumerationSkipsHiddenFiles
78
                    errorHandler:^BOOL(NSURL *_Nonnull url, NSError *_Nonnull error) {
79
                      return YES;
80
                    }];
81
 
82
  for (NSURL *fileURL in enumerator) {
83
    @autoreleasepool {
84
      NSNumber *isRegularFile;
85
      [fileURL getResourceValue:&isRegularFile forKey:NSURLIsRegularFileKey error:nil];
86
      if (isRegularFile.boolValue) {
87
        totalBytes += [self fileSizeAtURL:fileURL];
88
      }
89
    }
90
  }
91
 
92
  return totalBytes;
93
}
94
 
95
- (GDTCORStorageSizeBytes)fileSizeAtURL:(NSURL *)fileURL {
96
  NSNumber *fileSize;
97
  [fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil];
98
  return fileSize.unsignedLongLongValue;
99
}
100
 
101
@end