summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/base64.c42
-rw-r--r--src/base64.h2
-rw-r--r--src/trg-file-parser.c41
-rw-r--r--src/trg-file-parser.h2
4 files changed, 23 insertions, 64 deletions
diff --git a/src/base64.c b/src/base64.c
index 99e904f..1e09ee0 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -17,49 +17,29 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
-#include <errno.h>
#include <glib.h>
+#include <glib/gstdio.h>
#include "base64.h"
-#define my_print_errno(x) printf("%s: error (%d) %s\n", __func__, errno, x);
-
-char *base64encode(char *filename)
+gchar *base64encode(const gchar *filename)
{
- gint fd = open(filename, O_RDONLY);
- struct stat sb;
- void *addr;
+ GError *error = NULL;
+ GMappedFile *mf = g_mapped_file_new(filename, FALSE, &error);
gchar *b64out = NULL;
- if (fd < 0) {
- my_print_errno("opening file");
- return NULL;
- }
-
- if (fstat(fd, &sb) == -1) {
- my_print_errno("on fstat");
- close(fd);
- return NULL;
- }
-
- addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (addr == MAP_FAILED) {
- my_print_errno("on mmap");
- close(fd);
- return NULL;
+ if (error)
+ {
+ g_error(error->message);
+ g_error_free(error);
+ } else {
+ b64out = g_base64_encode((guchar*)g_mapped_file_get_contents(mf), g_mapped_file_get_length(mf));
}
- b64out = g_base64_encode((guchar *) addr, sb.st_size);
- munmap(addr, sb.st_size);
- close(fd);
+ g_mapped_file_unref(mf);
return b64out;
}
diff --git a/src/base64.h b/src/base64.h
index e47bf62..ab84071 100644
--- a/src/base64.h
+++ b/src/base64.h
@@ -20,6 +20,6 @@
#ifndef BASE64_H_
#define BASE64_H_
-char *base64encode(char *filename);
+gchar *base64encode(const gchar *filename);
#endif /* BASE64_H_ */
diff --git a/src/trg-file-parser.c b/src/trg-file-parser.c
index 9bb9d67..99f230b 100644
--- a/src/trg-file-parser.c
+++ b/src/trg-file-parser.c
@@ -1,19 +1,12 @@
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
-#include <errno.h>
#include <glib.h>
+#include <glib/gstdio.h>
#include "bencode.h"
#include "trg-file-parser.h"
-#define my_print_errno(x) printf("%s: error (%d) %s\n", __func__, errno, x);
-
static trg_torrent_file_node
* trg_torrent_file_node_insert(trg_torrent_file_node * top,
be_node * file_node, guint index,
@@ -113,37 +106,23 @@ static trg_torrent_file_node *trg_parse_torrent_file_nodes(be_node *
return top_node;
}
-trg_torrent_file *trg_parse_torrent_file(char *filename)
+trg_torrent_file *trg_parse_torrent_file(const gchar *filename)
{
- int fd;
- struct stat sb;
- void *addr;
+ GError *error = NULL;
+ GMappedFile *mf;
be_node *top_node, *info_node, *name_node;
trg_torrent_file *ret = NULL;
- fd = open(filename, O_RDONLY);
- if (fd < 0) {
- my_print_errno("opening file");
- return NULL;
- }
+ mf = g_mapped_file_new(filename, FALSE, &error);
- if (fstat(fd, &sb) == -1) {
- my_print_errno("on fstat");
- close(fd);
+ if (error) {
+ g_error(error->message);
+ g_error_free(error);
return NULL;
+ } else {
+ top_node = be_decoden(g_mapped_file_get_contents(mf), g_mapped_file_get_length(mf));
}
- addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (addr == MAP_FAILED) {
- my_print_errno("on mmap");
- close(fd);
- return NULL;
- }
-
- top_node = be_decoden((char *) addr, sb.st_size);
- munmap(addr, sb.st_size);
- close(fd);
-
if (!top_node) {
return NULL;
} else if (be_validate_node(top_node, BE_DICT)) {
diff --git a/src/trg-file-parser.h b/src/trg-file-parser.h
index 0958b9d..a7781f1 100644
--- a/src/trg-file-parser.h
+++ b/src/trg-file-parser.h
@@ -12,4 +12,4 @@ typedef struct {
} trg_torrent_file;
void trg_torrent_file_free(trg_torrent_file * t);
-trg_torrent_file *trg_parse_torrent_file(char *filename);
+trg_torrent_file *trg_parse_torrent_file(const gchar *filename);