-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathSPTPersistentCacheHeader.m
More file actions
150 lines (125 loc) · 5.95 KB
/
SPTPersistentCacheHeader.m
File metadata and controls
150 lines (125 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2016 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#import <SPTPersistentCache/SPTPersistentCacheHeader.h>
#import "NSError+SPTPersistentCacheDomainErrors.h"
#include "crc32iso3309.h"
#include <sys/xattr.h>
const SPTPersistentCacheMagicType SPTPersistentCacheMagicValue = 0x46545053; // SPTF
const size_t SPTPersistentCacheRecordHeaderSize = sizeof(SPTPersistentCacheRecordHeader);
static NSString* const SPTPersistentCacheRecordHeaderAttributeName = @"com.spotify.cache";
static int const SPTPersistentCacheHeaderInvalidResult = -1;
_Static_assert(sizeof(SPTPersistentCacheRecordHeader) == 64,
"Struct SPTPersistentCacheRecordHeader has to be packed without padding");
_Static_assert(sizeof(SPTPersistentCacheRecordHeader) % 4 == 0,
"Struct size has to be multiple of 4");
NS_INLINE BOOL SPTPersistentCachePointerMagicAlignCheck(const void *ptr)
{
const unsigned align = _Alignof(SPTPersistentCacheMagicType);
uint64_t v = (uint64_t)(ptr);
return (v % align == 0);
}
SPTPersistentCacheRecordHeader SPTPersistentCacheRecordHeaderMake(uint64_t ttl,
uint64_t payloadSize,
uint64_t updateTime,
BOOL isLocked)
{
SPTPersistentCacheRecordHeader dummy;
memset(&dummy, 0, SPTPersistentCacheRecordHeaderSize);
SPTPersistentCacheRecordHeader *header = &dummy;
header->magic = SPTPersistentCacheMagicValue;
header->headerSize = (uint32_t)SPTPersistentCacheRecordHeaderSize;
header->refCount = (isLocked ? 1 : 0);
header->ttl = ttl;
header->payloadSizeBytes = payloadSize;
header->updateTimeSec = updateTime;
header->crc = SPTPersistentCacheCalculateHeaderCRC(header);
return dummy;
}
SPTPersistentCacheRecordHeader *SPTPersistentCacheGetHeaderFromData(void *data, size_t size)
{
if (size < SPTPersistentCacheRecordHeaderSize) {
return NULL;
}
return (SPTPersistentCacheRecordHeader *)data;
}
NSError* SPTPersistentCacheGetHeaderFromFileWithPath(NSString *filePath, SPTPersistentCacheRecordHeader *header)
{
memset(header, 0, SPTPersistentCacheRecordHeaderSize);
ssize_t size = getxattr([filePath UTF8String], [SPTPersistentCacheRecordHeaderAttributeName UTF8String], header, SPTPersistentCacheRecordHeaderSize, 0, 0);
if (size == SPTPersistentCacheHeaderInvalidResult) {
// try to load the legacy header. the fileHandle object uses file descriptor under the hood and responsible for closing it on deallocation.
NSFileHandle* fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSMutableData* rawData = [[fileHandle readDataOfLength:SPTPersistentCacheRecordHeaderSize] mutableCopy];
SPTPersistentCacheRecordHeader *legacyHeader = SPTPersistentCacheGetHeaderFromData(rawData.mutableBytes, rawData.length);
// If not enough data to cast to header, its not the file we can process
if (legacyHeader == NULL) {
return [NSError spt_persistentDataCacheErrorWithCode:SPTPersistentCacheLoadingErrorNotEnoughDataToGetHeader];
}
memcpy(header, legacyHeader, SPTPersistentCacheRecordHeaderSize);
}
return SPTPersistentCacheCheckValidHeader(header);
}
NSError* SPTPersistentCacheSetHeaderForFileWithPath(NSString *filepath, const SPTPersistentCacheRecordHeader *header) {
int res = setxattr([filepath UTF8String], [SPTPersistentCacheRecordHeaderAttributeName UTF8String], header, SPTPersistentCacheRecordHeaderSize, 0, 0);
if (res == SPTPersistentCacheHeaderInvalidResult) {
return [NSError spt_persistentDataCacheErrorWithCode:SPTPersistentCacheLoadingErrorFileAttributeOperationFail];
}
return nil;
}
int /*SPTPersistentCacheLoadingError*/ SPTPersistentCacheValidateHeader(const SPTPersistentCacheRecordHeader *header)
{
if (header == NULL) {
return SPTPersistentCacheLoadingErrorInternalInconsistency;
}
// Check that header could be read according to alignment
if (!SPTPersistentCachePointerMagicAlignCheck(header)) {
return SPTPersistentCacheLoadingErrorHeaderAlignmentMismatch;
}
// 1. Check magic
if (header->magic != SPTPersistentCacheMagicValue) {
return SPTPersistentCacheLoadingErrorMagicMismatch;
}
// 2. Check CRC
uint32_t crc = SPTPersistentCacheCalculateHeaderCRC(header);
if (crc != header->crc) {
return SPTPersistentCacheLoadingErrorInvalidHeaderCRC;
}
// 3. Check header size
if (header->headerSize != SPTPersistentCacheRecordHeaderSize) {
return SPTPersistentCacheLoadingErrorWrongHeaderSize;
}
return -1;
}
NSError * SPTPersistentCacheCheckValidHeader(SPTPersistentCacheRecordHeader *header)
{
int code = SPTPersistentCacheValidateHeader(header);
if (code == -1) { // No error
return nil;
}
return [NSError spt_persistentDataCacheErrorWithCode:code];
}
uint32_t SPTPersistentCacheCalculateHeaderCRC(const SPTPersistentCacheRecordHeader *header)
{
if (header == NULL) {
return 0;
}
return spt_crc32((const uint8_t *)header, SPTPersistentCacheRecordHeaderSize - sizeof(header->crc));
}