From 4e6141b823043abc3e48acb48e45e43b8fa680fe Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 13 Jul 2013 16:25:15 +0200 Subject: [PATCH] package list: add option '-i' to list only the meta info --- src/bin/package/command_list.cpp | 59 +++++++++++++++++++++++--------- src/bin/package/package.cpp | 1 + 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/bin/package/command_list.cpp b/src/bin/package/command_list.cpp index fc84b074f6..1862316e6a 100644 --- a/src/bin/package/command_list.cpp +++ b/src/bin/package/command_list.cpp @@ -78,18 +78,29 @@ struct VersionPolicyV2 { }; +enum ListMode { + LIST_ALL, + LIST_PATHS_ONLY, + LIST_META_INFO_ONLY +}; + + template struct PackageContentListHandler : VersionPolicy::PackageContentHandler { - PackageContentListHandler(bool listAttributes) + PackageContentListHandler(bool listEntries, bool listAttributes) : fPrinter(), fLevel(0), - fListAttribute(listAttributes) + fListEntries(listEntries), + fListAttribute(listEntries && listAttributes) { } virtual status_t HandleEntry(typename VersionPolicy::PackageEntry* entry) { + if (!fListEntries) + return B_OK; + fLevel++; int indentation = (fLevel - 1) * 2; @@ -162,6 +173,9 @@ struct PackageContentListHandler : VersionPolicy::PackageContentHandler { virtual status_t HandleEntryDone( typename VersionPolicy::PackageEntry* entry) { + if (!fListEntries) + return B_OK; + fLevel--; return B_OK; } @@ -208,6 +222,7 @@ private: private: PackageInfoPrinter fPrinter; int fLevel; + bool fListEntries; bool fListAttribute; }; @@ -258,7 +273,7 @@ private: template static void -do_list(const char* packageFileName, bool listAttributes, bool filePathsOnly, +do_list(const char* packageFileName, bool listAttributes, ListMode listMode, bool ignoreVersionError) { // open package @@ -272,13 +287,23 @@ do_list(const char* packageFileName, bool listAttributes, bool filePathsOnly, } // list - if (filePathsOnly) { - PackageContentListPathsHandler handler; - error = packageReader.ParseContent(&handler); - } else { - PackageContentListHandler handler(listAttributes); - error = packageReader.ParseContent(&handler); + switch (listMode) { + case LIST_PATHS_ONLY: + { + PackageContentListPathsHandler handler; + error = packageReader.ParseContent(&handler); + break; + } + + case LIST_ALL: + case LIST_META_INFO_ONLY: + { + PackageContentListHandler handler( + listMode != LIST_META_INFO_ONLY, listAttributes); + error = packageReader.ParseContent(&handler); + } } + if (error != B_OK) exit(1); @@ -289,8 +314,8 @@ do_list(const char* packageFileName, bool listAttributes, bool filePathsOnly, int command_list(int argc, const char* const* argv) { + ListMode listMode = LIST_ALL; bool listAttributes = false; - bool filePathsOnly = false; while (true) { static struct option sLongOptions[] = { @@ -299,7 +324,7 @@ command_list(int argc, const char* const* argv) }; opterr = 0; // don't print errors - int c = getopt_long(argc, (char**)argv, "+hap", sLongOptions, NULL); + int c = getopt_long(argc, (char**)argv, "+ahip", sLongOptions, NULL); if (c == -1) break; @@ -308,12 +333,16 @@ command_list(int argc, const char* const* argv) listAttributes = true; break; + case 'i': + listMode = LIST_META_INFO_ONLY; + break; + case 'h': print_usage_and_exit(false); break; case 'p': - filePathsOnly = true; + listMode = LIST_PATHS_ONLY; break; default: @@ -331,10 +360,8 @@ command_list(int argc, const char* const* argv) BHPKG::BStandardErrorOutput errorOutput; // current package file format version - do_list(packageFileName, listAttributes, filePathsOnly, - true); - do_list(packageFileName, listAttributes, filePathsOnly, - false); + do_list(packageFileName, listAttributes, listMode, true); + do_list(packageFileName, listAttributes, listMode, false); return 0; } diff --git a/src/bin/package/package.cpp b/src/bin/package/package.cpp index 240517b3a7..180169c6b3 100644 --- a/src/bin/package/package.cpp +++ b/src/bin/package/package.cpp @@ -80,6 +80,7 @@ static const char* kUsage = " Lists the contents of package file .\n" "\n" " -a - Also list the file attributes.\n" + " -i - Only print the meta information, not the files.\n" " -p - Only print a list of file paths.\n" "\n" "Common Options:\n"