vfs: check userland buffers before reading them.
[haiku.git] / src / kits / tracker / TrashWatcher.cpp
blob1b5d58463f0ad1546444c480918e735e047536e3
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
36 #include "TrashWatcher.h"
38 #include <string.h>
40 #include <Debug.h>
41 #include <Directory.h>
42 #include <NodeMonitor.h>
43 #include <Path.h>
44 #include <Volume.h>
45 #include <VolumeRoster.h>
47 #include "Attributes.h"
48 #include "Bitmaps.h"
49 #include "FSUtils.h"
50 #include "Tracker.h"
53 // #pragma mark - BTrashWatcher
56 BTrashWatcher::BTrashWatcher()
58 BLooper("TrashWatcher", B_LOW_PRIORITY),
59 fTrashNodeList(20, true)
61 FSCreateTrashDirs();
62 WatchTrashDirs();
63 fTrashFull = CheckTrashDirs();
64 UpdateTrashIcons();
66 // watch volumes
67 TTracker::WatchNode(0, B_WATCH_MOUNT, this);
71 BTrashWatcher::~BTrashWatcher()
73 stop_watching(this);
77 bool
78 BTrashWatcher::IsTrashNode(const node_ref* testNode) const
80 int32 count = fTrashNodeList.CountItems();
81 for (int32 index = 0; index < count; index++) {
82 node_ref* nref = fTrashNodeList.ItemAt(index);
83 if (nref->node == testNode->node && nref->device == testNode->device)
84 return true;
87 return false;
91 void
92 BTrashWatcher::MessageReceived(BMessage* message)
94 if (message->what != B_NODE_MONITOR) {
95 _inherited::MessageReceived(message);
96 return;
99 switch (message->FindInt32("opcode")) {
100 case B_ENTRY_CREATED:
101 if (!fTrashFull) {
102 fTrashFull = true;
103 UpdateTrashIcons();
105 break;
107 case B_ENTRY_MOVED:
109 // allow code to fall through if move is from/to trash
110 // but do nothing for moves in the same directory
111 ino_t toDir;
112 ino_t fromDir;
113 message->FindInt64("from directory", &fromDir);
114 message->FindInt64("to directory", &toDir);
115 if (fromDir == toDir)
116 break;
118 // fall-through
119 case B_DEVICE_UNMOUNTED:
120 case B_ENTRY_REMOVED:
122 bool full = CheckTrashDirs();
123 if (fTrashFull != full) {
124 fTrashFull = full;
125 UpdateTrashIcons();
127 break;
129 // We should handle DEVICE_UNMOUNTED here too to remove trash
131 case B_DEVICE_MOUNTED:
133 dev_t device;
134 BDirectory trashDir;
135 if (message->FindInt32("new device", &device) == B_OK
136 && FSGetTrashDir(&trashDir, device) == B_OK) {
137 node_ref trashNode;
138 trashDir.GetNodeRef(&trashNode);
139 TTracker::WatchNode(&trashNode, B_WATCH_DIRECTORY, this);
140 fTrashNodeList.AddItem(new node_ref(trashNode));
142 // Check if the new volume has anything trashed.
143 if (CheckTrashDirs() && !fTrashFull) {
144 fTrashFull = true;
145 UpdateTrashIcons();
148 break;
154 void
155 BTrashWatcher::UpdateTrashIcons()
157 BVolumeRoster roster;
158 BVolume volume;
159 roster.Rewind();
161 BDirectory trashDir;
162 while (roster.GetNextVolume(&volume) == B_OK) {
163 if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) {
164 // pull out the icons for the current trash state from resources
165 // and apply them onto the trash directory node
166 size_t vectorSize = 0;
167 const void* vectorData = GetTrackerResources()->LoadResource(
168 B_VECTOR_ICON_TYPE,
169 fTrashFull ? R_TrashFullIcon : R_TrashIcon, &vectorSize);
171 if (vectorData) {
172 trashDir.WriteAttr(kAttrIcon, B_VECTOR_ICON_TYPE, 0,
173 vectorData, vectorSize);
174 } else
175 TRESPASS();
181 void
182 BTrashWatcher::WatchTrashDirs()
184 BVolumeRoster volRoster;
185 volRoster.Rewind();
186 BVolume volume;
187 while (volRoster.GetNextVolume(&volume) == B_OK) {
188 if (volume.IsReadOnly() || !volume.IsPersistent())
189 continue;
191 BDirectory trashDir;
192 if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) {
193 node_ref trash_node;
194 trashDir.GetNodeRef(&trash_node);
195 watch_node(&trash_node, B_WATCH_DIRECTORY, this);
196 fTrashNodeList.AddItem(new node_ref(trash_node));
202 bool
203 BTrashWatcher::CheckTrashDirs()
205 BVolumeRoster volRoster;
206 volRoster.Rewind();
207 BVolume volume;
208 while (volRoster.GetNextVolume(&volume) == B_OK) {
209 if (volume.IsReadOnly() || !volume.IsPersistent())
210 continue;
212 BDirectory trashDir;
213 FSGetTrashDir(&trashDir, volume.Device());
214 trashDir.Rewind();
215 BEntry entry;
216 if (trashDir.GetNextEntry(&entry) == B_OK)
217 return true;
220 return false;