summaryrefslogtreecommitdiff
path: root/src/util/file-util.c
blob: c1e51329599cdce1d99bb6b31187d2948fe7fda2 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* 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 <app_debug.h>

#include "util/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 *mf_file_get(const char path[])
{
	char *file = NULL;
	if ((file = strrchr(path, '/')))
		file++;
	else
		file = (char *) path;

	return file;
}

char *mf_dir_get(const char path[])
{
	char *p = NULL;
	char buf[PATH_MAX + 1] = {0,};

	strncpy(buf, path, PATH_MAX);
	p = strrchr(buf, '/');
	if (!p)
		return strdup(path);

	if (p == buf)
		return strdup("/");

	*p = 0;
	return strdup(buf);
}

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

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

Eina_Bool mf_is_dir(const char *path)
{
	if (!path)
		return 0;

	struct stat info = {0,};

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

	return 0;
}

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

	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 mf_mkdir(const char *dir)
{
	if (mkdir(dir, default_mode) < 0)
		return 0;
	else
		return 1;
}

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

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

	if (mf_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 (!mf_mkpath_if_not_exists(ss))
				return 0;
		}
	}
	ss[i] = '\0';

	return mf_mkpath_if_not_exists(ss);
}

char *mf_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 mf_file_unlink(const char *filename)
{
	int status = unlink(filename);
	if (status < 0)
		return 0;
	else
		return 1;
}

int mf_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 mf_file_rmdir(const char *filename)
{
	int status = rmdir(filename);
	if (status < 0)
		return 0;
	else
		return 1;
}

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

	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 mf_file_recursive_rm(const char *dir)
{
	char buf[PATH_MAX_SIZE] = {0,};
	struct dirent ent_struct;
	struct dirent *dp = NULL;
	DIR *dirp = NULL;

	if (readlink(dir, buf, sizeof(buf)) > 0)
		return mf_file_unlink(dir);

	int ret = mf_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 (!mf_file_recursive_rm(dp->d_name))
						ret = 0;
				}
			}
			closedir(dirp);
		}

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

		return ret;
	} else
		return mf_file_unlink(dir);
}

int mf_file_cp(const char *src, const char *dst)
{
	FILE *f1 = NULL;
	FILE *f2 = NULL;
	char buf[BUF_MAX] = {0,};
	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 mf_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)) {
				mf_file_cp(src, dst);
				if (chmod(dst, info.st_mode) < 0)
					_DBG("failed to set attributes");
				if (unlink(src) < 0)
					_DBG("failed to delete source");
				return 1;
			}
		}
		return 0;
	}
	return 1;
}

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

void mf_util_free_eina_list_with_data(Eina_List **list, MYFILE_CONTENT_TYPE type)
{
	if (list == NULL || *list == NULL) {
		return;
	}

	void *pNode = NULL;
	Eina_List *l = NULL;

	switch (type) {
/*	case MYFILE_TYPE_GSTRING:
		EINA_LIST_FOREACH(*list, l, pNode) {
			GString *node = (GString *)pNode;
			SAFE_FREE_GSTRING(node);
		}
		break;*/
	case MYFILE_TYPE_CHAR:
		EINA_LIST_FOREACH(*list, l, pNode) {
			char *node = (char *)pNode;
			SAFE_FREE_CHAR(node);
		}
		break;
	case MYFILE_TYPE_FSNODE:
		EINA_LIST_FOREACH(*list, l, pNode) {
			fsNodeInfo *Node = (fsNodeInfo *)pNode;
			if (Node != NULL) {
				SAFE_FREE_CHAR(Node->path);
				SAFE_FREE_CHAR(Node->name);
				SAFE_FREE_CHAR(Node->ext);

				free(Node);
				Node = NULL;
			}
		}
		break;
/*	case MYFILE_TYPE_ITEM_DATA:
		EINA_LIST_FOREACH(*list, l, pNode) {
			mfItemData_s *Node = (mfItemData_s *)pNode;
			mf_util_normal_item_data_free(&Node);
		}
		break;*/
	default:
		break;
	}

	eina_list_free(*list);
	*list = NULL;

	return;
}

void util_free_item_data_s(item_data_s *id)
{
	if (id == NULL)
		return;

	SAFE_FREE_CHAR(id->selected_folder_path);
	SAFE_FREE_CHAR(id->parent_path);
	SAFE_FREE_CHAR(id->name);
	SAFE_FREE_CHAR(id->file_size);

	return;
}

void util_free_path_item_data_s(path_item_data_s *pid)
{
	if (pid == NULL)
		return;

	SAFE_FREE_CHAR(pid->full_path);
	SAFE_FREE_CHAR(pid->parent_path);

	return;
}