diff --git a/mms-lib/src/mms_file_util.c b/mms-lib/src/mms_file_util.c index a906672..f508928 100644 --- a/mms-lib/src/mms_file_util.c +++ b/mms-lib/src/mms_file_util.c @@ -177,6 +177,51 @@ mms_write_bytes( return mms_write_file(dir, file, data, len, path); } +/** + * Copies the file. Assumes that the destination directory exists. + */ +gboolean +mms_file_copy( + const char* src, + const char* dest, + GError** error) +{ + gboolean ok = FALSE; + int out = open(dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, MMS_FILE_PERM); + if (out >= 0) { + int in = open(src, O_RDONLY | O_BINARY); + if (in >= 0) { + int nbytes; + const size_t buflen = 4096; + void* buf = g_malloc(buflen); + ok = TRUE; + while ((nbytes = read(in, buf, buflen)) > 0) { + if (write(out, buf, nbytes) < nbytes) { + ok = FALSE; + MMS_ERROR(error, MMS_LIB_ERROR_IO, + "Failed to write %s: %s", dest, strerror(errno)); + break; + } + } + if (nbytes < 0) { + ok = FALSE; + MMS_ERROR(error, MMS_LIB_ERROR_IO, + "Failed to read %s: %s", src, strerror(errno)); + } + g_free(buf); + close(in); + } else { + MMS_ERROR(error, MMS_LIB_ERROR_IO, + "Failed to open file %s: %s", src, strerror(errno)); + } + close(out); + } else { + MMS_ERROR(error, MMS_LIB_ERROR_IO, + "Failed to create file %s: %s", dest, strerror(errno)); + } + return ok; +} + /* * Local Variables: * mode: C diff --git a/mms-lib/src/mms_file_util.h b/mms-lib/src/mms_file_util.h index fd28feb..f308067 100644 --- a/mms-lib/src/mms_file_util.h +++ b/mms-lib/src/mms_file_util.h @@ -69,6 +69,12 @@ mms_write_bytes( GBytes* bytes, char** path); +gboolean +mms_file_copy( + const char* src, + const char* dest, + GError** error); + #define mms_message_dir(config,id) \ (g_strconcat((config)->root_dir, "/" MMS_MESSAGE_DIR "/" , id, NULL)) #define mms_task_dir(task) \ diff --git a/mms-lib/src/mms_task_encode.c b/mms-lib/src/mms_task_encode.c index e8789f8..856ff92 100644 --- a/mms-lib/src/mms_task_encode.c +++ b/mms-lib/src/mms_task_encode.c @@ -467,8 +467,6 @@ mms_task_encode_prepare_attachments( MMSAttachmentInfo info = parts[i]; char* detected_type = NULL; char* path; - GFile* src; - GFile* dest; if (!info.content_type || !info.content_type[0]) { detected_type = mms_attachment_guess_content_type(info.file_name); @@ -479,9 +477,7 @@ mms_task_encode_prepare_attachments( g_basename(info.file_name), info.content_type); G_GNUC_END_IGNORE_DEPRECATIONS; - src = g_file_new_for_path(info.file_name); - dest = g_file_new_for_path(path); - if (g_file_copy(src, dest, 0, NULL, NULL, NULL, error)) { + if (mms_file_copy(info.file_name, path, error)) { info.file_name = path; attachment = mms_attachment_new(config, &info, error); if (attachment) { @@ -494,8 +490,7 @@ mms_task_encode_prepare_attachments( } else if (error && *error) { MMS_ERR("%s", MMS_ERRMSG(*error)); } - g_object_unref(src); - g_object_unref(dest); + g_free(detected_type); g_free(path); if (!attachment) break; diff --git a/mms-lib/test/resize/test_resize.c b/mms-lib/test/resize/test_resize.c index b048277..485530f 100644 --- a/mms-lib/test/resize/test_resize.c +++ b/mms-lib/test/resize/test_resize.c @@ -15,6 +15,7 @@ #include "mms_attachment.h" #include "mms_lib_util.h" #include "mms_lib_log.h" +#include "mms_file_util.h" #include "mms_log.h" #include @@ -342,31 +343,6 @@ test_png_size( return ok; } -static -gboolean -test_file_copy( - const char* src, - const char* dest) -{ - gboolean ok = FALSE; - FILE* in = fopen(src, "rb"); - if (in) { - FILE* out = fopen(dest, "wb"); - if (out) { - const size_t buflen = 4096; - size_t nbytes; - void* buf = g_malloc(buflen); - while ((nbytes = fread(buf, 1, buflen, in)) > 0 && - fwrite(buf, 1, nbytes, out) == nbytes); - ok = (feof(in) && !ferror(in) && !ferror(out)); - g_free(buf); - fclose(out); - } - fclose(in); - } - return ok; -} - static int test_run_one( @@ -382,7 +358,7 @@ test_run_one( if (dir) { GError* error = NULL; char* testfile = g_strconcat(dir, "/", name, NULL); - if (test_file_copy(test->file, testfile)) { + if (mms_file_copy(test->file, testfile, NULL)) { MMSAttachment* at; MMSAttachmentInfo info; MMSConfig test_config = *config; diff --git a/rpm/mms-engine.spec b/rpm/mms-engine.spec index 0eff9b4..780ceeb 100644 --- a/rpm/mms-engine.spec +++ b/rpm/mms-engine.spec @@ -12,6 +12,8 @@ Requires: ofono BuildRequires: python BuildRequires: file-devel +BuildRequires: libpng-devel +BuildRequires: libexif-devel BuildRequires: libjpeg-turbo-devel BuildRequires: pkgconfig(glib-2.0) >= 2.32 BuildRequires: pkgconfig(libsoup-2.4) >= 2.38 @@ -62,6 +64,9 @@ cp %{src}/%{dbusname}.push.conf %{buildroot}%{pushconfig}/%{dbusname}.conf cp mms-dump/build/release/mms-dump %{buildroot}%{_prefix}/bin/ cp mms-send/build/release/mms-send %{buildroot}%{_prefix}/bin/ +%check +make -C mms-lib/test test + %files %defattr(-,root,root,-) %config %{dbuspolicy}/%{dbusname}.conf