xref: /haiku/src/apps/haikudepot/server/AbstractSingleFileServerProcess.cpp (revision efafab643ce980e3f3c916795ed302599f6b4f66)
1 /*
2  * Copyright 2017-2018, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "AbstractSingleFileServerProcess.h"
6 
7 #include "HaikuDepotConstants.h"
8 #include "Logger.h"
9 #include "ServerSettings.h"
10 #include "StorageUtils.h"
11 
12 
13 AbstractSingleFileServerProcess::AbstractSingleFileServerProcess(
14 	AbstractServerProcessListener* listener, uint32 options)
15 	:
16 	AbstractServerProcess(listener, options)
17 {
18 }
19 
20 
21 AbstractSingleFileServerProcess::~AbstractSingleFileServerProcess()
22 {
23 }
24 
25 
26 status_t
27 AbstractSingleFileServerProcess::RunInternal()
28 {
29 	if (Logger::IsInfoEnabled())
30 		printf("[%s] will fetch data\n", Name());
31 
32 	BPath localPath = LocalPath();
33 	BString urlPathComponent = UrlPathComponent();
34 	status_t result = B_OK;
35 
36 	if (IsSuccess(result) && HasOption(SERVER_PROCESS_DROP_CACHE))
37 		result = DeleteLocalFile(localPath);
38 
39 	bool hasData = false;
40 	off_t size;
41 
42 	if (IsSuccess(result))
43 		result = StorageUtils::ExistsObject(localPath, &hasData, NULL, &size);
44 
45 	hasData = hasData && size > 0;
46 
47 	if (IsSuccess(result) && ShouldAttemptNetworkDownload(hasData)) {
48 		result = DownloadToLocalFileAtomically(
49 			localPath,
50 			ServerSettings::CreateFullUrl(urlPathComponent));
51 	}
52 
53 	if (IsSuccess(result)) {
54 		status_t hasDataResult = StorageUtils::ExistsObject(
55 			localPath, &hasData, NULL, &size);
56 
57 		hasData = hasData && size > 0;
58 
59 		if (hasDataResult == B_OK && !hasData)
60 			result = HD_ERR_NO_DATA;
61 	}
62 
63 	if (IsSuccess(result)) {
64 		if (Logger::IsInfoEnabled())
65 			printf("[%s] did fetch data\n", Name());
66 
67 			// now load the data in and process it.
68 
69 		printf("[%s] will process data\n", Name());
70 		result = ProcessLocalData();
71 
72 		switch (result) {
73 			case B_OK:
74 				printf("[%s] did process data\n", Name());
75 				break;
76 			default:
77 				MoveDamagedFileAside(localPath);
78 				break;
79 		}
80 	}
81 
82 	return result;
83 }
84 
85