-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.php
More file actions
296 lines (267 loc) · 8.93 KB
/
Copy pathFileSystem.php
File metadata and controls
296 lines (267 loc) · 8.93 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* @file
* Gitolite FileSystem abstraction
*/
namespace Gitolite;
use Gitolite\Entity\Entity,
Gitolite\Entity\Group,
Gitolite\Entity\User,
Gitolite\Entity\Repository;
class FileSystem
{
protected $directory;
protected $paths = array(
'keys' => 'keydir',
'configuration' => 'conf/gitolite.conf',
'groups' => 'conf/managed/groups',
'repositories' => 'conf/managed/repos',
);
protected $includes = array(
'managed/groups/*.conf' => 'managed\/groups\/\*\.conf',
'managed/repos/*.conf' => 'managed\/repos\/\*\.conf',
);
/**
* Constructs a file system.
*
* @param string $directory The root directory for the file system.
* @param array $paths The paths where files are located. Only used if $includes is also included.
* @param array $includes
* The includes which should be added to the gitolite.conf.
* key is the include statement, value is a regex to test if the include is already present (eg. ['groups/*.conf':'repos\/\*\.conf'])
*/
public function __construct($directory, $paths = NULL, $includes = NULL)
{
$this->directory = $directory;
if (is_array($paths) && is_array($includes)) {
$this->paths = $paths;
$this->includes = $includes;
}
}
/**
* Provides the absolute path to the specified resource.
*
* @return string The directory in which resource lives.
*/
public function getPath($resource)
{
return $this->directory . '/' . $this->paths[$resource];
}
/**
* Gets the path where the given entity should be saved.
*
* @param Gitolite\Entity\Entity | string The entity itself or the entity's classname.
*/
public function getEntitySavePath($entity)
{
if ($entity instanceof Entity) {
$entity = $entity->getEntityType();
}
switch((string) $entity) {
case 'User':
return $this->getPath('keys');
case 'Group':
return $this->getPath('groups');
case 'Repository':
return $this->getPath('repositories');
}
}
/**
* Ensure that the necessary includes have been added to the gitolite config file.
*
* @return bool|string FALSE if no changes were made otherwise the path to the config file.
*/
public function setupIncludes()
{
$filepath = $this->getPath('configuration');
$config = file_get_contents($filepath);
$missing_includes = array();
foreach ($this->includes as $include => $regexp) {
if (!preg_match('/include\s+"' . $regexp . '"/', $config, $matches)) {
$missing_includes[] = "include \"$include\"";
}
}
if (count($missing_includes) == 0) {
return FALSE;
}
$config .= "\n" . implode("\n", $missing_includes);
if (FALSE === file_put_contents($filepath, $config)) {
throw new \RuntimeException("Could not save config");
}
return $filepath;
}
/**
* Returns a reference to a user with the given username.
*
* @return Gitolite\User
*/
public function loadUser($username)
{
$keyFilePaths = $this->findKeyFiles($username);
$keyFiles = array();
if ($keyFilePaths) {
foreach ($keyFilePaths as $keyFilePath) {
$file = $this->loadFile($keyFilePath);
if (!is_null($file)) {
$keyFiles[] = $file;
}
}
}
$user = new User($username, $keyFiles);
return $user;
}
/**
* Loads all users in the admin repo.
*
* @return array of Gitolite\User objects.
*/
public function loadUsers()
{
$users = array();
$keyFilePaths = $this->findKeyFiles();
foreach ($keyFilePaths as $keyFilePath) {
$parsed = User::parseKeyFilename(basename($keyFilePath));
if ($parsed) {
$username = $parsed['username'];
if (!isset($users[$username])) {
$users[$username] = $this->loadUser($parsed['username']);
}
}
}
return $users;
}
/**
* Returns an array of key filepaths.
*
* @param string $username The optional username to search for.
* @return array of absolute key filepath strings.
*/
public function findKeyFiles($username = '*')
{
$keyDir = $this->getEntitySavePath('User');
$keyFiles = glob("$keyDir/$username@*.pub");
return $keyFiles;
}
/**
* Load a group object from the file system.
*
* @param string $groupName The name of the group.
* @param string $groupType The type of group (User|Repository).
*
* @return Gitolite\Group
*/
public function loadGroup($groupName, $groupType)
{
$filename = Group::formatFilename($groupName, $groupType);
$file = $this->loadFile($this->getEntitySavePath('Group') . "/$filename");
$group = new Group($groupName, $groupType, $file);
return $group;
}
/**
* Load a group object from the file system.
*
* @param string $groupType The type of group (User|Repository).
*
* @return Gitolite\Group
*/
public function loadGroups($groupType)
{
$search = $this->getEntitySavePath('Group') . '/' . Group::formatFilename('*', $groupType);
$groups = array();
foreach (glob($search) as $filename) {
$parsed = Group::parseFilename(basename($filename));
$file = $this->loadFile($filename);
$groups[] = new Group($parsed['name'], $groupType, $file);
}
return $groups;
}
/**
* Load a repo object from the file system.
*
* @param string $repoName The name of the group.
*
* @return Gitolite\Entity\Repository
*/
public function loadRepository($repoName)
{
$filename = Repository::formatFilename($repoName);
$file = $this->loadFile($this->getEntitySavePath('Repository') . "/$filename");
$repo = new Repository($repoName, $file);
return $repo;
}
/**
* Saves the entity to the file system.
*
* @param Gitolite\Entity $entity The entity to save.
* @throws RuntimeException if save fails.
*/
public function save(Entity $entity)
{
$directory = $this->getEntitySavePath($entity);
if (!file_exists($directory)) {
if (!mkdir($directory, 0755, TRUE)) {
throw new \RuntimeException("Could not create " . $entity->getEntityType() . " directory $directory");
}
}
elseif (!is_dir($directory)) {
throw new \RuntimeException("$directory is not a directory");
}
$filePaths = array('added' => array(), 'deleted' => array());
foreach ($entity->getFiles() as $file) {
$filePath = "$directory/" . $file->getFilename();
if ($file->isDeleted()) {
if (!unlink($filePath)) {
throw new \RuntimeException("Could not delete file $filePath");
}
$entity->removeFile($file);
$filePaths['deleted'][] = $filePath;
continue;
}
$current_contents = file_exists($filePath) ? file_get_contents($filePath) : '';
$new_contents = $file->getContents();
if ($new_contents == $current_contents) {
continue;
}
if (FALSE === file_put_contents($filePath, $new_contents)) {
throw new \RuntimeException("Could not save " . $entity->getType() . " " . $entity->getName() . " to $filePath");
}
$filePaths['added'][] = $filePath;
}
return $filePaths;
}
/**
* Delete the entity from the file system.
*
* @param Gitolite\Entity $entity The entity to delete.
*/
public function delete(Entity $entity)
{
$directory = $this->getEntitySavePath($entity);
$filePaths = array();
foreach ($entity->getFiles() as $file) {
$filePaths[] = $filePath = "$directory/" . $file->getFilename();
if (file_exists($filePath)) {
unlink($filePath);
}
}
return $filePaths;
}
/**
* Loads a file object from it's filepath.
*
* @param string $filepath The path to the file to load.
* @throws RuntimeException if file cannot be loaded.
*/
protected function loadFile($filepath)
{
$file = NULL;
if (file_exists($filepath)) {
$contents = file_get_contents($filepath);
if (FALSE === $contents) {
throw new \RuntimeException("Could not load file $filepath");
}
$file = new File(basename($filepath), $contents);
}
return $file;
}
}