]> mj.ucw.cz Git - libucw.git/commitdiff
More modules moved to io.h and renamed to io-*.c
authorMartin Mares <mj@ucw.cz>
Thu, 9 Feb 2012 22:09:46 +0000 (23:09 +0100)
committerMartin Mares <mj@ucw.cz>
Thu, 9 Feb 2012 22:09:46 +0000 (23:09 +0100)
This includes carefulio, mmap, file_size() and sync.

ucw/Makefile
ucw/carefulio.c [deleted file]
ucw/io-careful.c [new file with mode: 0644]
ucw/io-mmap.c [new file with mode: 0644]
ucw/io-size.c [new file with mode: 0644]
ucw/io-sync.c [new file with mode: 0644]
ucw/io.h
ucw/lib.h
ucw/mmap.c [deleted file]
ucw/sync.c [deleted file]

index 6c44266beba2ea5b54c9143844f2ec2cf3626d18..d33b01fbb3b43ae31f412522f11af1fb9f5699fa 100644 (file)
@@ -10,13 +10,13 @@ endif
 LIBUCW_MODS= \
        threads \
        alloc alloc_str realloc bigalloc mempool mempool-str mempool-fmt eltpool \
-       mmap partmap hashfunc \
+       partmap hashfunc \
        slists simple-lists bitsig \
        log log-stream log-file log-syslog log-conf proctitle tbf \
        conf-alloc conf-dump conf-input conf-intr conf-journal conf-parse conf-section \
        ipaccess \
        fastbuf ff-binary ff-string ff-printf ff-unicode ff-stkstring \
-       fb-file carefulio fb-mem fb-temp tempfile fb-mmap fb-limfd fb-buffer fb-grow fb-pool fb-atomic fb-param fb-socket \
+       fb-file fb-mem fb-temp tempfile fb-mmap fb-limfd fb-buffer fb-grow fb-pool fb-atomic fb-param fb-socket \
        char-cat char-upper char-lower unicode stkstring \
        wildmatch regex \
        prime primetable random timer \
@@ -27,7 +27,7 @@ LIBUCW_MODS= \
        lizard lizard-safe adler32 \
        md5 sha1 sha1-hmac \
        base64 base224 \
-       sync \
+       io-careful io-sync io-mmap io-size \
        qache \
        string str-esc str-split str-match str-imatch str-hex \
        bbuf gary \
