1 /*-*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <sys/types.h>
34 struct file_info
*mk_file_get_info(char *path
)
36 struct file_info
*f_info
;
37 struct stat f
, target
;
39 /* Stat right resource */
40 if (lstat(path
, &f
) == -1) {
44 f_info
= mk_mem_malloc(sizeof(struct file_info
));
45 f_info
->is_link
= MK_FILE_FALSE
;
46 f_info
->is_directory
= MK_FILE_FALSE
;
47 f_info
->exec_access
= MK_FILE_FALSE
;
48 f_info
->read_access
= MK_FILE_FALSE
;
50 if (S_ISLNK(f
.st_mode
)) {
51 f_info
->is_link
= MK_FILE_TRUE
;
52 if (stat(path
, &target
) == -1) {
60 f_info
->size
= target
.st_size
;
61 f_info
->last_modification
= target
.st_mtime
;
63 if (S_ISDIR(target
.st_mode
)) {
64 f_info
->is_directory
= MK_FILE_TRUE
;
67 /* Checking read access */
68 if (((target
.st_mode
& S_IRUSR
) && target
.st_uid
== EUID
) ||
69 ((target
.st_mode
& S_IRGRP
) && target
.st_gid
== EGID
) ||
70 (target
.st_mode
& S_IROTH
)) {
71 f_info
->read_access
= MK_FILE_TRUE
;
75 MK_TRACE("Target has not read acess");
79 /* Checking execution access */
80 if ((target
.st_mode
& S_IXUSR
&& target
.st_uid
== EUID
) ||
81 (target
.st_mode
& S_IXGRP
&& target
.st_gid
== EGID
) ||
82 (target
.st_mode
& S_IXOTH
)) {
83 f_info
->exec_access
= MK_FILE_TRUE
;
88 MK_TRACE("Target has not execution permission");
95 /* Read file content to a memory buffer,
96 * Use this function just for really SMALL files
98 char *mk_file_to_buffer(char *path
)
103 struct file_info
*finfo
;
105 if (!(finfo
= mk_file_get_info(path
))) {
109 if (!(fp
= fopen(path
, "r"))) {
113 buffer
= calloc(finfo
->size
+ 1, sizeof(char));
119 bytes
= fread(buffer
, finfo
->size
, 1, fp
);
128 return (char *) buffer
;