xref: /haiku/src/bin/package/PackageWritingUtils.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "PackageWritingUtils.h"
8 
9 #include <dirent.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include <package/hpkg/HPKGDefs.h>
15 
16 #include <AutoDeleter.h>
17 #include <AutoDeleterPosix.h>
18 
19 
20 status_t
21 add_current_directory_entries(BPackageWriter& packageWriter,
22 	BPackageWriterListener& listener, bool skipPackageInfo)
23 {
24 	// open the current directory
25 	DIR* dir = opendir(".");
26 	if (dir == NULL) {
27 		listener.PrintError("Error: Failed to opendir '.': %s\n",
28 			strerror(errno));
29 		return errno;
30 	}
31 	DirCloser dirCloser(dir);
32 
33 	while (dirent* entry = readdir(dir)) {
34 		// skip "." and ".."
35 		if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
36 			continue;
37 
38 		// skip the .PackageInfo, if requested
39 		if (skipPackageInfo
40 			&& strcmp(entry->d_name,
41 				BPackageKit::BHPKG::B_HPKG_PACKAGE_INFO_FILE_NAME) == 0) {
42 			continue;
43 		}
44 
45 		status_t error = packageWriter.AddEntry(entry->d_name);
46 		if (error != B_OK)
47 			return error;
48 	}
49 
50 	return B_OK;
51 }
52