Permalink
Browse files

Merge branch 'writing'

  • Loading branch information...
2 parents d891a4f + 5156e68 commit 174ea558640a04c9ee83a354a7bd454d6e02b59b @mikejs committed Sep 28, 2009
Showing with 366 additions and 23 deletions.
  1. +1 −1 SConstruct
  2. +67 −0 local_gridfile.cpp
  3. +40 −0 local_gridfile.h
  4. +6 −0 main.cpp
  5. +229 −22 operations.cpp
  6. +13 −0 operations.h
  7. +10 −0 utils.h
View
@@ -39,6 +39,6 @@ conf.CheckLib( makeBoost( "system" ) )
env = conf.Finish()
-files = ['main.cpp', 'operations.cpp', 'options.cpp']
+files = ['main.cpp', 'operations.cpp', 'options.cpp', 'local_gridfile.cpp']
env.Program('mount_gridfs', files)
View
@@ -0,0 +1,67 @@
+#include "local_gridfile.h"
+
+#include <algorithm>
+
+using namespace std;
+
+int LocalGridFile::write(const char *buf, size_t nbyte, off_t offset)
+{
+ int last_chunk = (offset + nbyte) / _chunkSize;
+ int written = 0;
+
+ while(last_chunk > _chunks.size() - 1) {
+ char *new_buf = new char[_chunkSize];
+ memset(new_buf, 0, _chunkSize);
+ _chunks.push_back(new_buf);
+ }
+
+ int chunk_num = offset / _chunkSize;
+ char* dest_buf = _chunks[chunk_num];
+
+ int buf_offset = offset % _chunkSize;
+ if(buf_offset) {
+ dest_buf += offset % _chunkSize;
+ int to_write = min(nbyte - written,
+ (long unsigned int)(_chunkSize - buf_offset));
+ memcpy(dest_buf, buf, to_write);
+ written += to_write;
+ chunk_num++;
+ }
+
+ while(written < nbyte) {
+ dest_buf = _chunks[chunk_num];
+ int to_write = min(nbyte - written,
+ (long unsigned int)_chunkSize);
+ memcpy(dest_buf, buf, to_write);
+ written += to_write;
+ chunk_num++;
+ }
+
+ _length = max(_length, (int)offset + written);
+ _dirty = true;
+
+ return written;
+}
+
+int LocalGridFile::read(char* buf, size_t size, off_t offset)
+{
+ size_t len = 0;
+ int chunk_num = offset / _chunkSize;
+
+ while(len < size && chunk_num < _chunks.size()) {
+ const char* chunk = _chunks[chunk_num];
+ size_t to_read = min((size_t)_chunkSize, size - len);
+
+ if(!len && offset) {
+ chunk += offset % _chunkSize;
+ to_read = min(to_read,
+ (size_t)(_chunkSize - (offset % _chunkSize)));
+ }
+
+ memcpy(buf + len, chunk, to_read);
+ len += to_read;
+ chunk_num++;
+ }
+
+ return len;
+}
View
@@ -0,0 +1,40 @@
+#ifndef _LOCAL_GRIDFILE_H
+#define _LOCAL_GRIDFILE_H
+
+#include <vector>
+#include <cstring>
+#include <iostream>
+
+const unsigned int DEFAULT_CHUNK_SIZE = 256 * 1024;
+
+class LocalGridFile {
+public:
+ LocalGridFile(int chunkSize = DEFAULT_CHUNK_SIZE) :
+ _chunkSize(chunkSize), _length(0), _dirty(true) {
+ _chunks.push_back(new char[_chunkSize]);
+ }
+
+ ~LocalGridFile() {
+ for(std::vector<char*>::iterator i = _chunks.begin();
+ i != _chunks.end(); i++) {
+ delete *i;
+ }
+ }
+
+ int getChunkSize() { return _chunkSize; }
+ int getNumChunks() { return _chunks.size(); }
+ int getLength() { return _length; }
+ char* getChunk(int n) { return _chunks[n]; }
+ bool dirty() { return _dirty; }
+ void flushed() { _dirty = false; }
+
+ int write(const char* buf, size_t nbyte, off_t offset);
+ int read(char* buf, size_t size, off_t offset);
+
+private:
+ int _chunkSize, _length;
+ bool _dirty;
+ std::vector<char*> _chunks;
+};
+
+#endif
View
@@ -28,10 +28,16 @@ int main(int argc, char *argv[])
gridfs_oper.getattr = gridfs_getattr;
gridfs_oper.readdir = gridfs_readdir;
gridfs_oper.open = gridfs_open;
+ gridfs_oper.create = gridfs_create;
+ gridfs_oper.release = gridfs_release;
+ gridfs_oper.unlink = gridfs_unlink;
gridfs_oper.read = gridfs_read;
gridfs_oper.listxattr = gridfs_listxattr;
gridfs_oper.getxattr = gridfs_getxattr;
gridfs_oper.setxattr = gridfs_setxattr;
+ gridfs_oper.write = gridfs_write;
+ gridfs_oper.flush = gridfs_flush;
+ gridfs_oper.rename = gridfs_rename;
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
Oops, something went wrong.

0 comments on commit 174ea55

Please sign in to comment.