Paul Martin
2016-04-16 eecaad8b8e2c447429c31a01d49260ddd6b4ee03
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Copyright 2015 gitblit.com.
 *
 * Licensed 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.
 */
package com.gitblit.manager;
 
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
 
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.gitblit.IStoredSettings;
import com.gitblit.Keys;
import com.gitblit.models.FilestoreModel;
import com.gitblit.models.FilestoreModel.Status;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.JsonUtils.GmtDateTypeAdapter;
import com.google.gson.ExclusionStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject;
import com.google.inject.Singleton;
 
/**
 * FilestoreManager handles files uploaded via:
 *     + git-lfs
 *  + ticket attachment (TBD)
 *
 * Files are stored using their SHA256 hash (as per git-lfs)
 * If the same file is uploaded through different repositories no additional space is used
 * Access is controlled through the current repository permissions.
 *
 * TODO: Identify what and how the actual BLOBs should work with federation
 *
 * @author Paul Martin
 *
 */
@Singleton
public class FilestoreManager implements IFilestoreManager {
 
    private final Logger logger = LoggerFactory.getLogger(getClass());
 
    private final IRuntimeManager runtimeManager;
    
    private final IStoredSettings settings;
 
    public static final int UNDEFINED_SIZE = -1;
 
    private static final String METAFILE = "filestore.json";
 
    private static final String METAFILE_TMP = "filestore.json.tmp";
 
    protected static final Type METAFILE_TYPE = new TypeToken<Collection<FilestoreModel>>() {}.getType();
 
    private Map<String, FilestoreModel > fileCache = new ConcurrentHashMap<String, FilestoreModel>();
 
 
    @Inject
    public FilestoreManager(IRuntimeManager runtimeManager) {
        this.runtimeManager = runtimeManager;
        this.settings = runtimeManager.getSettings();
    }
 
    @Override
    public IManager start() {
 
        // Try to load any existing metadata
        File dir = getStorageFolder();
        dir.mkdirs();
        File metadata = new File(dir, METAFILE);
 
        if (metadata.exists()) {
            Collection<FilestoreModel> items = null;
 
            Gson gson = gson();
            try (FileReader file = new FileReader(metadata)) {
                items = gson.fromJson(file, METAFILE_TYPE);
                file.close();
 
            } catch (IOException e) {
                e.printStackTrace();
            }
 
            for(Iterator<FilestoreModel> itr = items.iterator(); itr.hasNext(); ) {
                FilestoreModel model = itr.next();
                fileCache.put(model.oid, model);
            }
 
            logger.info("Loaded {} items from filestore metadata file", fileCache.size());
        }
        else
        {
            logger.info("No filestore metadata file found");
        }
 
        return this;
    }
 
    @Override
    public IManager stop() {
        return this;
    }
 
 
    @Override
    public boolean isValidOid(String oid) {
        //NOTE: Assuming SHA256 support only as per git-lfs
        return Pattern.matches("[a-fA-F0-9]{64}", oid);
    }
 
    @Override
    public FilestoreModel.Status addObject(String oid, long size, UserModel user, RepositoryModel repo) {
 
        //Handle access control
        if (!user.canPush(repo)) {
            if (user == UserModel.ANONYMOUS) {
                return Status.AuthenticationRequired;
            } else {
                return Status.Error_Unauthorized;
            }
        }
 
        //Handle object details
        if (!isValidOid(oid)) { return Status.Error_Invalid_Oid; }
 
        if (fileCache.containsKey(oid)) {
            FilestoreModel item = fileCache.get(oid);
 
            if (!item.isInErrorState() && (size != UNDEFINED_SIZE) && (item.getSize() != size)) {
                return Status.Error_Size_Mismatch;
            }
 
            item.addRepository(repo.name);
 
            if (item.isInErrorState()) {
                item.reset(user, size);
            }
        } else {
 
            if (size  < 0) {return Status.Error_Invalid_Size; }
            if ((getMaxUploadSize() != UNDEFINED_SIZE) && (size > getMaxUploadSize())) { return Status.Error_Exceeds_Size_Limit; }
 
            FilestoreModel model = new FilestoreModel(oid, size, user, repo.name);
            fileCache.put(oid, model);
            saveFilestoreModel(model);
        }
 
        return fileCache.get(oid).getStatus();
    }
 
