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))
36 if (lstat(path, &st) == -1)
39 if (strrchr(path, '/') != NULL) {
40 int n = strlen(path)-strlen(strrchr(path, '/'));
44 strncpy(dir, path, n);
53 int dlp_chmod(const char *path, mode_t mode, int recursive)
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",
64 if (lstat(path, &lstat_info) == -1)
67 fd = open(path, O_WRONLY, S_IRWXU);
71 if (fstat(fd, &fstat_info) == -1) {
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) {
90 return recurse(path, mode, dlp_chmod);