diff options
author | Andrew Tridgell <tridge@samba.org> | 2007-06-07 22:30:29 +1000 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2007-06-07 22:30:29 +1000 |
commit | 14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0 (patch) | |
tree | 1137f34391f0bbe3ce0aa7a23e2798b78565bdc7 | |
parent | 06a71762a41f8602ccfdf21e6a6bbe884c9d1a4f (diff) | |
download | samba-14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0.tar.gz samba-14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0.tar.xz samba-14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0.zip |
move more util code to lib/util
(This used to be ctdb commit de5ab0584c978a6be4afeacd80c84015b206a3c6)
-rw-r--r-- | ctdb/Makefile.in | 5 | ||||
-rw-r--r-- | ctdb/lib/util/util_file.c | 116 | ||||
-rw-r--r-- | ctdb/lib/util/util_time.c (renamed from ctdb/common/util.c) | 107 |
3 files changed, 119 insertions, 109 deletions
diff --git a/ctdb/Makefile.in b/ctdb/Makefile.in index ee504d037d..887f983af7 100644 --- a/ctdb/Makefile.in +++ b/ctdb/Makefile.in @@ -27,9 +27,10 @@ CFLAGS=-g -I$(srcdir)/include -Iinclude -Ilib -Ilib/util -I$(srcdir) \ LIB_FLAGS=@LDFLAGS@ -Llib @LIBS@ $(POPT_LIBS) @INFINIBAND_LIBS@ -UTIL_OBJ = lib/util/idtree.o lib/util/db_wrap.o lib/util/strlist.o lib/util/util.o +UTIL_OBJ = lib/util/idtree.o lib/util/db_wrap.o lib/util/strlist.o lib/util/util.o \ + lib/util/util_time.o lib/util/util_file.o -CTDB_COMMON_OBJ = common/ctdb_io.o common/util.o common/ctdb_util.o \ +CTDB_COMMON_OBJ = common/ctdb_io.o common/ctdb_util.o \ common/ctdb_ltdb.o common/ctdb_message.o common/cmdline.o \ lib/util/debug.o diff --git a/ctdb/lib/util/util_file.c b/ctdb/lib/util/util_file.c new file mode 100644 index 0000000000..e9050fb25f --- /dev/null +++ b/ctdb/lib/util/util_file.c @@ -0,0 +1,116 @@ +/* + functions taken from samba4 for quick prototyping of ctdb. These are + not intended to remain part of ctdb +*/ + +#include "includes.h" +#include "system/filesys.h" + + +static char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx) +{ + struct stat sbuf; + char *p; + + if (fstat(fd, &sbuf) != 0) return NULL; + + p = (char *)talloc_size(mem_ctx, sbuf.st_size+1); + if (!p) return NULL; + + if (read(fd, p, sbuf.st_size) != sbuf.st_size) { + talloc_free(p); + return NULL; + } + p[sbuf.st_size] = 0; + + if (size) *size = sbuf.st_size; + + return p; +} + + +static char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx) +{ + int fd; + char *p; + + if (!fname || !*fname) return NULL; + + fd = open(fname,O_RDONLY); + if (fd == -1) return NULL; + + p = fd_load(fd, size, mem_ctx); + + close(fd); + + return p; +} + + +/** +parse a buffer into lines +'p' will be freed on error, and otherwise will be made a child of the returned array +**/ +static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx) +{ + int i; + char *s, **ret; + + if (!p) return NULL; + + for (s = p, i=0; s < p+size; s++) { + if (s[0] == '\n') i++; + } + + ret = talloc_array(mem_ctx, char *, i+2); + if (!ret) { + talloc_free(p); + return NULL; + } + + talloc_steal(ret, p); + + memset(ret, 0, sizeof(ret[0])*(i+2)); + if (numlines) *numlines = i; + + ret[0] = p; + for (s = p, i=0; s < p+size; s++) { + if (s[0] == '\n') { + s[0] = 0; + i++; + ret[i] = s+1; + } + if (s[0] == '\r') s[0] = 0; + } + + return ret; +} + + +/** +load a file into memory and return an array of pointers to lines in the file +must be freed with talloc_free(). +**/ +_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx) +{ + char *p; + size_t size; + + p = file_load(fname, &size, mem_ctx); + if (!p) return NULL; + + return file_lines_parse(p, size, numlines, mem_ctx); +} + +char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len) +{ + int i; + char *hex_buffer; + + hex_buffer = talloc_array(mem_ctx, char, (len*2)+1); + + for (i = 0; i < len; i++) + slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]); + + return hex_buffer; +} diff --git a/ctdb/common/util.c b/ctdb/lib/util/util_time.c index 40bc75c3fa..13ddfdb2b6 100644 --- a/ctdb/common/util.c +++ b/ctdb/lib/util/util_time.c @@ -100,110 +100,3 @@ _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs) return timeval_add(&tv, secs, usecs); } -static char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx) -{ - struct stat sbuf; - char *p; - - if (fstat(fd, &sbuf) != 0) return NULL; - - p = (char *)talloc_size(mem_ctx, sbuf.st_size+1); - if (!p) return NULL; - - if (read(fd, p, sbuf.st_size) != sbuf.st_size) { - talloc_free(p); - return NULL; - } - p[sbuf.st_size] = 0; - - if (size) *size = sbuf.st_size; - - return p; -} - - -static char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx) -{ - int fd; - char *p; - - if (!fname || !*fname) return NULL; - - fd = open(fname,O_RDONLY); - if (fd == -1) return NULL; - - p = fd_load(fd, size, mem_ctx); - - close(fd); - - return p; -} - - -/** -parse a buffer into lines -'p' will be freed on error, and otherwise will be made a child of the returned array -**/ -static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx) -{ - int i; - char *s, **ret; - - if (!p) return NULL; - - for (s = p, i=0; s < p+size; s++) { - if (s[0] == '\n') i++; - } - - ret = talloc_array(mem_ctx, char *, i+2); - if (!ret) { - talloc_free(p); - return NULL; - } - - talloc_steal(ret, p); - - memset(ret, 0, sizeof(ret[0])*(i+2)); - if (numlines) *numlines = i; - - ret[0] = p; - for (s = p, i=0; s < p+size; s++) { - if (s[0] == '\n') { - s[0] = 0; - i++; - ret[i] = s+1; - } - if (s[0] == '\r') s[0] = 0; - } - - return ret; -} - - -/** -load a file into memory and return an array of pointers to lines in the file -must be freed with talloc_free(). -**/ -_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx) -{ - char *p; - size_t size; - - p = file_load(fname, &size, mem_ctx); - if (!p) return NULL; - - return file_lines_parse(p, size, numlines, mem_ctx); -} - -char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len) -{ - int i; - char *hex_buffer; - - hex_buffer = talloc_array(mem_ctx, char, (len*2)+1); - - for (i = 0; i < len; i++) - slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]); - - return hex_buffer; -} |