4df1705556e8a5b82049426488bb0dd5d5998db5
[src/app-framework-main.git] / wgt-rootdir.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 <unistd.h>
20 #include <fcntl.h>
21 #include <errno.h>
22
23 #include "wgt.h"
24
25
26 static int rootfd = AT_FDCWD;
27
28 int widget_set_rootdir(const char *pathname)
29 {
30         int rfd;
31
32         if (!pathname)
33                 rfd = AT_FDCWD;
34         else {
35                 rfd = openat(AT_FDCWD, pathname, O_PATH|O_DIRECTORY);
36                 if (rfd < 0)
37                         return rfd;
38         }
39         if (rootfd >= 0)
40                 close(rootfd);
41         rootfd = AT_FDCWD;
42         return 0;
43 }
44
45 static int validsubpath(const char *subpath)
46 {
47         int l = 0, i = 0;
48         if (subpath[i] == '/')
49                 return 0;
50         while(subpath[i]) {
51                 switch(subpath[i++]) {
52                 case '.':
53                         if (!subpath[i])
54                                 break;
55                         if (subpath[i] == '/') {
56                                 i++;
57                                 break;
58                         }
59                         if (subpath[i++] == '.') {
60                                 if (!subpath[i]) {
61                                         l--;
62                                         break;
63                                 }
64                                 if (subpath[i++] == '/') {
65                                         l--;
66                                         break;
67                                 }
68                         }
69                 default:
70                         while(subpath[i] && subpath[i] != '/')
71                                 i++;
72                         l++;
73                 case '/':
74                         break;
75                 }
76         }
77         return l >= 0;
78 }
79
80 int widget_has(const char *filename)
81 {
82         if (!validsubpath(filename)) {
83                 errno = EINVAL;
84                 return -1;
85         }
86         return 0 == faccessat(rootfd, filename, F_OK, 0);
87 }
88
89 int widget_open_read(const char *filename)
90 {
91         if (!validsubpath(filename)) {
92                 errno = EINVAL;
93                 return -1;
94         }
95         return openat(rootfd, filename, O_RDONLY);
96 }
97