xref: /haiku/src/kits/package/FetchUtils.cpp (revision 410ed2fbba58819ac21e27d3676739728416761d)
1 /*
2  * Copyright 2020, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 
10 #include "FetchUtils.h"
11 #include "string.h"
12 
13 #include <Entry.h>
14 #include <Node.h>
15 #include <TypeConstants.h>
16 
17 namespace BPackageKit {
18 
19 namespace BPrivate {
20 
21 
22 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
23 
24 #define DL_COMPLETE_ATTR "Meta:DownloadCompleted"
25 
26 
27 /*static*/ bool
28 FetchUtils::IsDownloadCompleted(const char* path)
29 {
30 	BEntry entry(path, true);
31 	BNode node(&entry);
32 	return IsDownloadCompleted(node);
33 }
34 
35 
36 /*static*/ bool
37 FetchUtils::IsDownloadCompleted(BNode& node)
38 {
39     bool isComplete;
40     status_t status = _GetAttribute(node, DL_COMPLETE_ATTR,
41         B_BOOL_TYPE, &isComplete, sizeof(isComplete));
42     if (status != B_OK) {
43         // Most likely cause is that the attribute was not written,
44         // for example by previous versions of the Package Kit.
45         // Worst outcome of assuming a partial download should be
46         // a no-op range request.
47         isComplete = false;
48     }
49     return isComplete;
50 }
51 
52 
53 /*static*/ status_t
54 FetchUtils::MarkDownloadComplete(BNode& node)
55 {
56     bool isComplete = true;
57     return _SetAttribute(node, DL_COMPLETE_ATTR,
58         B_BOOL_TYPE, &isComplete, sizeof(isComplete));
59 }
60 
61 
62 /*static*/ status_t
63 FetchUtils::SetFileType(BNode& node, const char* type)
64 {
65 	return _SetAttribute(node, "BEOS:TYPE",
66         B_MIME_STRING_TYPE, type, strlen(type) + 1);
67 }
68 
69 status_t
70 FetchUtils::_SetAttribute(BNode& node, const char* attrName,
71     type_code type, const void* data, size_t size)
72 {
73 	if (node.InitCheck() != B_OK)
74 		return node.InitCheck();
75 
76 	ssize_t written = node.WriteAttr(attrName, type, 0, data, size);
77 	if (written != (ssize_t)size) {
78 		if (written < 0)
79 			return (status_t)written;
80 		return B_IO_ERROR;
81 	}
82 	return B_OK;
83 }
84 
85 
86 status_t
87 FetchUtils::_GetAttribute(BNode& node, const char* attrName,
88     type_code type, void* data, size_t size)
89 {
90 	if (node.InitCheck() != B_OK)
91 		return node.InitCheck();
92 
93 	ssize_t read = node.ReadAttr(attrName, type, 0, data, size);
94 	if (read != (ssize_t)size) {
95 		if (read < 0)
96 			return (status_t)read;
97 		return B_IO_ERROR;
98 	}
99 	return B_OK;
100 }
101 
102 
103 #endif // HAIKU_TARGET_PLATFORM_HAIKU
104 
105 }	// namespace BPrivate
106 
107 }	// namespace BPackageKit
108