Changed API to check internet privilege
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / nitool.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "utils.h"
18 #include "ni_common.h"
19
20 #include <cstdio>
21 #include <cstring>
22
23 #include <algorithm>
24 #include <vector>
25
26 std::vector<std::string> getCmdArgs(char** begin, char** end)
27 {
28         std::vector<std::string> list;
29         for (char** itr = begin+1; itr != end; itr++) {
30                 if (strncmp(*itr, "--", 2) != 0)
31                         list.push_back(*itr);
32         }
33         return list;
34 }
35
36 static bool cmdOptionExists(char** begin, char** end, const std::string& option)
37 {
38         return std::find(begin, end, option) != end;
39 }
40
41 static void help(const char *argv0)
42 {
43         const char* helpDesc =
44                 "%s is deprecated. Please use dotnettool instead.\n"
45                 "Example:\n"
46                 "   # dotnettool --help\n\n";
47         printf(helpDesc, argv0);
48 }
49
50 int main(int argc, char* argv[])
51 {
52         bool pkgMode = false;
53         bool dllMode = false;
54         bool rmPkgMode = false;
55
56         if (initNICommon() != NI_ERROR_NONE) {
57                 return -1;
58         }
59
60         NIOption* opt = getNIOption();
61         if (opt == nullptr) {
62                 _SERR("Fail to create option structure.");
63                 return -1;
64         }
65
66         // Parse optional switches first.
67         if (cmdOptionExists(argv, argv+argc, "--verbose")) {
68                 opt->flags |= NI_FLAGS_VERBOSE;
69         }
70
71         // The following commands are mutually exclusive.
72         if (cmdOptionExists(argv, argv+argc, "--help")) {
73                 help(argv[0]);
74                 return 0;
75         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
76                 createNIPlatform("", opt->flags);
77                 return 0;
78         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
79                 dllMode = true;
80         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
81                 pkgMode = true;
82         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
83                 removeNIPlatform();
84                 return 0;
85         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
86                 rmPkgMode = true;
87         } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
88                 regenerateAppNI(opt->flags);
89                 return 0;
90         } else {
91                 help(argv[0]);
92                 return 0;
93         }
94
95         std::vector<std::string> args = getCmdArgs(argv, argv+argc);
96
97         if (args.size() < 1) {
98                 if (pkgMode)
99                         _SERR("Package name is missing.");
100                 else if (dllMode)
101                         _SERR("DLL path is missing.");
102                 help(argv[0]);
103                 return -1;
104         }
105
106         if (pkgMode) {
107                 for (const std::string pkg : args) {
108                         // if there is AOTed dlls under package root, that is skiped.
109                         int ret = createNIUnderPkgRoot(pkg, opt->flags);
110                         if (ret == NI_ERROR_INVALID_PACKAGE) {
111                                 _SERR("Failed to get root path from [%s]", pkg.c_str());
112                                 return -1;
113                         } else if (ret != NI_ERROR_NONE) {
114                                 _SERR("Failed to generate NI file [%s]", pkg.c_str());
115                                 return -1;
116                         }
117                 }
118         } else if (rmPkgMode) {
119                 for (const std::string pkg : args) {
120                         int ret = removeNIUnderPkgRoot(pkg);
121                         if (ret == NI_ERROR_INVALID_PACKAGE) {
122                                 _SERR("Failed to get root path from [%s]", pkg.c_str());
123                                 return -1;
124                         } else if (ret != NI_ERROR_NONE) {
125                                 _SERR("Failed to remove dlls for given package [%s]", pkg.c_str());
126                                 return -1;
127                         }
128                 }
129         } else if (dllMode) {
130                 // donot return error code for generation failure.
131                 // we have to run crossgen for all input dlls.
132                 for (const std::string dll : args) {
133                         int ret = createNIDll(dll, opt->flags);
134                         if (ret == NI_ERROR_ALREADY_EXIST) {
135                                 // skip for already exist case
136                         } else if (ret != NI_ERROR_NONE) {
137                                 _SERR("Failed to generate NI file [%s]", dll.c_str());
138                         }
139                 }
140         }
141
142         return 0;
143 }