diff --git a/ucw/carefulio.c b/ucw/carefulio.c
deleted file mode 100644 (file)
index 44e01e1..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- *     UCW Library -- Careful Read/Write
- *
- *     (c) 2004 Martin Mares <mj@ucw.cz>
- *
- *     This software may be freely distributed and used according to the terms
- *     of the GNU Lesser General Public License.
- */
-
-#include "ucw/lib.h"
-
-#include <unistd.h>
-
-/*
- *  Reads and writes on sockets and pipes can return partial results,
- *  so we implement an iterated read/write call.
- */
-
-int
-careful_read(int fd, void *buf, int len)
-{
-  byte *pos = buf;
-  while (len)
-    {
-      int l = read(fd, pos, len);
-      if (l < 0)
-       return -1;
-      if (!l)
-       return 0;
-      pos += l;
-      len -= l;
-    }
-  return 1;
-}
-
-int
-careful_write(int fd, const void *buf, int len)
-{
-  const byte *pos = buf;
-  while (len)
-    {
-      int l = write(fd, pos, len);
-      if (l < 0)
-       return -1;
-      if (!l)
-       return 0;
-      pos += l;
-      len -= l;
-    }
-  return 1;
-}
diff --git a/ucw/io-careful.c b/ucw/io-careful.c
new file mode 100644 (file)
index 0000000..21b3678
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ *     UCW Library -- Careful Read/Write
+ *
+ *     (c) 2004--2012 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include "ucw/lib.h"
+#include "ucw/io.h"
+
+#include <unistd.h>
+
+/*
+ *  Reads and writes on sockets and pipes can return partial results,
+ *  so we implement an iterated read/write call.
+ */
+
+int
+careful_read(int fd, void *buf, int len)
+{
+  byte *pos = buf;
+  while (len)
+    {
+      int l = read(fd, pos, len);
+      if (l < 0)
+       return -1;
+      if (!l)
+       return 0;
+      pos += l;
+      len -= l;
+    }
+  return 1;
+}
+
+int
+careful_write(int fd, const void *buf, int len)
+{
+  const byte *pos = buf;
+  while (len)
+    {
+      int l = write(fd, pos, len);
+      if (l < 0)
+       return -1;
+      if (!l)
+       return 0;
+      pos += l;
+      len -= l;
+    }
+  return 1;
+}
diff --git a/ucw/io-mmap.c b/ucw/io-mmap.c
new file mode 100644 (file)
index 0000000..30000b0
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *     UCW Library -- Mapping of Files
+ *
+ *     (c) 1999--2012 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include "ucw/lib.h"
+#include "ucw/io.h"
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+void *
+mmap_file(const char *name, unsigned *len, int writeable)
+{
+  int fd = open(name, writeable ? O_RDWR : O_RDONLY);
+  struct stat st;
+  void *x;
+
+  if (fd < 0)
+    die("open(%s): %m", name);
+  if (fstat(fd, &st) < 0)
+    die("fstat(%s): %m", name);
+  if (len)
+    *len = st.st_size;
+  if (st.st_size)
+    {
+      x = mmap(NULL, st.st_size, writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, fd, 0);
+      if (x == MAP_FAILED)
+       die("mmap(%s): %m", name);
+    }
+  else /* For empty file, we can return any non-zero address */
+    x = "";
+  close(fd);
+  return x;
+}
+
+void
+munmap_file(void *start, unsigned len)
+{
+  munmap(start, len);
+}
diff --git a/ucw/io-size.c b/ucw/io-size.c
new file mode 100644 (file)
index 0000000..547aee5
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ *     UCW Library -- File Sizes
+ *
+ *     (c) 1999--2012 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include "ucw/lib.h"
+#include "ucw/io.h"
+
+ucw_off_t ucw_file_size(const char *name)
+{
+  int fd = ucw_open(name, O_RDONLY);
+  if (fd < 0)
+    die("Cannot open %s: %m", name);
+  ucw_off_t len = ucw_seek(fd, 0, SEEK_END);
+  close(fd);
+  return len;
+}
diff --git a/ucw/io-sync.c b/ucw/io-sync.c
new file mode 100644 (file)
index 0000000..08ac93f
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ *     UCW Library -- Syncing Directories
+ *
+ *     (c) 2004--2012 Martin Mares <mj@ucw.cz>
+ */
+
+#include "ucw/lib.h"
+#include "ucw/io.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+void
+sync_dir(const char *name)
+{
+  int fd = open(name, O_RDONLY
+#ifdef CONFIG_LINUX
+               | O_DIRECTORY
+#endif
+);
+  if (fd < 0)
+    goto err;
+  int err = fsync(fd);
+  close(fd);
+  if (err >= 0)
+    return;
+ err:
+  msg(L_ERROR, "Unable to sync directory %s: %m", name);
+}
index 593dcf7d727c196e8587ae7162263061fd610d9a..aa36f936f640763e25810404eb2543bef875ecd8 100644 (file)
--- a/ucw/io.h
+++ b/ucw/io.h
@@ -49,15 +49,22 @@ typedef struct stat ucw_stat_t;
 
 #define HAVE_PREAD
 