    @Override
    public FilestoreModel.Status uploadBlob(String oid, long size, UserModel user, RepositoryModel repo, InputStream streamIn) {
 
        //Access control and object logic
        Status state = addObject(oid, size, user, repo);
 
        if (state != Status.Upload_Pending) {
            return state;
        }
 
        FilestoreModel model = fileCache.get(oid);
 
        if (!model.actionUpload(user)) {
            return Status.Upload_In_Progress;
        } else {
            long actualSize = 0;
            File file = getStoragePath(oid);
 
            try {
                file.getParentFile().mkdirs();
                file.createNewFile();
 
                try (FileOutputStream streamOut = new FileOutputStream(file)) {
 
                    actualSize = IOUtils.copyLarge(streamIn, streamOut);
 
                    streamOut.flush();
                    streamOut.close();
 
                    if (model.getSize() != actualSize) {
                        model.setStatus(Status.Error_Size_Mismatch, user);
 
                        logger.warn(MessageFormat.format("Failed to upload blob {0} due to size mismatch, expected {1} got {2}",
                                oid, model.getSize(), actualSize));
                    } else {
                        String actualOid = "";
 
                        try (FileInputStream fileForHash = new FileInputStream(file)) {
                            actualOid = DigestUtils.sha256Hex(fileForHash);
                            fileForHash.close();
                        }
 
                        if (oid.equalsIgnoreCase(actualOid)) {
                            model.setStatus(Status.Available, user);
                        } else {
                            model.setStatus(Status.Error_Hash_Mismatch, user);
 
                            logger.warn(MessageFormat.format("Failed to upload blob {0} due to hash mismatch, got {1}", oid, actualOid));
                        }
                    }
                }
            } catch (Exception e) {
 
                model.setStatus(Status.Error_Unknown, user);
                logger.warn(MessageFormat.format("Failed to upload blob {0}", oid), e);
            } finally {
                saveFilestoreModel(model);
            }
 
            if (model.isInErrorState()) {
                file.delete();
                model.removeRepository(repo.name);
            }
        }
 
        return model.getStatus();
    }
 
    private FilestoreModel.Status canGetObject(String oid, UserModel user, RepositoryModel repo) {
 
        //Access Control
        if (!user.canView(repo)) {
            if (user == UserModel.ANONYMOUS) {
                return Status.AuthenticationRequired;
            } else {
                return Status.Error_Unauthorized;
            }
        }
 
        //Object Logic
        if (!isValidOid(oid)) {
            return Status.Error_Invalid_Oid;
        }
 
        if (!fileCache.containsKey(oid)) {
            return Status.Unavailable;
        }
 
        FilestoreModel item = fileCache.get(oid);
 
        if (item.getStatus() == Status.Available) {
            return Status.Available;
        }
 
        return Status.Unavailable;
    }
 
    @Override
    public FilestoreModel getObject(String oid, UserModel user, RepositoryModel repo) {
 
        if (canGetObject(oid, user, repo) == Status.Available) {
            return fileCache.get(oid);
        }
 
        return null;
    }
 
    @Override
    public FilestoreModel.Status downloadBlob(String oid, UserModel user, RepositoryModel repo, OutputStream streamOut) {
 
        //Access control and object logic
        Status status = canGetObject(oid, user, repo);
 
        if (status != Status.Available) {
            return status;
        }
 
        FilestoreModel item = fileCache.get(oid);
 
        if (streamOut != null) {
            try (FileInputStream streamIn = new FileInputStream(getStoragePath(oid))) {
 
                IOUtils.copyLarge(streamIn, streamOut);
 
                streamOut.flush();
                streamIn.close();
            } catch (EOFException e) {
                logger.error(MessageFormat.format("Client aborted connection for {0}", oid), e);
                return Status.Error_Unexpected_Stream_End;
            } catch (Exception e) {
                logger.error(MessageFormat.format("Failed to download blob {0}", oid), e);
                return Status.Error_Unknown;
            }
        }
 
        return item.getStatus();
    }
 
