xref: /haiku/src/apps/haikudepot/server/AbstractSingleFileServerProcess.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2017-2022, 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 	HDINFO("[%s] will fetch data", Name());
33 	BPath localPath;
34 	status_t result = GetLocalPath(localPath);
35 
36 	if (result != B_OK)
37 		return result;
38 
39 	BString urlPathComponent = UrlPathComponent();
40 
41 	if (IsSuccess(result) && HasOption(SERVER_PROCESS_DROP_CACHE))
42 		result = DeleteLocalFile(localPath);
43 
44 	bool hasData = false;
45 	off_t size;
46 
47 	if (IsSuccess(result))
48 		result = StorageUtils::ExistsObject(localPath, &hasData, NULL, &size);
49 
50 	hasData = hasData && size > 0;
51 
52 	if (IsSuccess(result) && ShouldAttemptNetworkDownload(hasData)) {
53 		result = DownloadToLocalFileAtomically(
54 			localPath,
55 			ServerSettings::CreateFullUrl(urlPathComponent));
56 
57 		if (!IsSuccess(result)) {
58 			if (hasData) {
59 				HDINFO("[%s] failed to update data, but have old data "
60 					"anyway so carry on with that", Name());
61 				result = B_OK;
62 			} else
63 				HDERROR("[%s] failed to obtain data", Name());
64 		} else
65 			HDINFO("[%s] did fetch data", Name());
66 	}
67 
68 	if (IsSuccess(result)) {
69 		status_t hasDataResult = StorageUtils::ExistsObject(
70 			localPath, &hasData, NULL, &size);
71 
72 		hasData = hasData && size > 0;
73 
74 		if (hasDataResult == B_OK && !hasData)
75 			result = HD_ERR_NO_DATA;
76 	}
77 
78 	if (IsSuccess(result)) {
79 		HDINFO("[%s] will process data", Name());
80 		result = ProcessLocalData();
81 
82 		switch (result) {
83 			case B_OK:
84 				HDINFO("[%s] did process data", Name());
85 				break;
86 			default:
87 				HDERROR("[%s] failed processing data", Name());
88 				MoveDamagedFileAside(localPath);
89 				break;
90 		}
91 	}
92 
93 	return result;
94 }
95 
96 
97 status_t
98 AbstractSingleFileServerProcess::GetStandardMetaDataPath(BPath& path) const
99 {
100 	return GetLocalPath(path);
101 }
102