From e4f43875ceeb97bf209f0be389b01ffa88f9cfe8 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Wed, 19 Mar 2008 11:02:36 -0700 Subject: [PATCH] bufferlist read_file and write_file --- src/TODO | 1 - src/config.cc | 31 +++++++++++++++++++++++++++++++ src/crush/CrushWrapper.h | 14 ++++++++++++++ src/crush/CrushWrapper.i | 2 +- src/include/buffer.h | 3 +++ src/mkmonfs.cc | 18 +----------------- src/mon/MonMap.cc | 21 ++++----------------- src/osdmaptool.cc | 35 ++++------------------------------- 8 files changed, 58 insertions(+), 67 deletions(-) diff --git a/src/TODO b/src/TODO index f6722aefcb0a9..6a11c36e2cedf 100644 --- a/src/TODO +++ b/src/TODO @@ -3,7 +3,6 @@ code cleanup - use le32 etc annotation - probably kill base case in encoder.h, replace with int types, with appropriate swabbing? - addr=? -- fix ceph_lookup_open kernel client - make sure link/unlink results reflected by inode/dentry cache (let fill_trace do it? invalidate? do actual update?) diff --git a/src/config.cc b/src/config.cc index b3e499cddc5a8..25d7b2d3c67d5 100644 --- a/src/config.cc +++ b/src/config.cc @@ -17,6 +17,7 @@ #include "include/types.h" #include #include +#include // hack hack hack ugly FIXME #include "include/atomic.h" @@ -24,6 +25,36 @@ atomic_t buffer_total_alloc; #include "osd/osd_types.h" +int buffer::list::read_file(const char *fn) +{ + struct stat st; + int fd = ::open(fn, O_RDONLY); + if (fd < 0) { + cerr << "can't open " << fn << ": " << strerror(errno) << std::endl; + return -errno; + } + ::fstat(fd, &st); + bufferptr bp(st.st_size); + append(bp); + ::read(fd, (void*)c_str(), length()); + ::close(fd); + return 0; +} + +int buffer::list::write_file(const char *fn) +{ + int fd = ::open(fn, O_WRONLY|O_CREAT|O_TRUNC, 0644); + if (fd < 0) { + cerr << "can't write " << fn << ": " << strerror(errno) << std::endl; + return -errno; + } + ::write(fd, (void*)c_str(), length()); + ::close(fd); + return 0; +} + + + // debug output Mutex _dout_lock; ostream *_dout = &std::cout; diff --git a/src/crush/CrushWrapper.h b/src/crush/CrushWrapper.h index 9641d1ab337ac..7df9480fb991d 100644 --- a/src/crush/CrushWrapper.h +++ b/src/crush/CrushWrapper.h @@ -296,6 +296,20 @@ public: out[i] = rawout[i]; } + int read_from_file(const char *fn) { + bufferlist bl; + int r = bl.read_file(fn); + if (r < 0) return r; + bufferlist::iterator blp = bl.begin(); + _decode(blp); + return 0; + } + int write_to_file(const char *fn) { + bufferlist bl; + _encode(bl); + return bl.write_file(fn); + } + void _encode(bufferlist &bl, bool lean=false) { ::_encode_simple(crush->max_buckets, bl); ::_encode_simple(crush->max_rules, bl); diff --git a/src/crush/CrushWrapper.i b/src/crush/CrushWrapper.i index 509d217949f96..76340611b3ec7 100644 --- a/src/crush/CrushWrapper.i +++ b/src/crush/CrushWrapper.i @@ -12,7 +12,7 @@ I32 len; int i; SV **tv; - int view; +// int view; //printf("typemap\n"); diff --git a/src/include/buffer.h b/src/include/buffer.h index 1e818a7287312..a6725fc6c7987 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -906,6 +906,9 @@ public: out.unsetf(std::ios::right); } + int read_file(const char *fn); + int write_file(const char *fn); + }; }; diff --git a/src/mkmonfs.cc b/src/mkmonfs.cc index a931bb980ecfd..1b610f86857b8 100644 --- a/src/mkmonfs.cc +++ b/src/mkmonfs.cc @@ -27,22 +27,6 @@ void usage() exit(1); } -int read_file(const char *fn, bufferlist &bl) -{ - struct stat st; - int fd = ::open(fn, O_RDONLY); - if (fd < 0) { - cerr << "can't open " << fn << ": " << strerror(errno) << std::endl; - return -errno; - } - ::fstat(fd, &st); - bufferptr bp(st.st_size); - bl.append(bp); - ::read(fd, (void*)bl.c_str(), bl.length()); - ::close(fd); - return 0; -} - int main(int argc, const char **argv) { @@ -80,7 +64,7 @@ int main(int argc, const char **argv) // load monmap bufferlist monmapbl; - int err = read_file(monmapfn, monmapbl); + int err = monmapbl.read_file(monmapfn); if (err < 0) exit(1); MonMap monmap; diff --git a/src/mon/MonMap.cc b/src/mon/MonMap.cc index 10d4e302726be..bc7711e888647 100644 --- a/src/mon/MonMap.cc +++ b/src/mon/MonMap.cc @@ -13,29 +13,16 @@ int MonMap::write(const char *fn) bufferlist bl; encode(bl); - // write - int fd = ::open(fn, O_RDWR|O_CREAT); - if (fd < 0) return fd; - ::fchmod(fd, 0644); - ::write(fd, (void*)bl.c_str(), bl.length()); - ::close(fd); - return 0; + return bl.write_file(fn); } int MonMap::read(const char *fn) { // read bufferlist bl; - int fd = ::open(fn, O_RDONLY); - if (fd < 0) return -errno; - struct stat st; - ::fstat(fd, &st); - bufferptr bp(st.st_size); - bl.append(bp); - ::read(fd, (void*)bl.c_str(), bl.length()); - ::close(fd); - - // decode + int r = bl.read_file(fn); + if (r < 0) + return r; decode(bl); return 0; } diff --git a/src/osdmaptool.cc b/src/osdmaptool.cc index f10037722354d..f8345e089b65a 100644 --- a/src/osdmaptool.cc +++ b/src/osdmaptool.cc @@ -45,33 +45,6 @@ void printmap(const char *me, OSDMap *m) */ } -int read_file(const char *fn, bufferlist &bl) -{ - struct stat st; - int fd = ::open(fn, O_RDONLY); - if (fd < 0) { - cerr << "can't open " << fn << ": " << strerror(errno) << std::endl; - return -errno; - } - ::fstat(fd, &st); - bufferptr bp(st.st_size); - bl.append(bp); - ::read(fd, (void*)bl.c_str(), bl.length()); - ::close(fd); - return 0; -} - -int write_file(const char *fn, bufferlist &bl) -{ - int fd = ::open(fn, O_WRONLY|O_CREAT|O_TRUNC, 0644); - if (fd < 0) { - cerr << "can't write " << fn << ": " << strerror(errno) << std::endl; - return -errno; - } - ::write(fd, (void*)bl.c_str(), bl.length()); - ::close(fd); - return 0; -} int main(int argc, const char **argv) { @@ -125,7 +98,7 @@ int main(int argc, const char **argv) int r = 0; if (!(createsimple && clobber)) - r = read_file(fn, bl); + r = bl.read_file(fn); if (!createsimple && r < 0) { cerr << me << ": couldn't open " << fn << ": " << strerror(errno) << std::endl; return -1; @@ -148,7 +121,7 @@ int main(int argc, const char **argv) if (import_crush) { bufferlist cbl; - r = read_file(import_crush, cbl); + r = cbl.read_file(import_crush); if (r < 0) { cerr << me << ": error reading crush map from " << import_crush << std::endl; exit(1); @@ -165,7 +138,7 @@ int main(int argc, const char **argv) if (export_crush) { bufferlist cbl; osdmap.crush._encode(cbl); - r = write_file(export_crush, cbl); + r = cbl.write_file(export_crush); if (r < 0) { cerr << me << ": error writing crush map to " << import_crush << std::endl; exit(1); @@ -190,7 +163,7 @@ int main(int argc, const char **argv) cout << me << ": writing epoch " << osdmap.get_epoch() << " to " << fn << std::endl; - int r = write_file(fn, bl); + int r = bl.write_file(fn); assert(r >= 0); } -- 2.39.5