don't change of directory anymore
[src/app-framework-main.git] / src / wgtpkg-install.c
1 /*
2  Copyright 2015 IoT.bzh
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 #define _GNU_SOURCE
18
19 #include <errno.h>
20 #include <syslog.h>
21 #include <string.h>
22
23 #include "verbose.h"
24 #include "wgtpkg.h"
25 #include "wgt.h"
26 #include "wgt-info.h"
27
28 static int check_temporary_constraints(const struct wgt_desc *desc)
29 {
30         if (!desc->icons) {
31                 syslog(LOG_ERR, "widget has not icon defined (temporary constraints)");
32                 errno = EINVAL;
33                 return -1;
34         }
35         if (desc->icons->next) {
36                 syslog(LOG_ERR, "widget has more than one icon defined (temporary constraints)");
37                 errno = EINVAL;
38                 return -1;
39         }
40         if (!desc->content_src) {
41                 syslog(LOG_ERR, "widget has not content defined (temporary constraints)");
42                 errno = EINVAL;
43                 return -1;
44         }
45         if (!desc->content_type) {
46                 syslog(LOG_ERR, "widget has not type for its content (temporary constraints)");
47                 errno = EINVAL;
48                 return -1;
49         }
50         return 0;
51 }
52
53 static int check_permissions(const char *name, int required)
54 {
55         if (permission_exists(name)) {
56                 if (request_permission(name)) {
57                         debug("granted permission: %s", name);
58                 } else if (required) {
59                         syslog(LOG_ERR, "ungranted permission required: %s", name);
60                         errno = EPERM;
61                         return 0;
62                 } else {
63                         notice("ungranted permission optional: %s", name);
64                 }
65         }
66         return 1;
67 }
68
69 static int check_widget(const struct wgt_desc *desc)
70 {
71         int result;
72         const struct wgt_desc_feature *feature;
73         const char *name;
74
75         result = check_temporary_constraints(desc);
76         feature = desc->features;
77         while(feature) {
78                 name = feature->name;
79                 if (0 == strcmp(name, AGLWIDGET)) {
80                         
81                 } else {
82                         if (!check_permissions(feature->name, feature->required))
83                                 result = -1;
84                 }
85                 feature = feature->next;
86         }
87         return result;
88 }
89
90 static int place(const char *root, const char *appid, const char *version, int force)
91 {
92         char newdir[PATH_MAX];
93         int rc;
94
95         rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, appid, version);
96         if (rc >= sizeof newdir) {
97                 syslog(LOG_ERR, "path to long: %s/%s/%s", root, appid, version);
98                 errno = EINVAL;
99                 return -1;
100         }
101
102         rc = move_workdir(newdir, 1, force);
103         return rc;
104 }
105
106 /* install the widget of the file */
107 void install_widget(const char *wgtfile, const char *root, int force)
108 {
109         struct wgt_info *ifo;
110         const struct wgt_desc *desc;
111
112         notice("-- INSTALLING widget %s --", wgtfile);
113
114         /* workdir */
115         if (make_workdir_base(root, "UNPACK", 0)) {
116                 syslog(LOG_ERR, "failed to create a working directory");
117                 goto error1;
118         }
119
120         if (zread(wgtfile, 0))
121                 goto error2;
122
123         if (check_all_signatures())
124                 goto error2;
125
126         ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
127         if (!ifo)
128                 goto error2;
129
130         desc = wgt_info_desc(ifo);
131         if (check_widget(desc))
132                 goto error3;
133
134 /*
135         if (check_and_place())
136                 goto error2;
137 */      
138         return;
139
140 error3:
141         wgt_info_unref(ifo);
142
143 error2:
144         remove_workdir();
145
146 error1:
147         return;
148 }
149