-static inline ucw_off_t
-ucw_file_size(const char *name)
-{
-  int fd = ucw_open(name, O_RDONLY);
-  if (fd < 0)
-    die("Cannot open %s: %m", name);
-  ucw_off_t len = ucw_seek(fd, 0, SEEK_END);
-  close(fd);
-  return len;
-}
+/* io-size.c */
+
+ucw_off_t ucw_file_size(const char *name);
+
+/* io-mmap.c */
+
+void *mmap_file(const char *name, unsigned *len, int writeable);
+void munmap_file(void *start, unsigned len);
+
+/* io-careful.c */
+
+int careful_read(int fd, void *buf, int len);
+int careful_write(int fd, const void *buf, int len);
+
+/* io-sync.c */
+
+void sync_dir(const char *name);
 
 #endif /* !_UCW_LFS_H */
index ec984888d2287d8ac42a9765da1b9e0c24c35a41..2b76b7a37d5e02c457928bb29021125759f433ca 100644 (file)
--- a/ucw/lib.h
+++ b/ucw/lib.h
@@ -181,11 +181,6 @@ uns random_max(uns max);                   /** Return a pseudorandom 32-bit number in range [0,@
 u64 random_u64(void);                          /** Return a pseudorandom 64-bit number. **/
 u64 random_max_u64(u64 max);                   /** Return a pseudorandom 64-bit number in range [0,@max). **/
 
-/* mmap.c */
-
-void *mmap_file(const char *name, unsigned *len, int writeable);
-void munmap_file(void *start, unsigned len);
-
 /* proctitle.c */
 
 void setproctitle_init(int argc, char **argv);
@@ -206,15 +201,6 @@ int run_command_v(const char *cmd, va_list args);
 void NONRET exec_command_v(const char *cmd, va_list args);
 void echo_command_v(char *buf, int size, const char *cmd, va_list args);
 
-/* carefulio.c */
-
-int careful_read(int fd, void *buf, int len);
-int careful_write(int fd, const void *buf, int len);
-
-/* sync.c */
-
-void sync_dir(const char *name);
-
 /* sighandler.c */
 
 typedef int (*ucw_sighandler_t)(int);  // gets signum, returns nonzero if abort() should be called
diff --git a/ucw/mmap.c b/ucw/mmap.c
deleted file mode 100644 (file)
index 6b98236..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- *     UCW Library -- Mapping of Files
- *
- *     (c) 1999--2002 Martin Mares <mj@ucw.cz>
- *
- *     This software may be freely distributed and used according to the terms
- *     of the GNU Lesser General Public License.
- */
-
-#include "ucw/lib.h"
-
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-
-void *
-mmap_file(const char *name, unsigned *len, int writeable)
-{
-  int fd = open(name, writeable ? O_RDWR : O_RDONLY);
-  struct stat st;
-  void *x;
-
-  if (fd < 0)
-    die("open(%s): %m", name);
-  if (fstat(fd, &st) < 0)
-    die("fstat(%s): %m", name);
-  if (len)
-    *len = st.st_size;
-  if (st.st_size)
-    {
-      x = mmap(NULL, st.st_size, writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, fd, 0);
-      if (x == MAP_FAILED)
-       die("mmap(%s): %m", name);
-    }
-  else /* For empty file, we can return any non-zero address */
-    x = "";
-  close(fd);
-  return x;
-}
-
-void
-munmap_file(void *start, unsigned len)
-{
-  munmap(start, len);
-}
diff --git a/ucw/sync.c b/ucw/sync.c
deleted file mode 100644 (file)
index 0f3ef78..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- *     UCW Library -- Syncing Directories
- *
- *     (c) 2004--2005 Martin Mares <mj@ucw.cz>
- */
-
-#include "ucw/lib.h"
-
-#include <fcntl.h>
-#include <unistd.h>
-
-void
-sync_dir(const char *name)
-{
-  int fd = open(name, O_RDONLY
-#ifdef CONFIG_LINUX
-               | O_DIRECTORY
-#endif
-);
-  if (fd < 0)
-    goto err;
-  int err = fsync(fd);
-  close(fd);
-  if (err >= 0)
-    return;
- err:
-  msg(L_ERROR, "Unable to sync directory %s: %m", name);
-}