1 /* 2 * Copyright 2017, 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 "Logger.h" 8 #include "ServerSettings.h" 9 #include "StorageUtils.h" 10 11 12 AbstractSingleFileServerProcess::AbstractSingleFileServerProcess( 13 AbstractServerProcessListener* listener, uint32 options) 14 : 15 AbstractServerProcess(listener, options) 16 { 17 } 18 19 20 AbstractSingleFileServerProcess::~AbstractSingleFileServerProcess() 21 { 22 } 23 24 25 status_t 26 AbstractSingleFileServerProcess::RunInternal() 27 { 28 if (Logger::IsInfoEnabled()) 29 printf("[%s] will fetch data\n", Name()); 30 31 BPath localPath = LocalPath(); 32 BString urlPathComponent = UrlPathComponent(); 33 status_t result = B_OK; 34 35 if (IsSuccess(result) && HasOption(SERVER_PROCESS_DROP_CACHE)) 36 result = DeleteLocalFile(localPath); 37 38 bool hasData; 39 off_t size; 40 41 if (IsSuccess(result)) 42 result = StorageUtils::ExistsObject(localPath, &hasData, NULL, &size); 43 44 hasData = hasData && size > 0; 45 46 if (IsSuccess(result) && ShouldAttemptNetworkDownload(hasData)) { 47 result = DownloadToLocalFileAtomically( 48 localPath, 49 ServerSettings::CreateFullUrl(urlPathComponent)); 50 } 51 52 if (IsSuccess(result) || result == APP_ERR_NOT_MODIFIED) { 53 status_t hasDataResult = StorageUtils::ExistsObject( 54 localPath, &hasData, NULL, &size); 55 56 hasData = hasData && size > 0; 57 58 if (hasDataResult == B_OK && !hasData) 59 result = APP_ERR_NO_DATA; 60 } 61 62 if (IsSuccess(result) || result == APP_ERR_NOT_MODIFIED) { 63 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