summaryrefslogtreecommitdiff
path: root/common/src/vp-file-util.c
blob: 2d13d155a92c7cf9b6da1dded02e9179f3b20f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2000-2015 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "vp-file-util.h"

#define PATH_MAX_SIZE 256
#define BUF_MAX 16384
static mode_t default_mode =
	S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;

const char *vp_file_get(const char path[])
{
	char *file = NULL;
	struct stat info = { 0, };
	if (stat(path, &info) == 0) {
		if ((file = strrchr(path, '/')))
			file++;
	} else {
		file = (char *) path;
	}
	return file;
}

char *vp_dir_get(const char path[])
{
	char *p = NULL;
	char buf[PATH_MAX] = { 0, };
	struct stat info = { 0, };

	if (stat(path, &info) == 0) {
		strncpy(buf, path, PATH_MAX);
		buf[PATH_MAX - 1] = 0;
		p = dirname(buf);
		return strdup(p);
	} else {
		return "";
	}
}

int vp_file_exists(const char *path)
{
	struct stat info = { 0, };

	if (stat(path, &info) == 0)
		return 1;
	else
		return 0;
}

Eina_Bool vp_is_dir(const char *path)
{
	struct stat info = { 0, };

	if (stat(path, &info) == 0) {
		if (S_ISDIR(info.st_mode)) {
			return 1;
		}
	}

	return 0;
}

int vp_is_dir_empty(const char *path)
{
	struct stat info = { 0, };
	struct dirent *dp = NULL;
	DIR *dirp = NULL;
	struct dirent ent_struct;

	dirp = opendir(path);
	if (!dirp)
		return -1;

	while ((readdir_r(dirp, &ent_struct, &dp) == 0) && dp) {
		if (stat(dp->d_name, &info) == 0 && (strcmp(dp->d_name, "."))
				&& (strcmp(dp->d_name, ".."))) {
			closedir(dirp);
			return 0;
		}
	}
	closedir(dirp);
	return 1;
}

int vp_mkdir(const char *dir)
{
	if (mkdir(dir, default_mode) < 0)
		return 0;
	else
		return 1;
}

static int vp_mkpath_if_not_exists(const char *path)
{
	struct stat st = { 0, };
	if (stat(path, &st) < 0)
		return vp_mkdir(path);
	else if (!S_ISDIR(st.st_mode))
		return 0;
	else
		return 1;
}

int vp_mkpath(const char *path)
{
	char ss[PATH_MAX] = { 0, };
	unsigned int i = 0;

	if (vp_is_dir(path))
		return 1;

	for (i = 0; path[i] != '\0'; ss[i] = path[i], i++) {
		if (i == sizeof(ss) - 1)
			return 0;

		if ((path[i] == '/') && (i > 0)) {
			ss[i] = '\0';
			if (!vp_mkpath_if_not_exists(ss))
				return 0;
		}
	}
	ss[i] = '\0';

	return vp_mkpath_if_not_exists(ss);
}

char *vp_strip_ext(const char *path)
{
	char *p = NULL;
	char *file = NULL;

	p = strrchr(path, '.');
	if (!p) {
		file = strdup(path);
	} else if (p != path) {
		file = malloc(((p - path) + 1) * sizeof(char));
		if (file) {
			memcpy(file, path, (p - path));
			file[p - path] = 0;
		}
	}

	return file;
}

int vp_file_unlink(const char *filename)
{
	int status = unlink(filename);
	if (status < 0)
		return 0;
	else
		return 1;
}

int vp_file_size(const char *filename)
{
	struct stat info = { 0, };
	if (stat(filename, &info) == 0) {
		if (!S_ISDIR(info.st_mode))
			return info.st_size;
	}

	return 0;
}

int vp_file_rmdir(const char *filename)
{
	int status = rmdir(filename);
	if (status < 0)
		return 0;
	else
		return 1;
}

Eina_List *vp_file_ls(const char *dir)
{
	char *f;
	DIR *dirp = NULL;
	struct dirent *dp = NULL;
	Eina_List *list = NULL;
	struct dirent ent_struct;

	dirp = opendir(dir);
	if (!dirp)
		return NULL;

	while ((readdir_r(dirp, &ent_struct, &dp) == 0) && dp) {
		if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, ".."))) {
			f = strdup(dp->d_name);
			list = eina_list_append(list, f);
		}
	}
	closedir(dirp);

	list =
		eina_list_sort(list, eina_list_count(list),
			       EINA_COMPARE_CB(strcoll));

	return list;
}

int vp_file_recursive_rm(const char *dir)
{
	char buf[PATH_MAX_SIZE] = { 0, };
	struct dirent *dp = NULL;
	DIR *dirp = NULL;
	struct dirent ent_struct;

	if (readlink(dir, buf, sizeof(buf)) > 0) {
		return vp_file_unlink(dir);
	}

	int ret = vp_is_dir(dir);
	if (ret) {
		ret = 1;
		dirp = opendir(dir);
		if (dirp) {
			while ((readdir_r(dirp, &ent_struct, &dp) == 0) && dp) {
				if ((strcmp(dp->d_name, "."))
						&& (strcmp(dp->d_name, ".."))) {
					if (!vp_file_recursive_rm(dp->d_name))
						ret = 0;
				}
			}
			closedir(dirp);
		}

		if (!vp_file_rmdir(dir))
			ret = 0;

		return ret;
	} else {
		return vp_file_unlink(dir);
	}
}

int vp_file_cp(const char *src, const char *dst)
{
	FILE *f1 = NULL;
	FILE *f2 = NULL;
	char buf[BUF_MAX] = { 0, };	//TODO: How about moving buf to heap instead of stack
	char realpath1[PATH_MAX_SIZE] = { 0, };
	char realpath2[PATH_MAX_SIZE] = { 0, };
	size_t num;
	int ret = 1;

	if (!realpath(src, realpath1))
		return 0;

	if (realpath(dst, realpath2) && !strcmp(realpath1, realpath2))
		return 0;

	f1 = fopen(src, "rb");
	if (!f1)
		return 0;

	f2 = fopen(dst, "wb");
	if (!f2) {
		fclose(f1);
		return 0;
	}

	while ((num = fread(buf, 1, sizeof(buf), f1)) > 0) {
		if (fwrite(buf, 1, num, f2) != num)
			ret = 0;
	}

	fclose(f1);
	fclose(f2);

	return ret;
}

int vp_file_mv(const char *src, const char *dst)
{
	struct stat info = { 0, };
	if (stat(dst, &info) == 0)
		return 0;

	if (rename(src, dst)) {
		memset(&info, 0x00, sizeof(struct stat));
		if (stat(src, &info) == 0) {
			if (S_ISREG(info.st_mode)) {
				vp_file_cp(src, dst);
				chmod(dst, info.st_mode);
				unlink(src);
				return 1;
			}
		}
		return 0;
	}
	return 1;
}