Move directory iteration helper to own file
This commit is contained in:
parent
1bd38c4318
commit
35f8c67c17
@ -9,6 +9,7 @@ BinCommand package :
|
||||
command_list.cpp
|
||||
package.cpp
|
||||
PackageWriterListener.cpp
|
||||
PackageWritingUtils.cpp
|
||||
StandardErrorOutput.cpp
|
||||
|
||||
:
|
||||
|
49
src/bin/package/PackageWritingUtils.cpp
Normal file
49
src/bin/package/PackageWritingUtils.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "PackageWritingUtils.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <package/hpkg/HPKGDefs.h>
|
||||
|
||||
#include <AutoDeleter.h>
|
||||
|
||||
|
||||
status_t
|
||||
add_current_directory_entries(BPackageWriter& packageWriter,
|
||||
BPackageWriterListener& listener, bool skipPackageInfo)
|
||||
{
|
||||
// open the current directory
|
||||
DIR* dir = opendir(".");
|
||||
if (dir == NULL) {
|
||||
listener.PrintError("Error: Failed to opendir '.': %s\n",
|
||||
strerror(errno));
|
||||
return errno;
|
||||
}
|
||||
CObjectDeleter<DIR, int> dirCloser(dir, &closedir);
|
||||
|
||||
while (dirent* entry = readdir(dir)) {
|
||||
// skip "." and ".."
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
// skip the .PackageInfo, if requested
|
||||
if (skipPackageInfo
|
||||
&& strcmp(entry->d_name, B_HPKG_PACKAGE_INFO_FILE_NAME) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
status_t error = packageWriter.AddEntry(entry->d_name);
|
||||
if (error != B_OK)
|
||||
return error;
|
||||
}
|
||||
|
||||
return B_OK;
|
||||
}
|
22
src/bin/package/PackageWritingUtils.h
Normal file
22
src/bin/package/PackageWritingUtils.h
Normal file
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef PACKAGE_WRITING_UTILS_H
|
||||
#define PACKAGE_WRITING_UTILS_H
|
||||
|
||||
|
||||
#include <SupportDefs.h>
|
||||
|
||||
#include <package/hpkg/PackageWriter.h>
|
||||
|
||||
|
||||
using BPackageKit::BHPKG::BPackageWriter;
|
||||
using BPackageKit::BHPKG::BPackageWriterListener;
|
||||
|
||||
|
||||
status_t add_current_directory_entries(BPackageWriter& packageWriter,
|
||||
BPackageWriterListener& listener, bool skipPackageInfo);
|
||||
|
||||
|
||||
#endif // PACKAGE_WRITING_UTILS_H
|
@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
@ -22,6 +21,7 @@
|
||||
|
||||
#include "package.h"
|
||||
#include "PackageWriterListener.h"
|
||||
#include "PackageWritingUtils.h"
|
||||
#include "StandardErrorOutput.h"
|
||||
|
||||
|
||||
@ -109,29 +109,9 @@ command_create(int argc, const char* const* argv)
|
||||
}
|
||||
}
|
||||
|
||||
// add all files of current directory
|
||||
DIR* dir = opendir(".");
|
||||
if (dir == NULL) {
|
||||
listener.PrintError("Error: Failed to opendir '.': %s\n",
|
||||
strerror(errno));
|
||||
// add all files of the current directory, save for the .PackageInfo
|
||||
if (add_current_directory_entries(packageWriter, listener, true) != B_OK)
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (dirent* entry = readdir(dir)) {
|
||||
// skip "." and ".."
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
// also skip the .PackageInfo -- we'll add it later
|
||||
if (strcmp(entry->d_name, B_HPKG_PACKAGE_INFO_FILE_NAME) == 0)
|
||||
continue;
|
||||
|
||||
result = packageWriter.AddEntry(entry->d_name);
|
||||
if (result != B_OK)
|
||||
return 1;
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
// add the .PackageInfo
|
||||
result = packageWriter.AddEntry(B_HPKG_PACKAGE_INFO_FILE_NAME,
|
||||
|
@ -13,6 +13,7 @@ BuildPlatformMain <build>package :
|
||||
command_list.cpp
|
||||
package.cpp
|
||||
PackageWriterListener.cpp
|
||||
PackageWritingUtils.cpp
|
||||
StandardErrorOutput.cpp
|
||||
|
||||
:
|
||||
|
Loading…
Reference in New Issue
Block a user