xref: /haiku/src/kits/shared/AttributeUtilities.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2005-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2016, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "AttributeUtilities.h"
9 
10 #include <fs_attr.h>
11 #include <Node.h>
12 #include <StorageDefs.h>
13 
14 
15 static const int kCopyBufferSize = 64 * 1024;
16 	// 64 KB
17 
18 
19 namespace BPrivate {
20 
21 
22 status_t
23 CopyAttributes(BNode& source, BNode& destination)
24 {
25 	char attrName[B_ATTR_NAME_LENGTH];
26 	while (source.GetNextAttrName(attrName) == B_OK) {
27 		// get attr info
28 		attr_info attrInfo;
29 		status_t status = source.GetAttrInfo(attrName, &attrInfo);
30 		if (status != B_OK)
31 			return status;
32 
33 		// copy the attribute
34 		char buffer[kCopyBufferSize];
35 		off_t offset = 0;
36 		off_t bytesLeft = attrInfo.size;
37 
38 		// Go at least once through the loop, so that an empty attribute will be
39 		// created as well
40 		do {
41 			size_t toRead = kCopyBufferSize;
42 			if ((off_t)toRead > bytesLeft)
43 				toRead = bytesLeft;
44 
45 			// read
46 			ssize_t bytesRead = source.ReadAttr(attrName, attrInfo.type,
47 				offset, buffer, toRead);
48 			if (bytesRead < 0)
49 				return bytesRead;
50 
51 			// write
52 			ssize_t bytesWritten = destination.WriteAttr(attrName,
53 				attrInfo.type, offset, buffer, bytesRead);
54 			if (bytesWritten < 0)
55 				return bytesWritten;
56 
57 			bytesLeft -= bytesRead;
58 			offset += bytesRead;
59 		} while (bytesLeft > 0);
60 	}
61 	return B_OK;
62 }
63 
64 
65 }	// namespace BPrivate
66 
67