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 BPackageKit::BHPKG::BPackageWriter
;
28 using BPackageKit::BHPKG::BPackageWriterListener
;
29 using BPackageKit::BHPKG::BPackageWriterParameters
;
33 command_create(int argc
, const char* const* argv
)
35 const char* changeToDirectory
= NULL
;
36 const char* packageInfoFileName
= NULL
;
37 const char* installPath
= NULL
;
38 bool isBuildPackage
= false;
41 int32 compressionLevel
= BPackageKit::BHPKG::B_HPKG_COMPRESSION_LEVEL_BEST
;
44 static struct option sLongOptions
[] = {
45 { "help", no_argument
, 0, 'h' },
46 { "quiet", no_argument
, 0, 'q' },
47 { "verbose", no_argument
, 0, 'v' },
51 opterr
= 0; // don't print errors
52 int c
= getopt_long(argc
, (char**)argv
, "+b0123456789C:hi:I:qv",
68 compressionLevel
= c
- '0';
72 isBuildPackage
= true;
76 changeToDirectory
= optarg
;
80 print_usage_and_exit(false);
84 packageInfoFileName
= optarg
;
100 print_usage_and_exit(true);
105 // The remaining arguments is the package file, i.e. one more argument.
106 if (optind
+ 1 != argc
)
107 print_usage_and_exit(true);
109 const char* packageFileName
= argv
[optind
++];
111 // -I is only allowed when -b is given
112 if (installPath
!= NULL
&& !isBuildPackage
) {
113 fprintf(stderr
, "Error: \"-I\" is only allowed when \"-b\" is "
119 BPackageWriterParameters writerParameters
;
120 writerParameters
.SetCompressionLevel(compressionLevel
);
121 if (compressionLevel
== 0) {
122 writerParameters
.SetCompression(
123 BPackageKit::BHPKG::B_HPKG_COMPRESSION_NONE
);
126 PackageWriterListener
listener(verbose
, quiet
);
127 BPackageWriter
packageWriter(&listener
);
128 status_t result
= packageWriter
.Init(packageFileName
, &writerParameters
);
132 // If a package info file has been specified explicitly, open it.
133 int packageInfoFD
= -1;
134 if (packageInfoFileName
!= NULL
) {
135 packageInfoFD
= open(packageInfoFileName
, O_RDONLY
);
136 if (packageInfoFD
< 0) {
137 fprintf(stderr
, "Error: Failed to open package info file \"%s\": "
138 "%s\n", packageInfoFileName
, strerror(errno
));
143 // change directory, if requested
144 if (changeToDirectory
!= NULL
) {
145 if (chdir(changeToDirectory
) != 0) {
147 "Error: Failed to change the current working directory to "
148 "\"%s\": %s\n", changeToDirectory
, strerror(errno
));
154 packageWriter
.SetCheckLicenses(false);
156 // set install path, if specified
157 if (installPath
!= NULL
) {
158 result
= packageWriter
.SetInstallPath(installPath
);
159 if (result
!= B_OK
) {
160 fprintf(stderr
, "Error: Failed to set the package install path: "
161 "%s\n", strerror(result
));
166 // add all files of the current directory, save for the .PackageInfo
167 if (!isBuildPackage
) {
168 if (add_current_directory_entries(packageWriter
, listener
, true)
174 // add the .PackageInfo
175 result
= packageWriter
.AddEntry(
176 BPackageKit::BHPKG::B_HPKG_PACKAGE_INFO_FILE_NAME
, packageInfoFD
);
181 result
= packageWriter
.Finish();
186 printf("\nsuccessfully created package '%s'\n", packageFileName
);