11590Srgrimes/*
21590Srgrimes * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
31590Srgrimes * Distributed under the terms of the MIT License.
41590Srgrimes */
51590Srgrimes
61590Srgrimes
71590Srgrimes#include "PackageWritingUtils.h"
81590Srgrimes
91590Srgrimes#include <dirent.h>
101590Srgrimes#include <errno.h>
111590Srgrimes#include <stdio.h>
121590Srgrimes#include <string.h>
131590Srgrimes
141590Srgrimes#include <package/hpkg/HPKGDefs.h>
151590Srgrimes
161590Srgrimes#include <AutoDeleter.h>
171590Srgrimes
181590Srgrimes
191590Srgrimesstatus_t
201590Srgrimesadd_current_directory_entries(BPackageWriter& packageWriter,
211590Srgrimes	BPackageWriterListener& listener, bool skipPackageInfo)
221590Srgrimes{
231590Srgrimes	// open the current directory
241590Srgrimes	DIR* dir = opendir(".");
251590Srgrimes	if (dir == NULL) {
261590Srgrimes		listener.PrintError("Error: Failed to opendir '.': %s\n",
271590Srgrimes			strerror(errno));
281590Srgrimes		return errno;
291590Srgrimes	}
301590Srgrimes	CObjectDeleter<DIR, int> dirCloser(dir, &closedir);
311590Srgrimes
321590Srgrimes	while (dirent* entry = readdir(dir)) {
331590Srgrimes		// skip "." and ".."
341590Srgrimes		if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
3528149Scharnier			continue;
361590Srgrimes
3728149Scharnier		// skip the .PackageInfo, if requested
3828149Scharnier		if (skipPackageInfo
3939230Sgibbs			&& strcmp(entry->d_name, B_HPKG_PACKAGE_INFO_FILE_NAME) == 0) {
401590Srgrimes			continue;
411590Srgrimes		}
421590Srgrimes
431590Srgrimes		status_t error = packageWriter.AddEntry(entry->d_name);
441590Srgrimes		if (error != B_OK)
451590Srgrimes			return error;
461590Srgrimes	}
471590Srgrimes
481590Srgrimes	return B_OK;
491590Srgrimes}
501590Srgrimes