]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
bufferlist read_file and write_file
authorSage Weil <sage@newdream.net>
Wed, 19 Mar 2008 18:02:36 +0000 (11:02 -0700)
committerSage Weil <sage@newdream.net>
Wed, 19 Mar 2008 18:02:36 +0000 (11:02 -0700)
src/TODO
src/config.cc
src/crush/CrushWrapper.h
src/crush/CrushWrapper.i
src/include/buffer.h
src/mkmonfs.cc
src/mon/MonMap.cc
src/osdmaptool.cc

index f6722aefcb0a93463a13b9a28cda806dcf85b68f..6a11c36e2cedf191cafc59f32b7a8613094ace39 100644 (file)
--- 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?)
index b3e499cddc5a8fa79bf0fa9e712a7d29544f2d37..25d7b2d3c67d5214577e9c8d46b07469ec20241f 100644 (file)
@@ -17,6 +17,7 @@
 #include "include/types.h"
 #include <fstream>
 #include <stdlib.h>
+#include <errno.h>
 
 // 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;
index 9641d1ab337ac064ab4d033995b9e704dd43c4f3..7df9480fb991d12b0c9727c247ecdbdb27ed1e65 100644 (file)
@@ -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);
index 509d217949f96ad463ef0adef16b49c8e0336f68..76340611b3ec79e0e930b20e15b16d2d7522f3c1 100644 (file)
@@ -12,7 +12,7 @@
   I32 len;
   int i;
   SV **tv;
-  int view;
+//  int view;
 
 
   //printf("typemap\n");
index 1e818a7287312304ac256b95d49fa18b42be8677..a6725fc6c798775dfa6869b39697a66652ca732b 100644 (file)
@@ -906,6 +906,9 @@ public:
       out.unsetf(std::ios::right);
     }
 
+    int read_file(const char *fn);
+    int write_file(const char *fn);
+
   };
 };
 
index a931bb980ecfd5385dc88149cc04b213a943d15f..1b610f86857b88d4f3b8c7789a97d4034b5f3551 100644 (file)
@@ -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;
index 10d4e302726be71dfe32a4cb4ca372f74e307469..bc7711e88864704b365f09dab5285ce83734b8d0 100644 (file)
@@ -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;
 }
index f10037722354d45099635f1398bc5c5426c2a922..f8345e089b65aa1b05767661dd51f891f11d6b09 100644 (file)
@@ -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);
   }