xref: /haiku/src/apps/haikudepot/server/ServerIconExportUpdateProcess.cpp (revision 44b874666af6b25da6311e5a1c506ee3761f4268)
1 /*
2  * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include "ServerIconExportUpdateProcess.h"
7 
8 #include <stdio.h>
9 #include <sys/stat.h>
10 #include <time.h>
11 
12 #include <FileIO.h>
13 #include <support/ZlibCompressionAlgorithm.h>
14 
15 #include "ServerSettings.h"
16 #include "StorageUtils.h"
17 #include "TarArchiveService.h"
18 
19 
20 /*! This constructor will locate the cached data in a standardized location */
21 
22 ServerIconExportUpdateProcess::ServerIconExportUpdateProcess(
23 	const BPath& localStorageDirectoryPath)
24 {
25 	fLocalStorageDirectoryPath = localStorageDirectoryPath;
26 }
27 
28 
29 ServerIconExportUpdateProcess::~ServerIconExportUpdateProcess()
30 {
31 }
32 
33 
34 status_t
35 ServerIconExportUpdateProcess::Run()
36 {
37 	BPath tarGzFilePath(tmpnam(NULL));
38 	status_t result = B_OK;
39 
40 	printf("will start fetching icons\n");
41 
42 	result = Download(tarGzFilePath);
43 
44 	if (result != APP_ERR_NOT_MODIFIED) {
45 		if (result != B_OK)
46 			return result;
47 
48 		printf("delete any existing stored data\n");
49 		StorageUtils::RemoveDirectoryContents(fLocalStorageDirectoryPath);
50 
51 		BFile *tarGzFile = new BFile(tarGzFilePath.Path(), O_RDONLY);
52 		BDataIO* tarIn;
53 
54 		BZlibDecompressionParameters* zlibDecompressionParameters
55 			= new BZlibDecompressionParameters();
56 
57 		result = BZlibCompressionAlgorithm()
58 			.CreateDecompressingInputStream(tarGzFile,
59 			    zlibDecompressionParameters, tarIn);
60 
61 		if (result == B_OK) {
62 			result = TarArchiveService::Unpack(*tarIn,
63 			    fLocalStorageDirectoryPath);
64 
65 			if (result == B_OK) {
66 				if (0 != remove(tarGzFilePath.Path())) {
67 					fprintf(stdout, "unable to delete the temporary tgz path; "
68 					    "%s\n", tarGzFilePath.Path());
69 				}
70 			}
71 		}
72 
73 		delete tarGzFile;
74 	}
75 
76 	printf("did complete fetching icons\n");
77 
78 	return result;
79 }
80 
81 
82 void
83 ServerIconExportUpdateProcess::GetStandardMetaDataPath(BPath& path) const
84 {
85 	path.SetTo(fLocalStorageDirectoryPath.Path());
86 	path.Append("hicn/info.json");
87 }
88 
89 
90 void
91 ServerIconExportUpdateProcess::GetStandardMetaDataJsonPath(
92 	BString& jsonPath) const
93 {
94 		// the "$" here indicates that the data is at the top level.
95 	jsonPath.SetTo("$");
96 }
97 
98 
99 const char*
100 ServerIconExportUpdateProcess::LoggingName() const
101 {
102 	return "icon-export-update";
103 }
104 
105 
106 status_t
107 ServerIconExportUpdateProcess::Download(BPath& tarGzFilePath)
108 {
109 	return DownloadToLocalFile(tarGzFilePath,
110 		ServerSettings::CreateFullUrl("/__pkgicon/all.tar.gz"), 0, 0);
111 }