xref: /haiku/src/apps/diskusage/Snapshot.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3  * Distributed under the terms of the MIT/X11 license.
4  *
5  * Copyright (c) 1999 Mike Steed. You are free to use and distribute this software
6  * as long as it is accompanied by it's documentation and this copyright notice.
7  * The software comes with no warranty, etc.
8  */
9 
10 
11 #include "Snapshot.h"
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include <Directory.h>
17 #include <Mime.h>
18 #include <NodeInfo.h>
19 #include <Path.h>
20 #include <Volume.h>
21 
22 
23 static const char* kFileType = B_FILE_MIME_TYPE;
24 static const char* kDirType = "application/x-vnd.Be-directory";
25 static const char* kVolumeType = "application/x-vnd.Be-volume";
26 
27 
28 FileInfo::FileInfo()
29 	:
30 	pseudo(false),
31 	size(0),
32 	count(0),
33 	parent(NULL),
34 	children()
35 {
36 }
37 
38 
39 FileInfo::~FileInfo()
40 {
41 	while (children.size() != 0) {
42 		FileInfo* child = *children.begin();
43 		delete child;
44 		children.erase(children.begin());
45 	}
46 }
47 
48 
49 void
50 FileInfo::GetPath(string& path) const
51 {
52 	if (pseudo) {
53 		path.assign(ref.name);
54 	} else {
55 		BEntry entry(&ref, true);
56 		BPath pathObj(&entry);
57 		path.assign(pathObj.Path());
58 	}
59 }
60 
61 
62 FileInfo*
63 FileInfo::FindChild(const char* name) const
64 {
65 	vector<FileInfo*>::const_iterator i = children.begin();
66 	while (i != children.end()) {
67 		if (strcmp((*i)->ref.name, name) == 0)
68 			return *i;
69 		i++;
70 	}
71 
72 	return NULL;
73 }
74 
75 
76 BMimeType*
77 FileInfo::Type() const
78 {
79 	char mimeStr[B_MIME_TYPE_LENGTH] = { '\0' };
80 	if (parent == NULL) {
81 		// This is the volume's root directory; treat it as a volume type.
82 		strlcpy(mimeStr, kVolumeType, sizeof(mimeStr));
83 	} else {
84 		// Get the MIME type from the registrar.
85 		BNode node(&ref);
86 		if (node.InitCheck() == B_OK) {
87 			BNodeInfo nodeInfo(&node);
88 			if (nodeInfo.InitCheck() == B_OK) {
89 				status_t s = nodeInfo.GetType(mimeStr);
90 				if (s != B_OK && children.size() > 0) {
91 					if (s == B_ENTRY_NOT_FOUND) {
92 						// This status appears to be returned only for files on
93 						// BFS volumes (e.g., CDFS volumes return B_BAD_VALUE).
94 						//nodeInfo.SetType(kDirType);
95 					}
96 					strlcpy(mimeStr, kDirType, sizeof(mimeStr));
97 				}
98 			}
99 		}
100 	}
101 
102 	if (strlen(mimeStr) == 0)
103 		strlcpy(mimeStr, kFileType, sizeof(mimeStr));
104 
105 	return new BMimeType(mimeStr);
106 }
107 
108 
109 // #pragma mark -
110 
111 
112 VolumeSnapshot::VolumeSnapshot(const BVolume* volume)
113 {
114 	char nameBuffer[B_FILE_NAME_LENGTH];
115 	volume->GetName(nameBuffer);
116 	name = nameBuffer;
117 
118 	capacity = volume->Capacity();
119 	freeBytes = volume->FreeBytes();
120 	rootDir = NULL;
121 	freeSpace = NULL;
122 }
123 
124 
125 VolumeSnapshot::~VolumeSnapshot()
126 {
127 	delete rootDir;
128 	delete freeSpace;
129 }
130