Exemple #1
0
// create_app
static
entry_ref
create_app(const char *filename, const char *signature,
		   bool install = false, bool makeExecutable = true,
		   uint32 appFlags = B_SINGLE_LAUNCH)
{
	BString testApp;
	CHK(find_test_app("RosterBroadcastTestApp1", &testApp) == B_OK);
	system((string("cp ") + testApp.String() + " " + filename).c_str());
	if (makeExecutable)
		system((string("chmod a+x ") + filename).c_str());
	BFile file;
	CHK(file.SetTo(filename, B_READ_WRITE) == B_OK);
	BAppFileInfo appFileInfo;
	CHK(appFileInfo.SetTo(&file) == B_OK);
	if (signature)
		CHK(appFileInfo.SetSignature(signature) == B_OK);
	CHK(appFileInfo.SetAppFlags(appFlags) == B_OK);
	if (install && signature)
		CHK(BMimeType(signature).Install() == B_OK);
	// We write the signature into a separate attribute, just in case we
	// decide to also test files without BEOS:APP_SIG attribute.
	BString signatureString(signature);
	file.WriteAttrString("signature", &signatureString);
	return ref_for_path(filename);
}
Exemple #2
0
// main
int
main(int argc, const char *const *argv)
{
	kArgc = argc;
	kArgv = argv;

	// parameters
	const char **files = new const char*[argc];
	int fileCount = 0;
	const char *type = NULL;
	const char *signature = NULL;
	const char *preferredApp = NULL;
	
	// parse the arguments
	for (int argi = 1; argi < argc; ) {
		const char *arg = argv[argi++];
		if (arg[0] == '-') {
			if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
				print_usage_and_exit(false);

			} else if (strcmp(arg, "-preferredAppSig") == 0) {
				preferredApp = next_arg(argi);

			} else if (strcmp(arg, "-s") == 0) {
				signature = next_arg(argi);

			} else if (strcmp(arg, "-t") == 0) {
				type = next_arg(argi);

			} else {
				fprintf(stderr, "Error: Invalid option: \"%s\"\n", arg);
				print_usage_and_exit(true);
			}
		} else {
			// file
			files[fileCount++] = arg;
		}
	}

	// check parameters
	if (!preferredApp && !signature && !type) {
		fprintf(stderr, "Error: At least one option of \"-preferredAppSig\", "
			"\"-s\", and \"-t\" must be given.\n");
		print_usage_and_exit(true);
	}

	if (fileCount == 0) {
		fprintf(stderr, "Error: No file specified.\n");
		print_usage_and_exit(true);
	}	

	// check for valid MIME types
	check_mime_type(preferredApp);
	check_mime_type(type);
	check_mime_type(signature);

	// iterate through the files
	for (int i = 0; i < fileCount; i++) {
		const char *fileName = files[i];

		// check, whether the file exists
		BEntry entry;
		status_t error = entry.SetTo(fileName, false);
		if (error != B_OK) {
			fprintf(stderr, "Error: Can't access file \"%s\": %s\n",
				fileName, strerror(error));

			exit(1);
		}

		if (!entry.Exists()) {
			fprintf(stderr, "Error: \"%s\": No such file or directory.\n",
				fileName);

			exit(1);
		}

		// ... and has the right type
		if (signature && !entry.IsFile()) {
			fprintf(stderr, "Error: \"%s\" is not a file. Signatures can only "
				"be set for executable files.\n", fileName);

			exit(1);
		}

		// open the file
		BFile file;
		BNode _node;
		BNode &node = (signature ? file : _node);
		error = (signature ? file.SetTo(fileName, B_READ_WRITE)
			: node.SetTo(fileName));
		if (error != B_OK) {
			fprintf(stderr, "Error: Failed to open file \"%s\": %s\n",
				fileName, strerror(error));

			exit(1);
		}

		// prepare an node/app info object
		BAppFileInfo appInfo;
		BNodeInfo _nodeInfo;
		BNodeInfo &nodeInfo = (signature ? appInfo : _nodeInfo);
		error = (signature ? appInfo.SetTo(&file) : nodeInfo.SetTo(&node));
		if (error != B_OK) {
			fprintf(stderr, "Error: Failed to open file \"%s\": %s\n",
				fileName, strerror(error));

			exit(1);
		}

		// set preferred app
		if (preferredApp) {
			error = nodeInfo.SetPreferredApp(preferredApp);
			if (error != B_OK) {
				fprintf(stderr, "Error: Failed to set the preferred "
					"application of file \"%s\": %s\n", fileName,
					strerror(error));

				exit(1);
			}
		}

		// set type
		if (type) {
			error = nodeInfo.SetType(type);
			if (error != B_OK) {
				fprintf(stderr, "Error: Failed to set the MIME type of file "
					"\"%s\": %s\n", fileName, strerror(error));

				exit(1);
			}
		}

		// set signature
		if (signature) {
			error = appInfo.SetSignature(signature);
			if (error != B_OK) {
				fprintf(stderr, "Error: Failed to set the signature of file "
					"\"%s\": %s\n", fileName, strerror(error));

				exit(1);
			}
		}
	}

	return 0;
}