Repository navigation
Expand file tree
/
Copy pathinc-store.php
More file actions
254 lines (230 loc) · 6.86 KB
/
Copy pathinc-store.php
File metadata and controls
254 lines (230 loc) · 6.86 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
// ---------------------------------------------------------------------------
// CyborgSCIF - local file storage
//
// Layout under DATA_DIR:
// devices/<guid>.json one per logged in device, mtime = last call
// people/<identity>.json nick, registration, channels (like CyborgShell spaces)
// nicks/<hex nick> nick claim, contents = identity (created with 'x')
// channelnames/<hex name> channel name claim, contents = channel GUID ('x')
// channels/<guid>/channel.json owner, name, topic, modes, key, limit
// channels/<guid>/members/<identity> index of who is in the channel now
// reaper.stamp last timeout sweep
//
// Writes go to a temp file then rename, so readers never see half a file and
// need no lock. Read-change-write is done under a lock file created with
// fopen 'x' (only one caller can create it). No flock, no database.
// ---------------------------------------------------------------------------
function store_Init()
{
$arrDirs = ['', 'devices', 'people', 'nicks', 'channelnames', 'channels'];
foreach ($arrDirs as $strDir)
{
if (!is_dir(DATA_DIR . $strDir))
{
@mkdir(DATA_DIR . $strDir, 0755, true);
}
}
}
// ---------------------------------------------------------------------------
// JSON files
// ---------------------------------------------------------------------------
function store_Read($strPath_a)
{
$varResult = null;
if (is_file($strPath_a))
{
$strJSON = @file_get_contents($strPath_a);
if ($strJSON !== false)
{
$varData = json_decode($strJSON, true);
if (is_array($varData))
{
$varResult = $varData;
}
}
}
return $varResult;
}
// temp + rename: readers see the old file or the new one, never a partial one
function store_Write($strPath_a, $arrData_a)
{
$strTemp = $strPath_a . '.' . irc_NewGuid() . '.tmp';
if (@file_put_contents($strTemp, json_encode($arrData_a, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)) === false)
{
throw new Exception('store: cannot write ' . $strTemp);
}
if (!@rename($strTemp, $strPath_a))
{
@unlink($strTemp);
throw new Exception('store: cannot rename to ' . $strPath_a);
}
}
// read, change, write under the file's lock. $fnChange_a gets the current data
// (or null) and returns the new data, or null to leave the file alone.
function store_Update($strPath_a, $fnChange_a)
{
$varResult = null;
$strLock = store_Lock($strPath_a);
try
{
$varResult = call_user_func($fnChange_a, store_Read($strPath_a));
if ($varResult !== null)
{
store_Write($strPath_a, $varResult);
}
}
catch (Exception $objEx)
{
store_Unlock($strLock);
throw $objEx;
}
store_Unlock($strLock);
return $varResult;
}
// ---------------------------------------------------------------------------
// Lock files
// ---------------------------------------------------------------------------
function store_Lock($strPath_a)
{
$strLock = $strPath_a . '.lock';
$objLock = @fopen($strLock, 'x');
$intWaited = 0;
while ($objLock === false && $intWaited < LOCK_MAX_WAIT_US)
{
$intMTime = @filemtime($strLock);
if ($intMTime !== false && $intMTime < time() - LOCK_STALE)
{
// holder died long ago
@unlink($strLock);
}
else
{
usleep(LOCK_WAIT_US);
$intWaited += LOCK_WAIT_US;
}
$objLock = @fopen($strLock, 'x');
}
if ($objLock === false)
{
throw new Exception('store: lock timeout ' . $strLock);
}
fclose($objLock);
return $strLock;
}
function store_Unlock($strLock_a)
{
@unlink($strLock_a);
}
// ---------------------------------------------------------------------------
// Claim files: exclusive create, contents = owner. true if we now hold it.
// ---------------------------------------------------------------------------
function store_Claim($strPath_a, $strOwner_a)
{
$blnResult = false;
$objFile = @fopen($strPath_a, 'x');
if ($objFile !== false)
{
fwrite($objFile, $strOwner_a);
fclose($objFile);
$blnResult = true;
}
elseif (store_ClaimOwner($strPath_a) === $strOwner_a)
{
$blnResult = true;
}
return $blnResult;
}
function store_ClaimOwner($strPath_a)
{
$strResult = '';
if (is_file($strPath_a))
{
$strResult = trim((string)@file_get_contents($strPath_a));
}
return $strResult;
}
function store_Release($strPath_a)
{
@unlink($strPath_a);
}
// ---------------------------------------------------------------------------
// Paths
// ---------------------------------------------------------------------------
function store_SafeKey($strKey_a)
{
return preg_replace('/[^A-Za-z0-9_-]/', '', $strKey_a);
}
function store_DevicePath($strGuid_a)
{
return DATA_DIR . 'devices/' . store_SafeKey($strGuid_a) . '.json';
}
function store_PersonPath($strIdentity_a)
{
return DATA_DIR . 'people/' . store_SafeKey($strIdentity_a) . '.json';
}
// nicks and channel names are case insensitive (ASCII casemapping) and may hold
// characters that are awkward in file names, so they are hex encoded
function store_NickPath($strNick_a)
{
return DATA_DIR . 'nicks/' . bin2hex(strtolower($strNick_a));
}
function store_ChannelNamePath($strName_a)
{
return DATA_DIR . 'channelnames/' . bin2hex(strtolower($strName_a));
}
function store_ChannelDir($strGuid_a)
{
return DATA_DIR . 'channels/' . store_SafeKey($strGuid_a) . '/';
}
function store_ChannelPath($strGuid_a)
{
return store_ChannelDir($strGuid_a) . 'channel.json';
}
function store_MemberPath($strGuid_a, $strIdentity_a)
{
return store_ChannelDir($strGuid_a) . 'members/' . store_SafeKey($strIdentity_a);
}
// file names in a folder, sorted ('' dir missing = [])
function store_List($strDir_a, $strPattern_a)
{
$arrResult = [];
$arrFiles = glob($strDir_a . $strPattern_a);
if (is_array($arrFiles))
{
foreach ($arrFiles as $strFile)
{
$arrResult[] = basename($strFile);
}
}
sort($arrResult);
return $arrResult;
}
function store_RemoveDir($strDir_a)
{
$strDir = rtrim($strDir_a, '/') . '/';
$arrItems = @scandir($strDir);
if (is_array($arrItems))
{
foreach ($arrItems as $strBase)
{
$strItem = $strDir . $strBase;
if ($strBase !== '.' && $strBase !== '..')
{
if (is_dir($strItem))
{
store_RemoveDir($strItem);
}
else
{
@unlink($strItem);
}
}
}
}
@rmdir($strDir_a);
}
function store_Now()
{
return date('Y-m-d H:i:s');
}