From: Vyacheslav Cherkashin Date: Fri, 22 Sep 2017 16:08:45 +0000 (+0300) Subject: Implement 'file_buffer' X-Git-Tag: submit/tizen_4.0/20170929.085108^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F28%2F152628%2F2;p=platform%2Fcore%2Fsystem%2Fswap-probe.git Implement 'file_buffer' It is used to read data from file to file_buffer struct Change-Id: I1dbb6b76850d21b7e36b8d69e2f70fb6c0e71efd Signed-off-by: Vyacheslav Cherkashin --- diff --git a/Makefile b/Makefile index 914511f..adec95c 100644 --- a/Makefile +++ b/Makefile @@ -119,6 +119,7 @@ UTILITY_SRCS = \ ./helper/loader.c \ ./helper/got_patching.c \ ./helper/get_got.c \ + ./helper/file_buffer.c \ ./helper/screenshot_iface.c PROBE_SRCS = \ diff --git a/helper/file_buffer.c b/helper/file_buffer.c new file mode 100644 index 0000000..acf5271 --- /dev/null +++ b/helper/file_buffer.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Vyacheslav Cherkashin + * + * 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. + * + * Contributors: + * - Samsung RnD Institute Russia + * + */ + + +#include +#include +#include +#include +#include +#include +#include "file_buffer.h" +#include "daprobe.h" + + +static size_t fsize(int fd) +{ + struct stat st; + if (fstat(fd, &st) != 0) + return 0; + return st.st_size; +} + +static int do_read_file(int fd, void *data, size_t len) +{ + while (len) { + ssize_t n = read(fd, data, len); + if (n == -1) { + if (errno == EINTR) + continue; + + return -errno; + } else if (n == 0) { + return -EPERM; + } + + len -= n; + data += n; + } + + return 0; +} + +struct file_buffer *file_buffer_create(const char *path) +{ + int fd, ret; + struct file_buffer *fb; + const size_t MAX_FILE_SIZE = 32 * 1024 * 1024; + + fb = malloc(sizeof(*fb)); + if (!fb) { + PRINTERR("Out of memory"); + return NULL; + } + + fd = open(path, O_RDONLY); + if (fd == -1) { + PRINTERR("Cannot open '%s', errno=%d", path, fd); + goto free_fb; + } + + fb->size = fsize(fd); + if (!fb->size) { + PRINTERR("Cannot calculate file size, path='%s'", path); + goto close_fd; + } + + if (fb->size > MAX_FILE_SIZE) { + PRINTERR("File size is very long, size=%zu", fb->size); + goto close_fd; + } + + fb->data = malloc(fb->size); + if (!fb->data) { + PRINTERR("Out of memory"); + goto close_fd; + } + + ret = do_read_file(fd, fb->data, fb->size); + if (ret) { + PRINTERR("Cannot read file, ret=%d", ret); + goto free_data; + } + + close(fd); + return fb; + +free_data: + free(fb->data); +close_fd: + close(fd); +free_fb: + free(fb); + return NULL; +} + +void file_buffer_destroy(struct file_buffer *fb) +{ + free(fb->data); + free(fb); +} diff --git a/include/file_buffer.h b/include/file_buffer.h new file mode 100644 index 0000000..45b7934 --- /dev/null +++ b/include/file_buffer.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Vyacheslav Cherkashin + * + * 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. + * + * Contributors: + * - Samsung RnD Institute Russia + * + */ + + +#ifndef FILE_BUFFER_H +#define FILE_BUFFER_H + + +#include + + +struct file_buffer { + size_t size; + void *data; +}; + + +struct file_buffer *file_buffer_create(const char *path); +void file_buffer_destroy(struct file_buffer *fb); + + +#endif // FILE_BUFFER_H diff --git a/probe_screenshot/dacapture_wayland.c b/probe_screenshot/dacapture_wayland.c index 84580e1..dbd6d20 100755 --- a/probe_screenshot/dacapture_wayland.c +++ b/probe_screenshot/dacapture_wayland.c @@ -62,6 +62,7 @@ #include "dahelper.h" #include "binproto.h" +#include "file_buffer.h" #define MAX_HEIGHT 720 #define CAPTURE_TIMEOUT 2.0 @@ -624,99 +625,6 @@ void current_angle_set(int angle) } -struct file_data { - size_t len; - void *data; -}; - -static size_t fsize(int fd) -{ - struct stat st; - if (fstat(fd, &st) != 0) - return 0; - return st.st_size; -} - -static int do_read_file(int fd, void *data, size_t len) -{ - while (len) { - ssize_t n = read(fd, data, len); - if (n == -1) { - if (errno == EINTR) - continue; - - return -errno; - } else if (n == 0) { - return -EPERM; - } - - len -= n; - data += n; - } - - return 0; -} - -static struct file_data *file_data_create(const char *path) -{ - int fd, ret; - struct file_data *fdata; - const size_t MAX_FILE_SIZE = 32 * 1024 * 1024; - - fdata = malloc(sizeof(*fdata)); - if (!fdata) { - PRINTERR("Out of memory"); - return NULL; - } - - fd = open(path, O_RDONLY); - if (fd == -1) { - PRINTERR("Cannot open '%s', errno=%d", path, fd); - goto free_fdata; - } - - fdata->len = fsize(fd); - if (!fdata->len) { - PRINTERR("Cannot calculate file size, path='%s'", path); - goto close_fd; - } - - if (fdata->len > MAX_FILE_SIZE) { - PRINTERR("file size is very long, len=%zu", fdata->len); - goto close_fd; - } - - fdata->data = malloc(fdata->len); - if (!fdata->data) { - PRINTERR("Out of memory"); - goto close_fd; - } - - ret = do_read_file(fd, fdata->data, fdata->len); - if (ret) { - PRINTERR("Cannot read file, ret=%d", ret); - goto free_data; - } - - close(fd); - return fdata; - -free_data: - free(fdata->data); -close_fd: - close(fd); -free_fdata: - free(fdata); - return NULL; -} - -static void file_data_free(struct file_data *data) -{ - free(data->data); - free(data); -} - - static size_t screenshot_pack_info(char *buf) { /* pack probe */ @@ -730,13 +638,13 @@ static size_t screenshot_pack_info(char *buf) static int screenshot_send_to_socket(const char *path) { - struct file_data *fdata; + struct file_buffer *fb; char sh_info[256]; void *buf, *p; size_t sh_info_len, buf_size; - fdata = file_data_create(path); - if (!fdata) + fb = file_buffer_create(path); + if (!fb) return -1; /* pack common colums */ @@ -750,11 +658,11 @@ static int screenshot_send_to_socket(const char *path) * | 4 | ... | 4 | ... | * */ - buf_size = 4 + sh_info_len + 4 + fdata->len; + buf_size = 4 + sh_info_len + 4 + fb->size; buf = malloc(buf_size); if (!buf) { PRINTERR("Out of memory"); - file_data_free(fdata); + file_buffer_destroy(fb); return -1; } @@ -766,12 +674,12 @@ static int screenshot_send_to_socket(const char *path) p = pack_int32(p, current_angle_get()); /* pack img.png */ - p = pack_bin(p, fdata->data, fdata->len); + p = pack_bin(p, fb->data, fb->size); /* send screenshot to manager */ msg_send(APP_MSG_IMAGE, buf, buf_size); free(buf); - file_data_free(fdata); + file_buffer_destroy(fb); return 0; }