commit 2785164f532c7f95d2468bef65ee309336e4bc93
parent f450209a3a8f19f21652cb32dd4ff1ce1efdd8fd
Author: sin <sin@2f30.org>
Date: Thu, 21 Feb 2019 21:04:17 +0000
Put some funcs in utils.c
Diffstat:
M | Makefile | | | 6 | +++--- |
M | dedup.c | | | 60 | ------------------------------------------------------------ |
M | dedup.h | | | 6 | ++++++ |
A | utils.c | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 76 insertions(+), 63 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,8 +2,8 @@ VERSION = 0.3
PREFIX = /usr/local
MANPREFIX = $(PREFIX)/man
BIN = dedup
-SRC = $(BIN).c chunker.c hash.c pack.c unpack.c
-OBJ = $(BIN).o chunker.o hash.o pack.o unpack.o
+SRC = $(BIN).c chunker.c hash.c pack.c unpack.c utils.c
+OBJ = $(BIN).o chunker.o hash.o pack.o unpack.o utils.o
DISTFILES = \
$(SRC) \
LICENSE \
@@ -13,7 +13,7 @@ DISTFILES = \
config.h \
$(BIN).1 \
dedup.h \
- tree.h
+ tree.h \
CFLAGS = -g -Wall
CPPFLAGS = -I/usr/local/include -D_FILE_OFFSET_BITS=64
diff --git a/dedup.c b/dedup.c
@@ -143,66 +143,6 @@ print_stats(struct stats *st)
fprintf(stderr, "cache misses: %llu\n", cache_misses);
}
-void
-str2bin(char *s, uint8_t *d)
-{
- size_t i, size = strlen(s) / 2;
-
- for (i = 0; i < size; i++, s += 2)
- sscanf(s, "%2hhx", &d[i]);
-}
-
-off_t
-xlseek(int fd, off_t offset, int whence)
-{
- off_t ret;
-
- ret = lseek(fd, offset, whence);
- if (ret < 0)
- err(1, "lseek");
- return ret;
-}
-
-ssize_t
-xread(int fd, void *buf, size_t nbytes)
-{
- uint8_t *bp = buf;
- ssize_t total = 0;
-
- while (nbytes > 0) {
- ssize_t n;
-
- n = read(fd, &bp[total], nbytes);
- if (n < 0)
- err(1, "read");
- else if (n == 0)
- return total;
- total += n;
- nbytes -= n;
- }
- return total;
-}
-
-ssize_t
-xwrite(int fd, const void *buf, size_t nbytes)
-{
- const uint8_t *bp = buf;
- ssize_t total = 0;
-
- while (nbytes > 0) {
- ssize_t n;
-
- n = write(fd, &bp[total], nbytes);
- if (n < 0)
- err(1, "write");
- else if (n == 0)
- return total;
- total += n;
- nbytes -= n;
- }
- return total;
-}
-
int
cache_entry_cmp(struct cache_entry *e1, struct cache_entry *e2)
{
diff --git a/dedup.h b/dedup.h
@@ -18,3 +18,9 @@ int pack(unsigned char *dst, char *fmt, ...);
/* unpack.c */
int unpack(unsigned char *src, char *fmt, ...);
+
+/* utils.c */
+void str2bin(char *s, uint8_t *d);
+off_t xlseek(int fd, off_t offset, int whence);
+ssize_t xread(int fd, void *buf, size_t nbytes);
+ssize_t xwrite(int fd, const void *buf, size_t nbytes);
diff --git a/utils.c b/utils.c
@@ -0,0 +1,67 @@
+#include <sys/types.h>
+
+#include <err.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+void
+str2bin(char *s, uint8_t *d)
+{
+ size_t i, size = strlen(s) / 2;
+
+ for (i = 0; i < size; i++, s += 2)
+ sscanf(s, "%2hhx", &d[i]);
+}
+
+off_t
+xlseek(int fd, off_t offset, int whence)
+{
+ off_t ret;
+
+ ret = lseek(fd, offset, whence);
+ if (ret < 0)
+ err(1, "lseek");
+ return ret;
+}
+
+ssize_t
+xread(int fd, void *buf, size_t nbytes)
+{
+ uint8_t *bp = buf;
+ ssize_t total = 0;
+
+ while (nbytes > 0) {
+ ssize_t n;
+
+ n = read(fd, &bp[total], nbytes);
+ if (n < 0)
+ err(1, "read");
+ else if (n == 0)
+ return total;
+ total += n;
+ nbytes -= n;
+ }
+ return total;
+}
+
+ssize_t
+xwrite(int fd, const void *buf, size_t nbytes)
+{
+ const uint8_t *bp = buf;
+ ssize_t total = 0;
+
+ while (nbytes > 0) {
+ ssize_t n;
+
+ n = write(fd, &bp[total], nbytes);
+ if (n < 0)
+ err(1, "write");
+ else if (n == 0)
+ return total;
+ total += n;
+ nbytes -= n;
+ }
+ return total;
+}