3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 #include <sys/vfs.h> /* for statfs */
26 #include "fsapi/operation.h"
27 #include "fsapi/operation_internal.h"
28 #include "fsapi/util.h"
29 #include "security-assistant/md5.h"
31 #include "utility/sync_util.h"
33 #include <tzplatform_config.h>
36 #define EXPORT_API __attribute__ ((visibility("default")))
39 #ifndef SYNC_AGENT_LOG
41 #define LOG_TAG "AF_FS"
44 #define FS_BUFFER_SIZE 2048
46 static bool file_append_started = FALSE;
47 static bool file_read_started = FALSE;
48 static char *temp_filename_kept = NULL;
49 static char *requested_filename_kept = NULL;
50 static FILE *fd_source_kept = NULL;
52 static unsigned char _sync_str_to_hex_char(unsigned char hex);
54 EXPORT_API sync_agent_fsapi_return_e sync_agent_is_existing_fs(const char *file_name)
58 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
62 return fs_is_existing(file_name);
65 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_fs_info(char **result)
69 retvm_if(result == NULL, 0, "result paramter is NULL !!!");
73 long double free_size = 0;
74 long double total_size = 0;
76 int file_name_len_max = 256;
77 int folder_name_len_max = 256;
79 char *file_name_prevenet_char = ":;*?/<>\"";
80 char *folder_name_prevent_char = ":;*?/<>\"";
82 int file_name_code_schema = 2;
83 int allow_dup_name = 0;
84 int folder_depth_max = 15;
86 ret = sync_agent_get_fs_mem_size(tzplatform_getenv(TZ_USER_CONTENT), &free_size, &total_size);
88 _DEBUG_ERROR("[sync_agent_get_fs_info] getting mem size fail");
89 return SYNC_AGENT_FSAPI_FALSE;
92 *result = g_strdup_printf("%.0Lf,%d,%s,%d,%d,%d,%d,%s", total_size, file_name_len_max, file_name_prevenet_char, file_name_code_schema, allow_dup_name, folder_depth_max, folder_name_len_max, folder_name_prevent_char);
96 return SYNC_AGENT_FSAPI_TRUE;
99 EXPORT_API sync_agent_fsapi_return_e sync_agent_free_fs_info(char *result)
103 retvm_if(result == NULL, 0, "result paramter is NULL !!!");
110 return SYNC_AGENT_FSAPI_TRUE;
113 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_fs_mem_size(const char *root, long double *size_free, long double *size_total)
117 retvm_if(root == NULL, 0, "root paramter is NULL !!!");
119 if (!fs_is_existing(root)) {
120 _DEBUG_ERROR("[%s] Requested instance '%s' not exist.\n", __func__, root);
121 return SYNC_AGENT_FSAPI_FALSE;
124 struct statfs fs_info;
126 if (statfs(root, &fs_info) != 0) {
127 _DEBUG_ERROR("[sync_agent_get_fs_mem_size] statfs error.");
128 return SYNC_AGENT_FSAPI_FALSE;
131 *size_free = ((long double)fs_info.f_bavail * (long double)fs_info.f_bsize); /* / 1024; */
132 *size_total = ((long double)fs_info.f_blocks * (long double)fs_info.f_bsize); /* / 1024; */
134 _DEBUG_INFO("[sync_agent_get_fs_mem_size] FS info root : %s.", root);
135 _DEBUG_INFO("[sync_agent_get_fs_mem_size] FS info free size : %.0f", *size_free);
136 _DEBUG_INFO("[sync_agent_get_fs_mem_size] FS info total size : %.0f", *size_total);
140 return SYNC_AGENT_FSAPI_TRUE;
147 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_file_size(const char *file_name, unsigned long *size)
151 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
153 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
154 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
155 return SYNC_AGENT_FSAPI_FALSE;
160 int ret = stat(file_name, &buf);
162 _DEBUG_ERROR("[%s] stat error : %s", __func__, strerror(errno));
163 return SYNC_AGENT_FSAPI_FALSE;
166 *size = (unsigned long)buf.st_size;
170 return SYNC_AGENT_FSAPI_TRUE;
173 int fs_get_file_date(const char *file_name, char **time_string)
177 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
179 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
180 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
181 return SYNC_AGENT_FSAPI_FALSE;
185 char time[FS_MAX_TIME_STRING_BUF] = { 0 };
187 int ret = stat(file_name, &buf);
189 _DEBUG_ERROR("[%s] stat error : %s", __func__, strerror(errno));
190 return SYNC_AGENT_FSAPI_FALSE;
193 fs_get_time_string(&buf.st_mtime, time);
194 *time_string = g_strdup(time);
198 return SYNC_AGENT_FSAPI_TRUE;
201 EXPORT_API sync_agent_fsapi_return_e sync_agent_copy_file(const char *source, const char *dest)
205 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
206 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
208 if (!fs_is_existing(source) || fs_is_directory(source) || fs_is_existing(dest)) {
209 _DEBUG_ERROR("[%s] Requested source instance '%s' not exist or not a file, or\n", __func__, source);
210 _DEBUG_ERROR("[%s] requested dest instance '%s' already exist.\n", __func__, dest);
211 return SYNC_AGENT_FSAPI_FALSE;
214 FILE *f1 = NULL, *f2 = NULL;
216 char buf[1024 * 100] = { 0 };
217 unsigned long bytes_read = 0;
218 unsigned long bytes_written = 0;
220 f1 = (FILE *) g_fopen(source, "rb");
222 _DEBUG_ERROR("[sync_agent_copy_file] file f1 open error.");
227 f2 = (FILE *) g_fopen(dest, "wb");
229 _DEBUG_ERROR("[sync_agent_copy_file] file f2 open error.");
234 while ((bytes_read = fread(buf, 1, sizeof(buf), f1)) > 0) {
235 if (bytes_read > sizeof(buf)) {
236 _DEBUG_ERROR("[sync_agent_copy_file] read error.");
241 bytes_written = fwrite(buf, 1, bytes_read, f2);
242 if (!feof(f1) && bytes_written != bytes_read) {
243 _DEBUG_ERROR("[sync_agent_copy_file] write error.");
262 if( !File_Read(source, &body, &size) ) {
263 fprintf(stderr, "[%s] File_Read() returned error.\n", __func__);
264 return SYNC_AGENT_FSAPI_FALSE;
267 if( !File_Write(dest, body, size) ) {
268 fprintf(stderr, "[%s] File_Read() returned error.\n", __func__);
269 return SYNC_AGENT_FSAPI_FALSE;
274 return SYNC_AGENT_FSAPI_TRUE;
278 EXPORT_API sync_agent_fsapi_return_e sync_agent_delete_file(const char *file_name)
282 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
284 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
285 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
286 return SYNC_AGENT_FSAPI_FALSE;
289 int ret = g_unlink(file_name);
291 _DEBUG_ERROR("[%s] g_unlink error : %s\n", __func__, strerror(errno));
292 return SYNC_AGENT_FSAPI_FALSE;
297 return SYNC_AGENT_FSAPI_TRUE;
300 EXPORT_API sync_agent_fsapi_return_e sync_agent_move_file(const char *source, const char *dest)
304 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
305 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
307 if (!fs_is_existing(source) || fs_is_directory(source) || fs_is_existing(dest)) {
308 _DEBUG_ERROR("[%s] Requested source instance '%s' not exist or not a file, or\n", __func__, source);
309 _DEBUG_ERROR("[%s] requested dest instance '%s' already exist.\n", __func__, dest);
310 return SYNC_AGENT_FSAPI_FALSE;
313 int ret = g_rename(source, dest);
315 _DEBUG_ERROR("[sync_agent_move_file] g_rename error : %s", strerror(errno));
316 _DEBUG_ERROR("[sync_agent_move_file] source : %s target : %s", source, dest);
317 return SYNC_AGENT_FSAPI_FALSE;
322 return SYNC_AGENT_FSAPI_TRUE;
325 EXPORT_API sync_agent_fsapi_return_e sync_agent_rename_file(const char *source, const char *dest)
329 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
330 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
334 return sync_agent_move_file(source, dest);
337 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_file_list(const char *folder_name, GSList ** list)
341 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
343 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
344 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
345 return SYNC_AGENT_FSAPI_FALSE;
350 char time[FS_MAX_TIME_STRING_BUF] = { 0 };
351 char *list_item = NULL;
353 GError *gerror = NULL;
354 GDir *dir = g_dir_open(folder_name, 0, &gerror);
356 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
357 g_error_free(gerror);
358 return SYNC_AGENT_FSAPI_FALSE;
361 const char *file_name = g_dir_read_name(dir);
363 for (; file_name; file_name = g_dir_read_name(dir)) {
365 path = g_strdup_printf("%s/%s", folder_name, file_name);
367 if (!fs_is_directory(path)) {
368 int ret = stat(path, &buf);
370 _DEBUG_ERROR("[%s] g_stat error : %s\n", __func__, strerror(errno));
371 return SYNC_AGENT_FSAPI_FALSE;
374 fs_get_time_string(&buf.st_mtime, time);
375 list_item = g_strdup_printf("%s,%ld,%s", file_name, (long)buf.st_size, time);
377 *list = g_slist_append(*list, list_item);
387 return SYNC_AGENT_FSAPI_TRUE;
390 EXPORT_API sync_agent_fsapi_return_e sync_agent_read_file(const char *file_name, char **required_contents, unsigned long required_length, unsigned long *size_return, bool * is_final)
394 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
396 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
397 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
398 return SYNC_AGENT_FSAPI_FALSE;
403 if ((fd_source_kept == NULL) && !file_read_started) {
404 fd_source_kept = (FILE *) g_fopen(file_name, "rb");
405 if (fd_source_kept == NULL) {
406 _DEBUG_ERROR("[sync_agent_read_file] temp_filename_kept open error.");
410 file_read_started = TRUE;
411 } else if ((fd_source_kept != NULL) && file_read_started) {
413 _DEBUG_ERROR("[%s] internal condition not matching.\n", __func__);
418 *required_contents = (char *)calloc(required_length, sizeof(char));
419 if (*required_contents == NULL) {
420 _DEBUG_ERROR("[sync_agent_read_file] calloc error. [*required_contents]");
426 bytes_read = fread(*required_contents, 1, required_length, fd_source_kept);
428 if ((bytes_read > required_length) || ((!feof(fd_source_kept)) && (bytes_read < required_length))) {
429 _DEBUG_ERROR("[sync_agent_read_file] read error.");
434 *size_return = bytes_read;
436 if (feof(fd_source_kept)) {
447 if (fd_source_kept != NULL) {
448 fclose(fd_source_kept);
449 fd_source_kept = NULL;
451 file_read_started = FALSE;
456 if( !fs_is_existing(file_name) || fs_is_directory(file_name) ) {
457 fprintf(stderr, "[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
458 return SYNC_AGENT_FSAPI_FALSE;
461 GError *gerror = NULL;
463 if( !g_file_get_contents(file_name, contents, (gsize*)length, &gerror) ) {
464 fprintf(stderr, "[%s] g_file_get_contents error : %s\n", __func__, gerror->message);
466 g_error_free(gerror);
467 return SYNC_AGENT_FSAPI_FALSE;
470 return SYNC_AGENT_FSAPI_TRUE;
474 EXPORT_API sync_agent_fsapi_return_e sync_agent_read_whole_file(const char *file_name, char **required_contents, unsigned long *size_return)
478 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
480 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
481 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
482 return SYNC_AGENT_FSAPI_FALSE;
485 GError *gerror = NULL;
487 if (!g_file_get_contents(file_name, required_contents, (gsize *) size_return, &gerror)) {
488 _DEBUG_ERROR("[%s] g_file_get_contents error : %s\n", __func__, gerror->message);
490 g_error_free(gerror);
491 return SYNC_AGENT_FSAPI_FALSE;
496 return SYNC_AGENT_FSAPI_TRUE;
499 EXPORT_API sync_agent_fsapi_return_e sync_agent_free_file(char **requested_contents)
503 retvm_if(requested_contents == NULL, 0, "requested_contents paramter is NULL !!!");
505 if (*requested_contents != NULL)
506 free(*requested_contents);
510 return SYNC_AGENT_FSAPI_TRUE;
513 EXPORT_API sync_agent_fsapi_return_e sync_agent_write_file(const char *file_name, const char *contents, unsigned long length, bool is_final, bool overwrite)
517 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
518 retvm_if(contents == NULL, 0, "contents is NULL !!");
521 if (fs_is_existing(file_name)) {
522 _DEBUG_ERROR("[%s] Requested instance '%s' already exist.\n", __func__, file_name);
523 return SYNC_AGENT_FSAPI_FALSE;
527 FILE *fd_temp = NULL;
528 GError *gerror = NULL;
530 unsigned long temp_size = 0;
532 if ((temp_filename_kept == NULL) && !file_append_started) {
533 if (!g_file_open_tmp(NULL, &temp_filename_kept, &gerror)) {
534 _DEBUG_ERROR("[%s] g_file_open_tmp error : %s\n", __func__, gerror->message);
536 g_error_free(gerror);
540 _DEBUG_INFO("[sync_agent_write_file] Temp file name kept new : %s", temp_filename_kept);
542 file_append_started = TRUE;
543 requested_filename_kept = g_strdup(file_name);
545 } else if ((temp_filename_kept != NULL) && file_append_started) {
546 if (!fs_is_existing(temp_filename_kept)) {
547 _DEBUG_ERROR("[%s] Temp file '%s' not existing.\n", __func__, temp_filename_kept);
551 _DEBUG_INFO("[sync_agent_write_file] Temp file %s kept previously, existing", temp_filename_kept);
554 _DEBUG_ERROR("[%s] internal condition not matching.\n", __func__);
559 if (temp_filename_kept != NULL) {
560 fd_temp = (FILE *) g_fopen(temp_filename_kept, "ab");
565 if (fd_temp == NULL) {
566 _DEBUG_ERROR("[sync_agent_write_file] temp file open error.");
571 if (fwrite(contents, 1, length, fd_temp) != length) {
572 _DEBUG_ERROR("[sync_agent_write_file] fwrite error.");
577 if (fd_temp != NULL) {
582 sync_agent_get_file_size(temp_filename_kept, &temp_size);
583 _DEBUG_INFO("[sync_agent_write_file] temp file size %ld", temp_size);
587 if (fs_is_existing(file_name)) {
588 if (!sync_agent_delete_file(file_name)) {
589 _DEBUG_ERROR("[sync_agent_write_file] deleting destination file for overwriting fail.");
592 if (!sync_agent_copy_file(temp_filename_kept, file_name)) {
593 _DEBUG_ERROR("[sync_agent_write_file] copying temp file to destination fail.");
596 if (!sync_agent_delete_file(temp_filename_kept)) {
597 _DEBUG_ERROR("[sync_agent_write_file] deleting temp file fail.");
603 if (!sync_agent_copy_file(temp_filename_kept, file_name)) {
604 _DEBUG_ERROR("[sync_agent_write_file] copying temp file to destination fail.");
607 if (!sync_agent_delete_file(temp_filename_kept)) {
608 _DEBUG_ERROR("[sync_agent_write_file] deleting temp file fail.");
614 if (!sync_agent_copy_file(temp_filename_kept, file_name)) {
615 _DEBUG_ERROR("[sync_agent_write_file] copying temp file to destination fail.");
618 if (!sync_agent_delete_file(temp_filename_kept)) {
619 _DEBUG_ERROR("[sync_agent_write_file] deleting temp file fail.");
635 g_free(temp_filename_kept);
636 g_free(requested_filename_kept);
637 temp_filename_kept = NULL;
638 requested_filename_kept = NULL;
639 file_append_started = FALSE;
644 if( fs_is_existing(file_name) ) {
645 fprintf(stderr, "[%s] Requested instance '%s' already exist.", __func__, file_name);
646 return SYNC_AGENT_FSAPI_FALSE;
649 GError *gerror = NULL;
651 if( !g_file_set_contents(file_name, contents, length, &gerror) ) {
652 fprintf(stderr, "[%s] g_file_set_contents error : %s\n", __func__, gerror->message);
654 g_error_free(gerror);
655 return SYNC_AGENT_FSAPI_FALSE;
658 return SYNC_AGENT_FSAPI_TRUE;
662 EXPORT_API sync_agent_fsapi_return_e sync_agent_write_whole_file(const char *file_name, const char *contents, unsigned long length, bool overwrite)
666 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
667 retvm_if(contents == NULL, 0, "contents is NULL !!");
669 if (fs_is_existing(file_name)) {
670 if (overwrite && !fs_is_directory(file_name)) {
671 int result = sync_agent_delete_file(file_name);
673 _DEBUG_ERROR("[%s] sync_agent_delete_file() FAIL !!!.", __func__, file_name);
674 return SYNC_AGENT_FSAPI_FALSE;
677 _DEBUG_ERROR("[%s] Requested instance '%s' already exist.", __func__, file_name);
678 return SYNC_AGENT_FSAPI_FALSE;
682 GError *gerror = NULL;
684 if (!g_file_set_contents(file_name, contents, length, &gerror)) {
685 _DEBUG_ERROR("[%s] g_file_set_contents error : %s\n", __func__, gerror->message);
687 g_error_free(gerror);
688 return SYNC_AGENT_FSAPI_FALSE;
693 return SYNC_AGENT_FSAPI_TRUE;
696 EXPORT_API sync_agent_fsapi_return_e sync_agent_append_file(const char *file_name, const char *contents, unsigned long length, bool overwrite)
700 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
701 retvm_if(contents == NULL, 0, "contents is NULL !!");
704 if (!sync_agent_delete_file(file_name)) {
705 if (!fs_is_existing(file_name)) {
706 _DEBUG_ERROR("[%s] The file %s requested to delete for overwriting is not existing, so just continue.", __func__, file_name);
708 if (fs_is_directory(file_name)) {
709 _DEBUG_ERROR("[%s] Can't overwrite because requested instance '%s' not a file.", __func__, file_name);
710 return SYNC_AGENT_FSAPI_FALSE;
712 _DEBUG_ERROR("[%s] FAILED to delete requested file '%s'.", __func__, file_name);
713 return SYNC_AGENT_FSAPI_FALSE;
722 fd = (FILE *) g_fopen(file_name, "ab");
724 _DEBUG_ERROR("[sync_agent_append_file] file open error.");
729 if (fwrite(contents, 1, length, fd) != length) {
730 _DEBUG_ERROR("[%s] fwrite error '%s'.", __func__, file_name);
735 if (fsync(fileno(fd)) != 0)
745 _DEBUG_INFO("[%s] Returning..", __func__);
753 EXPORT_API sync_agent_fsapi_return_e sync_agent_create_directory(const char *folder_name)
757 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
759 if (fs_is_existing(folder_name)) {
760 _DEBUG_ERROR("[%s] Requested instance '%s' already exist.\n", __func__, folder_name);
761 return SYNC_AGENT_FSAPI_FALSE;
764 int ret = g_mkdir_with_parents(folder_name, 0755);
766 _DEBUG_ERROR("[%s] g_mkdir error : %s\n", __func__, strerror(errno));
767 return SYNC_AGENT_FSAPI_FALSE;
772 return SYNC_AGENT_FSAPI_TRUE;
775 EXPORT_API sync_agent_fsapi_return_e sync_agent_delete_directory(const char *folder_name, bool recursive)
779 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
781 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
782 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
783 return SYNC_AGENT_FSAPI_FALSE;
786 GError *gerror = NULL;
793 dir = g_dir_open(folder_name, 0, &gerror);
795 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
796 g_error_free(gerror);
797 return SYNC_AGENT_FSAPI_FALSE;
800 name = (char *)g_dir_read_name(dir);
802 for (; name; name = (char *)g_dir_read_name(dir)) {
804 path = g_strdup_printf("%s/%s", folder_name, name);
806 if (fs_is_directory(path)) {
807 if (!sync_agent_delete_directory(path, true))
808 return SYNC_AGENT_FSAPI_FALSE;
810 if (!sync_agent_delete_file(path))
811 return SYNC_AGENT_FSAPI_FALSE;
821 ret = g_rmdir(folder_name);
823 _DEBUG_ERROR("[%s] g_rmdir error : %s\n", __func__, strerror(errno));
824 return SYNC_AGENT_FSAPI_FALSE;
829 return SYNC_AGENT_FSAPI_TRUE;
832 EXPORT_API sync_agent_fsapi_return_e sync_agent_empty_directory_contents(const char *folder_name)
836 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
838 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
839 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
840 return SYNC_AGENT_FSAPI_FALSE;
843 GError *gerror = NULL;
848 dir = g_dir_open(folder_name, 0, &gerror);
850 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
851 g_error_free(gerror);
852 return SYNC_AGENT_FSAPI_FALSE;
855 name = (char *)g_dir_read_name(dir);
857 for (; name; name = (char *)g_dir_read_name(dir)) {
859 path = g_strdup_printf("%s/%s", folder_name, name);
861 if (fs_is_directory(path)) {
862 if (!sync_agent_delete_directory(path, true)) {
863 _DEBUG_ERROR("[%s] sync_agent_delete_directory error : %s\n", __func__, strerror(errno));
864 return SYNC_AGENT_FSAPI_FALSE;
867 if (!sync_agent_delete_file(path)) {
868 _DEBUG_ERROR("[%s] sync_agent_delete_file error : %s\n", __func__, strerror(errno));
869 return SYNC_AGENT_FSAPI_FALSE;
881 return SYNC_AGENT_FSAPI_TRUE;
884 EXPORT_API sync_agent_fsapi_return_e sync_agent_rename_directory(const char *source, const char *dest)
888 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
889 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
891 if (!fs_is_existing(source) || !fs_is_directory(source) || fs_is_existing(dest)) {
892 _DEBUG_ERROR("[%s] Requested source instance '%s' not exist or not a directory, or\n", __func__, source);
893 _DEBUG_ERROR("[%s] requested dest instance '%s' already exist.\n", __func__, dest);
894 return SYNC_AGENT_FSAPI_FALSE;
897 int ret = g_rename(source, dest);
899 _DEBUG_ERROR("[%s] g_rename error : %s\n", __func__, strerror(errno));
900 return SYNC_AGENT_FSAPI_FALSE;
905 return SYNC_AGENT_FSAPI_TRUE;
908 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_directory_list(const char *folder_name, GSList ** list)
912 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
914 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
915 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
916 return SYNC_AGENT_FSAPI_FALSE;
921 char time[FS_MAX_TIME_STRING_BUF] = { 0 };
922 char *list_item = NULL;
924 GError *gerror = NULL;
925 GDir *dir = g_dir_open(folder_name, 0, &gerror);
927 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
928 g_error_free(gerror);
929 return SYNC_AGENT_FSAPI_FALSE;
932 const char *file_name = g_dir_read_name(dir);
934 for (; file_name; file_name = g_dir_read_name(dir)) {
936 path = g_strdup_printf("%s/%s", folder_name, file_name);
938 if (fs_is_directory(path)) {
939 int ret = stat(path, &buf);
941 _DEBUG_ERROR("[%s] g_stat error : %s\n", __func__, strerror(errno));
942 return SYNC_AGENT_FSAPI_FALSE;
945 fs_get_time_string(&buf.st_mtime, time);
946 list_item = g_strdup_printf("%s,%ld,%s", file_name, (long)buf.st_nlink - 2, time);
948 *list = g_slist_append(*list, list_item);
958 return SYNC_AGENT_FSAPI_TRUE;
962 EXPORT_API void sync_agent_free_fs_list(GSList * list)
966 retm_if(list == NULL, "folder_name paramter is NULL !!!");
970 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
979 EXPORT_API sync_agent_fsapi_return_e sync_agent_file_md5_validation(const char *file_path, const char *find_md5_word, bool * is_same)
983 retvm_if(file_path == NULL, 0, "file_path paramter is NULL !!!");
984 retvm_if(find_md5_word == NULL, 0, "find_md5_word paramter is NULL !!!");
987 unsigned char *real_md5_word = NULL;
989 _DEBUG_INFO("File_Path = %s\n", file_path);
991 /* get file hash key */
992 ret = fs_file_md5_hashkey(file_path, &real_md5_word);
994 _DEBUG_ERROR("fs_file_md5_hashkey() Fail!!\n");
998 /* convert hex char to string (=> hash key) */
999 char szTmp_digest[256] = { 0, };
1001 for (k = 0; k < 16; k++) {
1002 szTmp_digest[k * 2] = _sync_str_to_hex_char((unsigned char)((real_md5_word[k] >> 4) & 0x0f));
1003 szTmp_digest[k * 2 + 1] = _sync_str_to_hex_char((unsigned char)(real_md5_word[k] & 0x0f));
1006 _DEBUG_INFO("find_md5_word = %s\n", find_md5_word);
1007 _DEBUG_INFO("real_md5_word = %s\n", szTmp_digest);
1009 /* compare read md5 checksum to file_md5_checksum */
1010 if (memcmp(find_md5_word, szTmp_digest, strlen(szTmp_digest))) {
1011 _DEBUG_ERROR("MD5 Hash Keys are not equal.\n");
1017 _DEBUG_INFO("MD5 Hash Keys are equal.\n");
1024 if (real_md5_word != NULL)
1025 free(real_md5_word);
1030 int fs_file_md5_hashkey(const char *file_path, unsigned char **hash_key)
1034 retvm_if(file_path == NULL, 0, "file_path paramter is NULL !!!");
1039 unsigned long full_file_size = 0;
1040 ret = sync_agent_get_file_size(file_path, &full_file_size);
1041 _DEBUG_INFO("full_file_size = %d\n", full_file_size);
1043 /* get MD5 encryption Hash Key */
1044 char *required_contents = NULL;
1045 unsigned char *real_md5_word = (unsigned char *)calloc(16, sizeof(char));
1046 if (real_md5_word == NULL) {
1047 _DEBUG_ERROR("CALLOC failed !!!");
1051 bool isFinal = false;
1052 unsigned long read_file_size = 0;
1054 sa_md5_context_s context;
1056 sa_md5_init(&context);
1058 while (isFinal == false) {
1060 ret = sync_agent_read_file(file_path, &required_contents, FS_BUFFER_SIZE, &read_file_size, &isFinal);
1062 _DEBUG_ERROR("sync_agent_read_file() Fail\n");
1067 _DEBUG_INFO("read_file_size = %d\n", read_file_size);
1068 _DEBUG_INFO("isFinal = %d\n", isFinal);
1069 sa_md5_update(&context, required_contents, read_file_size);
1071 sa_md5_final(&context, real_md5_word);
1073 /* print Hex char MD5 */
1074 int real_md5_length = strlen((const char *)real_md5_word);
1075 _DEBUG_INFO("real_md5_length = %d\n", real_md5_length);
1076 _DEBUG_INFO(" real md5 : ");
1078 for (; i < real_md5_length; ++i) {
1079 _DEBUG_INFO("%02x", real_md5_word[i]);
1083 *hash_key = real_md5_word;
1085 _DEBUG_INFO("Free Part");
1086 sync_agent_free_file(&required_contents);
1092 if (real_md5_word != NULL) {
1093 free(real_md5_word);
1094 real_md5_word = NULL;
1096 _DEBUG_INFO("Free Part");
1097 sync_agent_free_file(&required_contents);
1103 static unsigned char _sync_str_to_hex_char(unsigned char hex)
1107 if (0 <= (char)hex && (char)hex <= 9) {
1111 if (10 <= (char)hex && (char)hex <= 15) {
1113 return 'A' + hex - 10;