Adjust coding rules
[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         int n;
33
34         if (path == NULL)
35                 return -1;
36
37         if (lstat(path, &st) == -1)
38                 return -1;
39
40         if (strrchr(path, '/') != NULL) {
41                 n = strlen(path) - strlen(strrchr(path, '/'));
42                 if (n >= PATH_MAX)
43                         return -1;
44
45                 strncpy(dir, path, n);
46                 dir[n] = '\0';
47                 fn(dir, mode, 1);
48                 return 0;
49         }
50
51         return -1;
52 }
53
54 int dlp_chmod(const char *path, mode_t mode, int recursive)
55 {
56         int fd;
57         struct stat lstat_info;
58         struct stat fstat_info;
59 #ifdef HAVE_WIN32_PROC
60         fprintf(stderr, "error: dlp_chmod not implemented on Win32 (%s)\n",
61                         path);
62         return -1;
63 #else
64
65         if (lstat(path, &lstat_info) == -1)
66                 return -1;
67
68         fd = open(path, O_WRONLY, S_IRWXU);
69         if (fd == -1)
70                 return -1;
71
72         if (fstat(fd, &fstat_info) == -1) {
73                 close(fd);
74                 return -1;
75         }
76
77         /* this complex check is required because of 'chmod' security issue. */
78         /* otherwise hacker can change other file's permission by using race condition and symbolic link. */
79         if (lstat_info.st_mode == fstat_info.st_mode
80                         && lstat_info.st_ino == fstat_info.st_ino
81                         && lstat_info.st_dev == fstat_info.st_dev) {
82                 if (fchmod(fd, mode) == -1) {
83                         close(fd);
84                         return -1;
85                 }
86         }
87
88         close(fd);
89
90         if (recursive)
91                 return recurse(path, mode, dlp_chmod);
92
93         return 0;
94 #endif
95 }
96