1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 /*
7  * Copyright (C) 2018-2022 Intel Corporation.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef __FSUTILS_H__
23 #define __FSUTILS_H__
24 
25 #include <sys/stat.h>
26 #include <dirent.h>
27 #include <stdio.h>
28 #include <errno.h>
29 
30 #define MAX(a, b)                (((a) > (b)) ? (a) : (b))
31 #define MIN(a, b)                (((a) > (b)) ? (b) : (a))
32 
33 #define KB                      (1024)
34 #define MB                      (KB * KB)
35 #define MAXLINESIZE             (PATH_MAX + 128)
36 #define CPBUFFERSIZE            (4 * KB)
37 #define PAGE_SIZE               (4 * KB)
38 
39 struct mm_file_t {
40 	char *path;
41 	int fd;
42 	char *begin;
43 	ssize_t size;
44 };
45 
46 struct ac_filter_data {
47 	const char *str;
48 	size_t len;
49 };
50 
file_exists(const char * filename)51 static inline int file_exists(const char *filename)
52 {
53 	struct stat info;
54 
55 	return (stat(filename, &info) == 0);
56 }
57 
directory_exists(const char * path)58 static inline int directory_exists(const char *path)
59 {
60 	struct stat info;
61 
62 	return (stat(path, &info) == 0 && S_ISDIR(info.st_mode));
63 }
64 
get_file_size(const char * filepath)65 static inline ssize_t get_file_size(const char *filepath)
66 {
67 	struct stat info;
68 
69 	if (filepath == NULL)
70 		return -ENOENT;
71 
72 	if (stat(filepath, &info) < 0)
73 		return -errno;
74 
75 	return info.st_size;
76 }
77 
get_file_blocks_size(const char * filepath)78 static inline ssize_t get_file_blocks_size(const char *filepath)
79 {
80 	struct stat info;
81 
82 	if (filepath == NULL)
83 		return -ENOENT;
84 
85 	if (stat(filepath, &info) < 0)
86 		return -errno;
87 
88 	return info.st_blocks * 512;
89 }
90 
91 char *mm_get_line(struct mm_file_t *mfile, int line);
92 int mkdir_p(const char *path);
93 int remove_r(const char *dir);
94 int mm_count_lines(struct mm_file_t *mfile);
95 struct mm_file_t *mmap_file(const char *path);
96 void unmap_file(struct mm_file_t *mfile);
97 int do_copy_tail(const char *src, const char *dest, int limit);
98 int do_mv(char *src, char *dest);
99 ssize_t append_file(const char *filename, const char *text, size_t tlen);
100 int replace_file_head(char *filename, char *text);
101 int overwrite_file(const char *filename, const char *value);
102 int readline(int fd, char buffer[MAXLINESIZE]);
103 ssize_t file_read_string(const char *file, char *string, const int size);
104 void file_reset_init(const char *filename);
105 int file_read_int(const char *filename, unsigned int *pcurrent);
106 int file_update_int(const char *filename, unsigned int current,
107 			unsigned int max);
108 int do_copy_limit(const char *src, const char *des, size_t limitsize);
109 int space_available(const char *path, int quota);
110 int count_lines_in_file(const char *filename);
111 int read_full_binary_file(const char *path, unsigned long *size,
112 			void **data);
113 ssize_t file_read_key_value(char *value, const size_t limit, const char *path,
114 			const char *key, size_t klen);
115 ssize_t file_read_key_value_r(char *value, const size_t limit, const char *path,
116 			const char *key, size_t klen);
117 int ac_scandir(const char *dirp, struct dirent ***namelist,
118 		int (*filter)(const struct dirent *, const void *),
119 		const void *farg,
120 		int (*compar)(const struct dirent **,
121 				const struct dirent **));
122 int filter_filename_substr(const struct dirent *entry, const void *arg);
123 int filter_filename_exactly(const struct dirent *entry, const void *arg);
124 int filter_filename_startswith(const struct dirent *entry,
125 					const void *arg);
126 int dir_contains(const char *dir, const char *filename, size_t flen, int exact);
127 int lsdir(const char *dir, char *fullname[], int limit);
128 int find_file(const char *dir, size_t dlen, const char *target_file,
129 		size_t tflen, int depth, char *path[], int limit);
130 int dir_blocks_size(const char *dir, size_t dlen, size_t *size);
131 int read_file(const char *path, unsigned long *size, void **data);
132 int is_ac_filefmt(const char *file_fmt);
133 int config_fmt_to_files(const char *file_fmt, char ***out);
134 
135 #endif
136