summaryrefslogtreecommitdiff
path: root/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc
blob: 91204f52dbf555a710ec02366ee6284b02a5f1fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ni_common.h"
#include "log.h"
#include "utils.h"

#include <cstring>
#include <vector>
#include <sstream>
#include <glib.h>
#include <pkgmgr_installer_info.h>

#ifdef  LOG_TAG
#undef  LOG_TAG
#endif
#define LOG_TAG "DOTNET_INSTALLER_PLUGIN"

typedef struct Metadata {
	const char *key;
	const char *value;
} Metadata;

extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
{
	GList *tag = NULL;
	bool mdValue = false;
	Metadata *mdInfo = NULL;
	tag = g_list_first(list);
	while (tag) {
		mdInfo = (Metadata*)tag->data;
		if (strcmp(mdInfo->key, AOT_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
			_DBG("Prefer dotnet application AOT set TRUE");
			mdValue = true;
		}
		tag = g_list_next(tag);
	}

	if (mdValue) {
		NiCommonOption option = {std::string(), std::string(), std::string()};
		if (initNICommon(&option) < 0) {
			_ERR("Fail to initialize NI Common");
			return -1;
		}

		if (createNiUnderPkgRoot(pkgId, false) != NI_ERROR_NONE) {
			_ERR("Failed to get root path from [%s]", pkgId);
			return -1;
		} else {
			_INFO("Complete make application to native image");
		}

		std::string pkgRoot;
		if (getRootPath(pkgId, pkgRoot) < 0) {
			_ERR("Failed to get root path from [%s]", pkgId);
			return 0;
		}

		std::string binDir = concatPath(pkgRoot, "bin");
		std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
		if (bf::exists(tacDir)) {
			uid_t uid = 0;
			if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
				_ERR("Failed to get UID");
				return 0;
			}
			try {
				for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
					std::string symPath = symlinkAssembly.path().string();
					if (!isNativeImage(symPath)) {
						std::string originPath = bf::read_symlink(symPath).string();
						std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
						if (!bf::exists(originNiPath)) {
							if(createNiDll(originPath, false) != NI_ERROR_NONE) {
								_ERR("Failed to create NI file [%s]", originPath.c_str());
								return -1;
							}
						}
						std::string setNiPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
						if (!bf::exists(setNiPath)) {
							bf::create_symlink(originNiPath, setNiPath);
							_INFO("%s symbolic link file generated successfully.", setNiPath.c_str());
							if (lchown(setNiPath.c_str(), uid, 0)) {
								_ERR("Failed to change owner of: %s", setNiPath.c_str());
								return -1;
							}
						}
					}
				}
			} catch (const bf::filesystem_error& error) {
				_ERR("Failed to recursive directory: %s", error.what());
				return -1;
			}
		}
	}
	return 0;
}

extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
{
	return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgId, appId, list);
}

extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
{
	return 0;
}

extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
{
	return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
}

extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
{
	return 0;
}

extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
{
	return 0;
}