2 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "file_util.h"
27 static int recurse(const char *path, mode_t mode,
28 int (*fn)(const char *, mode_t, int))
37 if (lstat(path, &st) == -1)
40 if (strrchr(path, '/') != NULL) {
41 n = strlen(path) - strlen(strrchr(path, '/'));
45 strncpy(dir, path, n);
54 int dlp_chmod(const char *path, mode_t mode, int recursive)
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",
65 if (lstat(path, &lstat_info) == -1)
68 fd = open(path, O_WRONLY, S_IRWXU);
72 if (fstat(fd, &fstat_info) == -1) {
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) {
91 return recurse(path, mode, dlp_chmod);