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"
34 #define EXPORT_API __attribute__ ((visibility("default")))
37 #ifndef SYNC_AGENT_LOG
39 #define LOG_TAG "AF_FS"
42 #define FS_BUFFER_SIZE 2048
44 static bool file_append_started = FALSE;
45 static bool file_read_started = FALSE;
46 static char *temp_filename_kept = NULL;
47 static char *requested_filename_kept = NULL;
48 static FILE *fd_source_kept = NULL;
50 static unsigned char _sync_str_to_hex_char(unsigned char hex);
52 EXPORT_API sync_agent_fsapi_return_e sync_agent_is_existing_fs(const char *file_name)
56 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
60 return fs_is_existing(file_name);
63 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_fs_info(char **result)
67 retvm_if(result == NULL, 0, "result paramter is NULL !!!");
71 long double free_size = 0;
72 long double total_size = 0;
74 int file_name_len_max = 256;
75 int folder_name_len_max = 256;
77 char *file_name_prevenet_char = ":;*?/<>\"";
78 char *folder_name_prevent_char = ":;*?/<>\"";
80 int file_name_code_schema = 2;
81 int allow_dup_name = 0;
82 int folder_depth_max = 15;
84 ret = sync_agent_get_fs_mem_size("/opt/usr/media", &free_size, &total_size);
86 _DEBUG_ERROR("[sync_agent_get_fs_info] getting mem size fail");
87 return SYNC_AGENT_FSAPI_FALSE;
90 *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);
94 return SYNC_AGENT_FSAPI_TRUE;
97 EXPORT_API sync_agent_fsapi_return_e sync_agent_free_fs_info(char *result)
101 retvm_if(result == NULL, 0, "result paramter is NULL !!!");
108 return SYNC_AGENT_FSAPI_TRUE;
111 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_fs_mem_size(const char *root, long double *size_free, long double *size_total)
115 retvm_if(root == NULL, 0, "root paramter is NULL !!!");
117 if (!fs_is_existing(root)) {
118 _DEBUG_ERROR("[%s] Requested instance '%s' not exist.\n", __func__, root);
119 return SYNC_AGENT_FSAPI_FALSE;
122 struct statfs fs_info;
124 if (statfs(root, &fs_info) != 0) {
125 _DEBUG_ERROR("[sync_agent_get_fs_mem_size] statfs error.");
126 return SYNC_AGENT_FSAPI_FALSE;
129 *size_free = ((long double)fs_info.f_bavail * (long double)fs_info.f_bsize); /* / 1024; */
130 *size_total = ((long double)fs_info.f_blocks * (long double)fs_info.f_bsize); /* / 1024; */
132 _DEBUG_INFO("[sync_agent_get_fs_mem_size] FS info root : %s.", root);
133 _DEBUG_INFO("[sync_agent_get_fs_mem_size] FS info free size : %.0f", *size_free);
134 _DEBUG_INFO("[sync_agent_get_fs_mem_size] FS info total size : %.0f", *size_total);
138 return SYNC_AGENT_FSAPI_TRUE;
145 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_file_size(const char *file_name, unsigned long *size)
149 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
151 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
152 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
153 return SYNC_AGENT_FSAPI_FALSE;
158 int ret = stat(file_name, &buf);
160 _DEBUG_ERROR("[%s] stat error : %s", __func__, strerror(errno));
161 return SYNC_AGENT_FSAPI_FALSE;
164 *size = (unsigned long)buf.st_size;
168 return SYNC_AGENT_FSAPI_TRUE;
171 int fs_get_file_date(const char *file_name, char **time_string)
175 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
177 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
178 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
179 return SYNC_AGENT_FSAPI_FALSE;
183 char time[FS_MAX_TIME_STRING_BUF] = { 0 };
185 int ret = stat(file_name, &buf);
187 _DEBUG_ERROR("[%s] stat error : %s", __func__, strerror(errno));
188 return SYNC_AGENT_FSAPI_FALSE;
191 fs_get_time_string(&buf.st_mtime, time);
192 *time_string = g_strdup(time);
196 return SYNC_AGENT_FSAPI_TRUE;
199 EXPORT_API sync_agent_fsapi_return_e sync_agent_copy_file(const char *source, const char *dest)
203 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
204 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
206 if (!fs_is_existing(source) || fs_is_directory(source) || fs_is_existing(dest)) {
207 _DEBUG_ERROR("[%s] Requested source instance '%s' not exist or not a file, or\n", __func__, source);
208 _DEBUG_ERROR("[%s] requested dest instance '%s' already exist.\n", __func__, dest);
209 return SYNC_AGENT_FSAPI_FALSE;
212 FILE *f1 = NULL, *f2 = NULL;
214 char buf[1024 * 100] = { 0 };
215 unsigned long bytes_read = 0;
216 unsigned long bytes_written = 0;
218 f1 = (FILE *) g_fopen(source, "rb");
220 _DEBUG_ERROR("[sync_agent_copy_file] file f1 open error.");
225 f2 = (FILE *) g_fopen(dest, "wb");
227 _DEBUG_ERROR("[sync_agent_copy_file] file f2 open error.");
232 while ((bytes_read = fread(buf, 1, sizeof(buf), f1)) > 0) {
233 if (bytes_read > sizeof(buf)) {
234 _DEBUG_ERROR("[sync_agent_copy_file] read error.");
239 bytes_written = fwrite(buf, 1, bytes_read, f2);
240 if (!feof(f1) && bytes_written != bytes_read) {
241 _DEBUG_ERROR("[sync_agent_copy_file] write error.");
260 if( !File_Read(source, &body, &size) ) {
261 fprintf(stderr, "[%s] File_Read() returned error.\n", __func__);
262 return SYNC_AGENT_FSAPI_FALSE;
265 if( !File_Write(dest, body, size) ) {
266 fprintf(stderr, "[%s] File_Read() returned error.\n", __func__);
267 return SYNC_AGENT_FSAPI_FALSE;
272 return SYNC_AGENT_FSAPI_TRUE;
276 EXPORT_API sync_agent_fsapi_return_e sync_agent_delete_file(const char *file_name)
280 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
282 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
283 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
284 return SYNC_AGENT_FSAPI_FALSE;
287 int ret = g_unlink(file_name);
289 _DEBUG_ERROR("[%s] g_unlink error : %s\n", __func__, strerror(errno));
290 return SYNC_AGENT_FSAPI_FALSE;
295 return SYNC_AGENT_FSAPI_TRUE;
298 EXPORT_API sync_agent_fsapi_return_e sync_agent_move_file(const char *source, const char *dest)
302 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
303 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
305 if (!fs_is_existing(source) || fs_is_directory(source) || fs_is_existing(dest)) {
306 _DEBUG_ERROR("[%s] Requested source instance '%s' not exist or not a file, or\n", __func__, source);
307 _DEBUG_ERROR("[%s] requested dest instance '%s' already exist.\n", __func__, dest);
308 return SYNC_AGENT_FSAPI_FALSE;
311 int ret = g_rename(source, dest);
313 _DEBUG_ERROR("[sync_agent_move_file] g_rename error : %s", strerror(errno));
314 _DEBUG_ERROR("[sync_agent_move_file] source : %s target : %s", source, dest);
315 return SYNC_AGENT_FSAPI_FALSE;
320 return SYNC_AGENT_FSAPI_TRUE;
323 EXPORT_API sync_agent_fsapi_return_e sync_agent_rename_file(const char *source, const char *dest)
327 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
328 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
332 return sync_agent_move_file(source, dest);
335 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_file_list(const char *folder_name, GSList ** list)
339 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
341 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
342 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
343 return SYNC_AGENT_FSAPI_FALSE;
348 char time[FS_MAX_TIME_STRING_BUF] = { 0 };
349 char *list_item = NULL;
351 GError *gerror = NULL;
352 GDir *dir = g_dir_open(folder_name, 0, &gerror);
354 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
355 g_error_free(gerror);
356 return SYNC_AGENT_FSAPI_FALSE;
359 const char *file_name = g_dir_read_name(dir);
361 for (; file_name; file_name = g_dir_read_name(dir)) {
363 path = g_strdup_printf("%s/%s", folder_name, file_name);
365 if (!fs_is_directory(path)) {
366 int ret = stat(path, &buf);
368 _DEBUG_ERROR("[%s] g_stat error : %s\n", __func__, strerror(errno));
369 return SYNC_AGENT_FSAPI_FALSE;
372 fs_get_time_string(&buf.st_mtime, time);
373 list_item = g_strdup_printf("%s,%ld,%s", file_name, (long)buf.st_size, time);
375 *list = g_slist_append(*list, list_item);
385 return SYNC_AGENT_FSAPI_TRUE;
388 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)
392 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
394 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
395 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
396 return SYNC_AGENT_FSAPI_FALSE;
401 if ((fd_source_kept == NULL) && !file_read_started) {
402 fd_source_kept = (FILE *) g_fopen(file_name, "rb");
403 if (fd_source_kept == NULL) {
404 _DEBUG_ERROR("[sync_agent_read_file] temp_filename_kept open error.");
408 file_read_started = TRUE;
409 } else if ((fd_source_kept != NULL) && file_read_started) {
411 _DEBUG_ERROR("[%s] internal condition not matching.\n", __func__);
416 *required_contents = (char *)calloc(required_length, sizeof(char));
417 if (*required_contents == NULL) {
418 _DEBUG_ERROR("[sync_agent_read_file] calloc error. [*required_contents]");
424 bytes_read = fread(*required_contents, 1, required_length, fd_source_kept);
426 if ((bytes_read > required_length) || ((!feof(fd_source_kept)) && (bytes_read < required_length))) {
427 _DEBUG_ERROR("[sync_agent_read_file] read error.");
432 *size_return = bytes_read;
434 if (feof(fd_source_kept)) {
445 if (fd_source_kept != NULL) {
446 fclose(fd_source_kept);
447 fd_source_kept = NULL;
449 file_read_started = FALSE;
454 if( !fs_is_existing(file_name) || fs_is_directory(file_name) ) {
455 fprintf(stderr, "[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
456 return SYNC_AGENT_FSAPI_FALSE;
459 GError *gerror = NULL;
461 if( !g_file_get_contents(file_name, contents, (gsize*)length, &gerror) ) {
462 fprintf(stderr, "[%s] g_file_get_contents error : %s\n", __func__, gerror->message);
464 g_error_free(gerror);
465 return SYNC_AGENT_FSAPI_FALSE;
468 return SYNC_AGENT_FSAPI_TRUE;
472 EXPORT_API sync_agent_fsapi_return_e sync_agent_read_whole_file(const char *file_name, char **required_contents, unsigned long *size_return)
476 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
478 if (!fs_is_existing(file_name) || fs_is_directory(file_name)) {
479 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a file.\n", __func__, file_name);
480 return SYNC_AGENT_FSAPI_FALSE;
483 GError *gerror = NULL;
485 if (!g_file_get_contents(file_name, required_contents, (gsize *) size_return, &gerror)) {
486 _DEBUG_ERROR("[%s] g_file_get_contents error : %s\n", __func__, gerror->message);
488 g_error_free(gerror);
489 return SYNC_AGENT_FSAPI_FALSE;
494 return SYNC_AGENT_FSAPI_TRUE;
497 EXPORT_API sync_agent_fsapi_return_e sync_agent_free_file(char **requested_contents)
501 retvm_if(requested_contents == NULL, 0, "requested_contents paramter is NULL !!!");
503 if (*requested_contents != NULL)
504 free(*requested_contents);
508 return SYNC_AGENT_FSAPI_TRUE;
511 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)
515 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
516 retvm_if(contents == NULL, 0, "contents is NULL !!");
519 if (fs_is_existing(file_name)) {
520 _DEBUG_ERROR("[%s] Requested instance '%s' already exist.\n", __func__, file_name);
521 return SYNC_AGENT_FSAPI_FALSE;
525 FILE *fd_temp = NULL;
526 GError *gerror = NULL;
528 unsigned long temp_size = 0;
530 if ((temp_filename_kept == NULL) && !file_append_started) {
531 if (!g_file_open_tmp(NULL, &temp_filename_kept, &gerror)) {
532 _DEBUG_ERROR("[%s] g_file_open_tmp error : %s\n", __func__, gerror->message);
534 g_error_free(gerror);
538 _DEBUG_INFO("[sync_agent_write_file] Temp file name kept new : %s", temp_filename_kept);
540 file_append_started = TRUE;
541 requested_filename_kept = g_strdup(file_name);
543 } else if ((temp_filename_kept != NULL) && file_append_started) {
544 if (!fs_is_existing(temp_filename_kept)) {
545 _DEBUG_ERROR("[%s] Temp file '%s' not existing.\n", __func__, temp_filename_kept);
549 _DEBUG_INFO("[sync_agent_write_file] Temp file %s kept previously, existing", temp_filename_kept);
552 _DEBUG_ERROR("[%s] internal condition not matching.\n", __func__);
557 if (temp_filename_kept != NULL) {
558 fd_temp = (FILE *) g_fopen(temp_filename_kept, "ab");
563 if (fd_temp == NULL) {
564 _DEBUG_ERROR("[sync_agent_write_file] temp file open error.");
569 if (fwrite(contents, 1, length, fd_temp) != length) {
570 _DEBUG_ERROR("[sync_agent_write_file] fwrite error.");
575 if (fd_temp != NULL) {
580 sync_agent_get_file_size(temp_filename_kept, &temp_size);
581 _DEBUG_INFO("[sync_agent_write_file] temp file size %ld", temp_size);
585 if (fs_is_existing(file_name)) {
586 if (!sync_agent_delete_file(file_name)) {
587 _DEBUG_ERROR("[sync_agent_write_file] deleting destination file for overwriting fail.");
590 if (!sync_agent_copy_file(temp_filename_kept, file_name)) {
591 _DEBUG_ERROR("[sync_agent_write_file] copying temp file to destination fail.");
594 if (!sync_agent_delete_file(temp_filename_kept)) {
595 _DEBUG_ERROR("[sync_agent_write_file] deleting temp file fail.");
601 if (!sync_agent_copy_file(temp_filename_kept, file_name)) {
602 _DEBUG_ERROR("[sync_agent_write_file] copying temp file to destination fail.");
605 if (!sync_agent_delete_file(temp_filename_kept)) {
606 _DEBUG_ERROR("[sync_agent_write_file] deleting temp file fail.");
612 if (!sync_agent_copy_file(temp_filename_kept, file_name)) {
613 _DEBUG_ERROR("[sync_agent_write_file] copying temp file to destination fail.");
616 if (!sync_agent_delete_file(temp_filename_kept)) {
617 _DEBUG_ERROR("[sync_agent_write_file] deleting temp file fail.");
633 g_free(temp_filename_kept);
634 g_free(requested_filename_kept);
635 temp_filename_kept = NULL;
636 requested_filename_kept = NULL;
637 file_append_started = FALSE;
642 if( fs_is_existing(file_name) ) {
643 fprintf(stderr, "[%s] Requested instance '%s' already exist.", __func__, file_name);
644 return SYNC_AGENT_FSAPI_FALSE;
647 GError *gerror = NULL;
649 if( !g_file_set_contents(file_name, contents, length, &gerror) ) {
650 fprintf(stderr, "[%s] g_file_set_contents error : %s\n", __func__, gerror->message);
652 g_error_free(gerror);
653 return SYNC_AGENT_FSAPI_FALSE;
656 return SYNC_AGENT_FSAPI_TRUE;
660 EXPORT_API sync_agent_fsapi_return_e sync_agent_write_whole_file(const char *file_name, const char *contents, unsigned long length, bool overwrite)
664 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
665 retvm_if(contents == NULL, 0, "contents is NULL !!");
667 if (fs_is_existing(file_name)) {
668 if (overwrite && !fs_is_directory(file_name)) {
669 int result = sync_agent_delete_file(file_name);
671 _DEBUG_ERROR("[%s] sync_agent_delete_file() FAIL !!!.", __func__, file_name);
672 return SYNC_AGENT_FSAPI_FALSE;
675 _DEBUG_ERROR("[%s] Requested instance '%s' already exist.", __func__, file_name);
676 return SYNC_AGENT_FSAPI_FALSE;
680 GError *gerror = NULL;
682 if (!g_file_set_contents(file_name, contents, length, &gerror)) {
683 _DEBUG_ERROR("[%s] g_file_set_contents error : %s\n", __func__, gerror->message);
685 g_error_free(gerror);
686 return SYNC_AGENT_FSAPI_FALSE;
691 return SYNC_AGENT_FSAPI_TRUE;
694 EXPORT_API sync_agent_fsapi_return_e sync_agent_append_file(const char *file_name, const char *contents, unsigned long length, bool overwrite)
698 retvm_if(file_name == NULL, 0, "file_name paramter is NULL !!!");
699 retvm_if(contents == NULL, 0, "contents is NULL !!");
702 if (!sync_agent_delete_file(file_name)) {
703 if (!fs_is_existing(file_name)) {
704 _DEBUG_ERROR("[%s] The file %s requested to delete for overwriting is not existing, so just continue.", __func__, file_name);
706 if (fs_is_directory(file_name)) {
707 _DEBUG_ERROR("[%s] Can't overwrite because requested instance '%s' not a file.", __func__, file_name);
708 return SYNC_AGENT_FSAPI_FALSE;
710 _DEBUG_ERROR("[%s] FAILED to delete requested file '%s'.", __func__, file_name);
711 return SYNC_AGENT_FSAPI_FALSE;
720 fd = (FILE *) g_fopen(file_name, "ab");
722 _DEBUG_ERROR("[sync_agent_append_file] file open error.");
727 if (fwrite(contents, 1, length, fd) != length) {
728 _DEBUG_ERROR("[%s] fwrite error '%s'.", __func__, file_name);
733 if (fsync(fileno(fd)) != 0)
743 _DEBUG_INFO("[%s] Returning..", __func__);
751 EXPORT_API sync_agent_fsapi_return_e sync_agent_create_directory(const char *folder_name)
755 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
757 if (fs_is_existing(folder_name)) {
758 _DEBUG_ERROR("[%s] Requested instance '%s' already exist.\n", __func__, folder_name);
759 return SYNC_AGENT_FSAPI_FALSE;
762 int ret = g_mkdir_with_parents(folder_name, 0755);
764 _DEBUG_ERROR("[%s] g_mkdir error : %s\n", __func__, strerror(errno));
765 return SYNC_AGENT_FSAPI_FALSE;
770 return SYNC_AGENT_FSAPI_TRUE;
773 EXPORT_API sync_agent_fsapi_return_e sync_agent_delete_directory(const char *folder_name, bool recursive)
777 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
779 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
780 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
781 return SYNC_AGENT_FSAPI_FALSE;
784 GError *gerror = NULL;
791 dir = g_dir_open(folder_name, 0, &gerror);
793 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
794 g_error_free(gerror);
795 return SYNC_AGENT_FSAPI_FALSE;
798 name = (char *)g_dir_read_name(dir);
800 for (; name; name = (char *)g_dir_read_name(dir)) {
802 path = g_strdup_printf("%s/%s", folder_name, name);
804 if (fs_is_directory(path)) {
805 if (!sync_agent_delete_directory(path, true))
806 return SYNC_AGENT_FSAPI_FALSE;
808 if (!sync_agent_delete_file(path))
809 return SYNC_AGENT_FSAPI_FALSE;
819 ret = g_rmdir(folder_name);
821 _DEBUG_ERROR("[%s] g_rmdir error : %s\n", __func__, strerror(errno));
822 return SYNC_AGENT_FSAPI_FALSE;
827 return SYNC_AGENT_FSAPI_TRUE;
830 EXPORT_API sync_agent_fsapi_return_e sync_agent_empty_directory_contents(const char *folder_name)
834 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
836 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
837 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
838 return SYNC_AGENT_FSAPI_FALSE;
841 GError *gerror = NULL;
846 dir = g_dir_open(folder_name, 0, &gerror);
848 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
849 g_error_free(gerror);
850 return SYNC_AGENT_FSAPI_FALSE;
853 name = (char *)g_dir_read_name(dir);
855 for (; name; name = (char *)g_dir_read_name(dir)) {
857 path = g_strdup_printf("%s/%s", folder_name, name);
859 if (fs_is_directory(path)) {
860 if (!sync_agent_delete_directory(path, true)) {
861 _DEBUG_ERROR("[%s] sync_agent_delete_directory error : %s\n", __func__, strerror(errno));
862 return SYNC_AGENT_FSAPI_FALSE;
865 if (!sync_agent_delete_file(path)) {
866 _DEBUG_ERROR("[%s] sync_agent_delete_file error : %s\n", __func__, strerror(errno));
867 return SYNC_AGENT_FSAPI_FALSE;
879 return SYNC_AGENT_FSAPI_TRUE;
882 EXPORT_API sync_agent_fsapi_return_e sync_agent_rename_directory(const char *source, const char *dest)
886 retvm_if(source == NULL, 0, "source paramter is NULL !!!");
887 retvm_if(dest == NULL, 0, "dest paramter is NULL !!!");
889 if (!fs_is_existing(source) || !fs_is_directory(source) || fs_is_existing(dest)) {
890 _DEBUG_ERROR("[%s] Requested source instance '%s' not exist or not a directory, or\n", __func__, source);
891 _DEBUG_ERROR("[%s] requested dest instance '%s' already exist.\n", __func__, dest);
892 return SYNC_AGENT_FSAPI_FALSE;
895 int ret = g_rename(source, dest);
897 _DEBUG_ERROR("[%s] g_rename error : %s\n", __func__, strerror(errno));
898 return SYNC_AGENT_FSAPI_FALSE;
903 return SYNC_AGENT_FSAPI_TRUE;
906 EXPORT_API sync_agent_fsapi_return_e sync_agent_get_directory_list(const char *folder_name, GSList ** list)
910 retvm_if(folder_name == NULL, 0, "folder_name paramter is NULL !!!");
912 if (!fs_is_existing(folder_name) || !fs_is_directory(folder_name)) {
913 _DEBUG_ERROR("[%s] Requested instance '%s' not exist, or not a directory.\n", __func__, folder_name);
914 return SYNC_AGENT_FSAPI_FALSE;
919 char time[FS_MAX_TIME_STRING_BUF] = { 0 };
920 char *list_item = NULL;
922 GError *gerror = NULL;
923 GDir *dir = g_dir_open(folder_name, 0, &gerror);
925 _DEBUG_ERROR("[%s] g_dir_open error : %s\n", __func__, gerror->message);
926 g_error_free(gerror);
927 return SYNC_AGENT_FSAPI_FALSE;
930 const char *file_name = g_dir_read_name(dir);
932 for (; file_name; file_name = g_dir_read_name(dir)) {
934 path = g_strdup_printf("%s/%s", folder_name, file_name);
936 if (fs_is_directory(path)) {
937 int ret = stat(path, &buf);
939 _DEBUG_ERROR("[%s] g_stat error : %s\n", __func__, strerror(errno));
940 return SYNC_AGENT_FSAPI_FALSE;
943 fs_get_time_string(&buf.st_mtime, time);
944 list_item = g_strdup_printf("%s,%ld,%s", file_name, (long)buf.st_nlink - 2, time);
946 *list = g_slist_append(*list, list_item);
956 return SYNC_AGENT_FSAPI_TRUE;
960 EXPORT_API void sync_agent_free_fs_list(GSList * list)
964 retm_if(list == NULL, "folder_name paramter is NULL !!!");
968 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
977 EXPORT_API sync_agent_fsapi_return_e sync_agent_file_md5_validation(const char *file_path, const char *find_md5_word, bool * is_same)
981 retvm_if(file_path == NULL, 0, "file_path paramter is NULL !!!");
982 retvm_if(find_md5_word == NULL, 0, "find_md5_word paramter is NULL !!!");
985 unsigned char *real_md5_word = NULL;
987 _DEBUG_INFO("File_Path = %s\n", file_path);
989 /* get file hash key */
990 ret = fs_file_md5_hashkey(file_path, &real_md5_word);
992 _DEBUG_ERROR("fs_file_md5_hashkey() Fail!!\n");
996 /* convert hex char to string (=> hash key) */
997 char szTmp_digest[256] = { 0, };
999 for (k = 0; k < 16; k++) {
1000 szTmp_digest[k * 2] = _sync_str_to_hex_char((unsigned char)((real_md5_word[k] >> 4) & 0x0f));
1001 szTmp_digest[k * 2 + 1] = _sync_str_to_hex_char((unsigned char)(real_md5_word[k] & 0x0f));
1004 _DEBUG_INFO("find_md5_word = %s\n", find_md5_word);
1005 _DEBUG_INFO("real_md5_word = %s\n", szTmp_digest);
1007 /* compare read md5 checksum to file_md5_checksum */
1008 if (memcmp(find_md5_word, szTmp_digest, strlen(szTmp_digest))) {
1009 _DEBUG_ERROR("MD5 Hash Keys are not equal.\n");
1015 _DEBUG_INFO("MD5 Hash Keys are equal.\n");
1022 if (real_md5_word != NULL)
1023 free(real_md5_word);
1028 int fs_file_md5_hashkey(const char *file_path, unsigned char **hash_key)
1032 retvm_if(file_path == NULL, 0, "file_path paramter is NULL !!!");
1037 unsigned long full_file_size = 0;
1038 ret = sync_agent_get_file_size(file_path, &full_file_size);
1039 _DEBUG_INFO("full_file_size = %d\n", full_file_size);
1041 /* get MD5 encryption Hash Key */
1042 char *required_contents = NULL;
1043 unsigned char *real_md5_word = (unsigned char *)calloc(16, sizeof(char));
1044 if (real_md5_word == NULL) {
1045 _DEBUG_ERROR("CALLOC failed !!!");
1049 bool isFinal = false;
1050 unsigned long read_file_size = 0;
1052 sa_md5_context_s context;
1054 sa_md5_init(&context);
1056 while (isFinal == false) {
1058 ret = sync_agent_read_file(file_path, &required_contents, FS_BUFFER_SIZE, &read_file_size, &isFinal);
1060 _DEBUG_ERROR("sync_agent_read_file() Fail\n");
1064 _DEBUG_INFO("read_file_size = %d\n", read_file_size);
1065 _DEBUG_INFO("isFinal = %d\n", isFinal);
1066 sa_md5_update(&context, required_contents, read_file_size);
1068 sa_md5_final(&context, real_md5_word);
1070 /* print Hex char MD5 */
1071 int real_md5_length = strlen((const char *)real_md5_word);
1072 _DEBUG_INFO("real_md5_length = %d\n", real_md5_length);
1073 _DEBUG_INFO(" real md5 : ");
1075 for (; i < real_md5_length; ++i) {
1076 _DEBUG_INFO("%02x", real_md5_word[i]);
1080 *hash_key = real_md5_word;
1082 _DEBUG_INFO("Free Part");
1083 sync_agent_free_file(&required_contents);
1091 static unsigned char _sync_str_to_hex_char(unsigned char hex)
1095 if (0 <= (char)hex && (char)hex <= 9) {
1099 if (10 <= (char)hex && (char)hex <= 15) {
1101 return 'A' + hex - 10;