This includes carefulio, mmap, file_size() and sync.
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 \
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 \
+++ /dev/null
-/*
- * 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;
-}
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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);
+}
#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 */
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);
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
+++ /dev/null
-/*
- * 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);
-}
+++ /dev/null
-/*
- * 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);
-}