Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[mms-lib] Replaced g_file_copy with mms_file_copy
Now that qemu no longer crashes during tests, enabled rpm check
  • Loading branch information
monich committed Apr 17, 2014
1 parent 96462e1 commit 29ebd7b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
45 changes: 45 additions & 0 deletions mms-lib/src/mms_file_util.c
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions mms-lib/src/mms_file_util.h
Expand Up @@ -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) \
Expand Down
9 changes: 2 additions & 7 deletions mms-lib/src/mms_task_encode.c
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
28 changes: 2 additions & 26 deletions mms-lib/test/resize/test_resize.c
Expand Up @@ -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 <libexif/exif-content.h>
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions rpm/mms-engine.spec
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 29ebd7b

Please sign in to comment.