    @Override
    public List<FilestoreModel> getAllObjects(List<RepositoryModel> viewableRepositories) {
        
        List<String> viewableRepositoryNames = new ArrayList<String>(viewableRepositories.size());
        
        for (RepositoryModel repository : viewableRepositories) {
            viewableRepositoryNames.add(repository.name);
        }
        
        if (viewableRepositoryNames.size() == 0) {
            return null;
        }
        
        final Collection<FilestoreModel> allFiles = fileCache.values();
        List<FilestoreModel> userViewableFiles = new ArrayList<FilestoreModel>(allFiles.size());
        
        for (FilestoreModel file : allFiles) {
            if (file.isInRepositoryList(viewableRepositoryNames)) {
                userViewableFiles.add(file);
            }
        }
        
        return userViewableFiles;                
    }
 
    @Override
    public File getStorageFolder() {
        return runtimeManager.getFileOrFolder(Keys.filestore.storageFolder, "${baseFolder}/lfs");
    }
 
    @Override
    public File getStoragePath(String oid) {
         return new File(getStorageFolder(), oid.substring(0, 2).concat("/").concat(oid.substring(2)));
    }
 
    @Override
    public long getMaxUploadSize() {
        return settings.getLong(Keys.filestore.maxUploadSize, -1);
    }
 
    @Override
    public long getFilestoreUsedByteCount() {
        Iterator<FilestoreModel> iterator = fileCache.values().iterator();
        long total = 0;
 
        while (iterator.hasNext()) {
 
            FilestoreModel item = iterator.next();
            if (item.getStatus() == Status.Available) {
                total += item.getSize();
            }
        }
 
        return total;
    }
 
    @Override
    public long getFilestoreAvailableByteCount() {
 
        try {
            return Files.getFileStore(getStorageFolder().toPath()).getUsableSpace();
        } catch (IOException e) {
            logger.error(MessageFormat.format("Failed to retrive available space in Filestore {0}", e));
        }
 
        return UNDEFINED_SIZE;
    };
 
    private synchronized void saveFilestoreModel(FilestoreModel model) {
 
        File metaFile = new File(getStorageFolder(), METAFILE);
        File metaFileTmp = new File(getStorageFolder(), METAFILE_TMP);
        boolean isNewFile = false;
 
        try {
            if (!metaFile.exists()) {
                metaFile.getParentFile().mkdirs();
                metaFile.createNewFile();
                isNewFile = true;
            }
            FileUtils.copyFile(metaFile, metaFileTmp);
 
        } catch (IOException e) {
            logger.error("Writing filestore model to file {0}, {1}", METAFILE, e);
        }
 
        try (RandomAccessFile fs = new RandomAccessFile(metaFileTmp, "rw")) {
 
            if (isNewFile) {
                fs.writeBytes("[");
            } else {
                fs.seek(fs.length() - 1);
                fs.writeBytes(",");
            }
 
            fs.writeBytes(gson().toJson(model));
            fs.writeBytes("]");
 
            fs.close();
 
        } catch (IOException e) {
            logger.error("Writing filestore model to file {0}, {1}", METAFILE_TMP, e);
        }
 
        try {
            if (metaFileTmp.exists()) {
                FileUtils.copyFile(metaFileTmp, metaFile);
 
                metaFileTmp.delete();
            } else {
                logger.error("Writing filestore model to file {0}", METAFILE);
            }
        }
        catch (IOException e) {
            logger.error("Writing filestore model to file {0}, {1}", METAFILE, e);
        }
    }
 
    /*
     * Intended for testing purposes only
     */
    @Override
    public void clearFilestoreCache() {
        fileCache.clear();
    }
 
    private static Gson gson(ExclusionStrategy... strategies) {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Date.class, new GmtDateTypeAdapter());
        if (!ArrayUtils.isEmpty(strategies)) {
            builder.setExclusionStrategies(strategies);
        }
        return builder.create();
    }
 
}