2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
4 * Distributed under the terms of the MIT License.
18 #include <package/PackageInfo.h>
19 #include <package/hpkg/HPKGDefs.h>
20 #include <package/hpkg/PackageWriter.h>
23 #include "PackageWriterListener.h"
24 #include "PackageWritingUtils.h"
27 using namespace BPackageKit::BHPKG
;
31 command_add(int argc
, const char* const* argv
)
33 const char* changeToDirectory
= NULL
;
34 const char* packageInfoFileName
= NULL
;
38 int32 compressionLevel
= BPackageKit::BHPKG::B_HPKG_COMPRESSION_LEVEL_BEST
;
41 static struct option sLongOptions
[] = {
42 { "help", no_argument
, 0, 'h' },
43 { "quiet", no_argument
, 0, 'q' },
44 { "verbose", no_argument
, 0, 'v' },
48 opterr
= 0; // don't print errors
49 int c
= getopt_long(argc
, (char**)argv
, "+0123456789C:fhi:qv",
65 compressionLevel
= c
- '0';
69 changeToDirectory
= optarg
;
73 print_usage_and_exit(false);
81 packageInfoFileName
= optarg
;
93 print_usage_and_exit(true);
98 // The remaining arguments are the package file and the entries to add.
100 print_usage_and_exit(true);
102 const char* packageFileName
= argv
[optind
++];
104 // entries must be specified, if a .PackageInfo hasn't been specified via
106 if (optind
>= argc
&& packageInfoFileName
== NULL
)
107 print_usage_and_exit(true);
109 const char* const* entriesToAdd
= argv
+ optind
;
110 int entriesToAddCount
= argc
- optind
;
113 BPackageWriterParameters writerParameters
;
114 writerParameters
.SetFlags(
115 B_HPKG_WRITER_UPDATE_PACKAGE
| (force
? B_HPKG_WRITER_FORCE_ADD
: 0));
116 writerParameters
.SetCompressionLevel(compressionLevel
);
117 if (compressionLevel
== 0) {
118 writerParameters
.SetCompression(
119 BPackageKit::BHPKG::B_HPKG_COMPRESSION_NONE
);
122 PackageWriterListener
listener(verbose
, quiet
);
123 BPackageWriter
packageWriter(&listener
);
124 status_t result
= packageWriter
.Init(packageFileName
, &writerParameters
);
128 // If a package info file has been specified explicitly, open it.
129 int packageInfoFD
= -1;
130 if (packageInfoFileName
!= NULL
) {
131 packageInfoFD
= open(packageInfoFileName
, O_RDONLY
);
132 if (packageInfoFD
< 0) {
133 fprintf(stderr
, "Error: Failed to open package info file \"%s\": "
134 "%s\n", packageInfoFileName
, strerror(errno
));
138 // change directory, if requested
139 if (changeToDirectory
!= NULL
) {
140 if (chdir(changeToDirectory
) != 0) {
142 "Error: Failed to change the current working directory to "
143 "\"%s\": %s\n", changeToDirectory
, strerror(errno
));
148 for (int i
= 0; i
< entriesToAddCount
; i
++) {
149 const char* entry
= entriesToAdd
[i
];
151 if (strcmp(entry
, ".") == 0) {
152 // add all entries in the current directory; skip .PackageInfo,
153 // if a different file was specified
154 if (add_current_directory_entries(packageWriter
,
155 listener
, packageInfoFileName
!= NULL
) != B_OK
)
158 // skip .PackageInfo, if a different file was specified
159 if (packageInfoFileName
!= NULL
160 && strcmp(entry
, B_HPKG_PACKAGE_INFO_FILE_NAME
) == 0) {
164 if (packageWriter
.AddEntry(entry
) != B_OK
)
169 // add the .PackageInfo, if explicitly specified
170 if (packageInfoFileName
!= NULL
) {
171 result
= packageWriter
.AddEntry(B_HPKG_PACKAGE_INFO_FILE_NAME
,
178 result
= packageWriter
.Finish();
183 printf("\nsuccessfully created package '%s'\n", packageFileName
);