xref: /haiku/src/servers/launch/AbstractEmptyDirectoryJob.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "AbstractEmptyDirectoryJob.h"
8 
9 #include <stdio.h>
10 
11 #include <Directory.h>
12 #include <Entry.h>
13 
14 
15 AbstractEmptyDirectoryJob::AbstractEmptyDirectoryJob(const BString& name)
16 	:
17 	BJob(name)
18 {
19 }
20 
21 
22 status_t
23 AbstractEmptyDirectoryJob::CreateAndEmpty(const char* path) const
24 {
25 	BEntry entry(path);
26 	if (!entry.Exists()) {
27 		create_directory(path, 0777);
28 
29 		status_t status = entry.SetTo(path);
30 		if (status != B_OK) {
31 			fprintf(stderr, "Cannot create directory \"%s\": %s\n", path,
32 				strerror(status));
33 			return status;
34 		}
35 	}
36 
37 	return _EmptyDirectory(entry, false);
38 }
39 
40 
41 status_t
42 AbstractEmptyDirectoryJob::_EmptyDirectory(BEntry& directoryEntry,
43 	bool remove) const
44 {
45 	BDirectory directory(&directoryEntry);
46 	BEntry entry;
47 	while (directory.GetNextEntry(&entry) == B_OK) {
48 		if (entry.IsDirectory())
49 			_EmptyDirectory(entry, true);
50 		else
51 			entry.Remove();
52 	}
53 
54 	return remove ? directoryEntry.Remove() : B_OK;
55 }
56