2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include <RemoveEngine.h>
13 #include <Directory.h>
21 // #pragma mark - BRemoveEngine
24 BRemoveEngine::BRemoveEngine()
31 BRemoveEngine::~BRemoveEngine()
36 BRemoveEngine::BController
*
37 BRemoveEngine::Controller() const
44 BRemoveEngine::SetController(BController
* controller
)
46 fController
= controller
;
51 BRemoveEngine::RemoveEntry(const Entry
& entry
)
55 status_t error
= entry
.GetPath(pathBuffer
, path
);
59 return _RemoveEntry(path
);
64 BRemoveEngine::_RemoveEntry(const char* path
)
67 if (fController
!= NULL
&& !fController
->EntryStarted(path
))
72 if (lstat(path
, &st
) < 0) {
73 return _HandleEntryError(path
, errno
, "Couldn't access \"%s\": %s\n",
74 path
, strerror(errno
));
77 // recurse, if entry is a directory
78 if (S_ISDIR(st
.st_mode
)) {
81 status_t error
= directory
.SetTo(path
);
83 return _HandleEntryError(path
, error
,
84 "Failed to open directory \"%s\": %s\n", path
, strerror(error
));
87 char buffer
[sizeof(dirent
) + B_FILE_NAME_LENGTH
];
88 dirent
*entry
= (dirent
*)buffer
;
89 while (directory
.GetNextDirents(entry
, sizeof(buffer
), 1) == 1) {
90 if (strcmp(entry
->d_name
, ".") == 0
91 || strcmp(entry
->d_name
, "..") == 0) {
95 // construct child entry path
97 error
= childPath
.SetTo(path
, entry
->d_name
);
99 return _HandleEntryError(path
, error
,
100 "Failed to construct entry path from dir \"%s\" and name "
101 "\"%s\": %s\n", path
, entry
->d_name
, strerror(error
));
105 error
= _RemoveEntry(childPath
.Path());
107 if (fController
!= NULL
108 && fController
->EntryFinished(path
, error
)) {
117 if (S_ISDIR(st
.st_mode
)) {
118 if (rmdir(path
) < 0) {
119 return _HandleEntryError(path
, errno
,
120 "Failed to remove \"%s\": %s\n", path
, strerror(errno
));
123 if (unlink(path
) < 0) {
124 return _HandleEntryError(path
, errno
,
125 "Failed to unlink \"%s\": %s\n", path
, strerror(errno
));
129 if (fController
!= NULL
)
130 fController
->EntryFinished(path
, B_OK
);
137 BRemoveEngine::_NotifyErrorVarArgs(status_t error
, const char* format
,
140 if (fController
!= NULL
) {
142 message
.SetToFormatVarArgs(format
, args
);
143 fController
->ErrorOccurred(message
, error
);
149 BRemoveEngine::_HandleEntryError(const char* path
, status_t error
,
150 const char* format
, ...)
152 if (fController
== NULL
)
156 va_start(args
, format
);
157 _NotifyErrorVarArgs(error
, format
, args
);
160 if (fController
->EntryFinished(path
, error
))
166 // #pragma mark - BController
169 BRemoveEngine::BController::BController()
174 BRemoveEngine::BController::~BController()
180 BRemoveEngine::BController::EntryStarted(const char* path
)
187 BRemoveEngine::BController::EntryFinished(const char* path
, status_t error
)
189 return error
== B_OK
;
194 BRemoveEngine::BController::ErrorOccurred(const char* message
, status_t error
)
199 } // namespace BPrivate