xref: /haiku/src/apps/haikudepot/server/AbstractSingleFileServerProcess.cpp (revision 54624bda43f13312a491fe1c91d22834be3374f5)
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 
6 
7 #include "AbstractSingleFileServerProcess.h"
8 
9 #include "HaikuDepotConstants.h"
10 #include "Logger.h"
11 #include "ServerHelper.h"
12 #include "ServerSettings.h"
13 #include "StorageUtils.h"
14 
15 
16 AbstractSingleFileServerProcess::AbstractSingleFileServerProcess(
17 	uint32 options)
18 	:
19 	AbstractServerProcess(options)
20 {
21 }
22 
23 
24 AbstractSingleFileServerProcess::~AbstractSingleFileServerProcess()
25 {
26 }
27 
28 
29 status_t
30 AbstractSingleFileServerProcess::RunInternal()
31 {
32 	if (Logger::IsInfoEnabled())
33 		printf("[%s] will fetch data\n", Name());
34 
35 	BPath localPath;
36 	status_t result = GetLocalPath(localPath);
37 
38 	if (result != B_OK)
39 		return result;
40 
41 	BString urlPathComponent = UrlPathComponent();
42 
43 	if (IsSuccess(result) && HasOption(SERVER_PROCESS_DROP_CACHE))
44 		result = DeleteLocalFile(localPath);
45 
46 	bool hasData = false;
47 	off_t size;
48 
49 	if (IsSuccess(result))
50 		result = StorageUtils::ExistsObject(localPath, &hasData, NULL, &size);
51 
52 	hasData = hasData && size > 0;
53 
54 	if (IsSuccess(result) && ShouldAttemptNetworkDownload(hasData)) {
55 		result = DownloadToLocalFileAtomically(
56 			localPath,
57 			ServerSettings::CreateFullUrl(urlPathComponent));
58 
59 		if (!IsSuccess(result)) {
60 			if (hasData) {
61 				printf("[%s] failed to update data, but have old data "
62 					"anyway so carry on with that\n", Name());
63 				result = B_OK;
64 			} else {
65 				printf("[%s] failed to obtain data\n", Name());
66 			}
67 		} else {
68 			if (Logger::IsInfoEnabled())
69 				printf("[%s] did fetch data\n", Name());
70 		}
71 	}
72 
73 	if (IsSuccess(result)) {
74 		status_t hasDataResult = StorageUtils::ExistsObject(
75 			localPath, &hasData, NULL, &size);
76 
77 		hasData = hasData && size > 0;
78 
79 		if (hasDataResult == B_OK && !hasData)
80 			result = HD_ERR_NO_DATA;
81 	}
82 
83 	if (IsSuccess(result)) {
84 		printf("[%s] will process data\n", Name());
85 		result = ProcessLocalData();
86 
87 		switch (result) {
88 			case B_OK:
89 				printf("[%s] did process data\n", Name());
90 				break;
91 			default:
92 				MoveDamagedFileAside(localPath);
93 				break;
94 		}
95 	}
96 
97 	return result;
98 }
99 
100 
101 status_t
102 AbstractSingleFileServerProcess::GetStandardMetaDataPath(BPath& path) const
103 {
104 	return GetLocalPath(path);
105 }