From: Martin Mares Date: Thu, 9 Feb 2012 22:09:46 +0000 (+0100) Subject: More modules moved to io.h and renamed to io-*.c X-Git-Tag: v5.0~88 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=6e1e67a5221c1c31c8dffe8705e6a450f302ba8b;p=libucw.git More modules moved to io.h and renamed to io-*.c This includes carefulio, mmap, file_size() and sync. --- diff --git a/ucw/Makefile b/ucw/Makefile index 6c44266b..d33b01fb 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -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 index 44e01e14..00000000 --- a/ucw/carefulio.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * UCW Library -- Careful Read/Write - * - * (c) 2004 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include "ucw/lib.h" - -#include - -/* - * 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 index 00000000..21b3678e --- /dev/null +++ b/ucw/io-careful.c @@ -0,0 +1,52 @@ +/* + * UCW Library -- Careful Read/Write + * + * (c) 2004--2012 Martin Mares + * + * 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 + +/* + * 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 index 00000000..30000b00 --- /dev/null +++ b/ucw/io-mmap.c @@ -0,0 +1,48 @@ +/* + * UCW Library -- Mapping of Files + * + * (c) 1999--2012 Martin Mares + * + * 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 +#include +#include +#include +#include + +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 index 00000000..547aee56 --- /dev/null +++ b/ucw/io-size.c @@ -0,0 +1,21 @@ +/* + * UCW Library -- File Sizes + * + * (c) 1999--2012 Martin Mares + * + * 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 index 00000000..08ac93fa --- /dev/null +++ b/ucw/io-sync.c @@ -0,0 +1,29 @@ +/* + * UCW Library -- Syncing Directories + * + * (c) 2004--2012 Martin Mares + */ + +#include "ucw/lib.h" +#include "ucw/io.h" + +#include +#include + +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); +} diff --git a/ucw/io.h b/ucw/io.h index 593dcf7d..aa36f936 100644 --- 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 */ diff --git a/ucw/lib.h b/ucw/lib.h index ec984888..2b76b7a3 100644 --- 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 index 6b982369..00000000 --- a/ucw/mmap.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * UCW Library -- Mapping of Files - * - * (c) 1999--2002 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include "ucw/lib.h" - -#include -#include -#include -#include -#include - -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 index 0f3ef788..00000000 --- a/ucw/sync.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * UCW Library -- Syncing Directories - * - * (c) 2004--2005 Martin Mares - */ - -#include "ucw/lib.h" - -#include -#include - -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); -}