d7f2bea28c0c41ad55290d3e7c0f451ced60b364
[platform/core/appfw/debug-launchpad.git] / src / file_util.c
1 /*
2  * Copyright (c) 2015 - 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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <limits.h>
23 #include <fcntl.h>
24
25 #include "file_util.h"
26
27 static int recurse(const char *path, mode_t mode,
28                 int (*fn)(const char *, mode_t, int))
29 {
30         struct stat st;
31         char dir[PATH_MAX];
32
33         if (path == NULL)
34                 return -1;
35
36         if (lstat(path, &st) == -1)
37                 return -1;
38
39         if (strrchr(path, '/') != NULL) {
40                 int n = strlen(path)-strlen(strrchr(path, '/'));
41                 if (n >= PATH_MAX)
42                         return -1;
43
44                 strncpy(dir, path, n);
45                 dir[n] = '\0';
46                 fn(dir, mode, 1);
47                 return 0;
48         }
49
50         return -1;
51 }
52
53 int dlp_chmod(const char *path, mode_t mode, int recursive)
54 {
55         int fd;
56         struct stat lstat_info;
57         struct stat fstat_info;
58 #ifdef HAVE_WIN32_PROC
59         fprintf(stderr, "error: dlp_chmod not implemented on Win32 (%s)\n",
60                         path);
61         return -1;
62 #else
63
64         if (lstat(path, &lstat_info) == -1)
65                 return -1;
66
67         fd = open(path, O_WRONLY, S_IRWXU);
68         if (fd == -1)
69                 return -1;
70
71         if (fstat(fd, &fstat_info) == -1) {
72                 close(fd);
73                 return -1;
74         }
75
76         /* this complex check is required because of 'chmod' security issue. */
77         /* otherwise hacker can change other file's permission by using race condition and symbolic link. */
78         if (lstat_info.st_mode == fstat_info.st_mode
79                         && lstat_info.st_ino == fstat_info.st_ino
80                         && lstat_info.st_dev == fstat_info.st_dev) {
81                 if (fchmod(fd, mode) == -1) {
82                         close(fd);
83                         return -1;
84                 }
85         }
86
87         close(fd);
88
89         if (recursive)
90                 return recurse(path, mode, dlp_chmod);
91
92         return 0;
93 #endif
94 }
95