tfs 的读写操作代码

链接是我写的读写打包的代码

http://pan.baidu.com/s/1o6mI4DO

1. include 头文件

/*************************************************************************
	> File Name: basetype.h
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> Blog: http://blog.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月22日 星期二 09时31分47秒
 ************************************************************************/

#ifndef _MY_BASETYPE_H_
#define _MY_BASETYPE_H_

#define  int32   int
 #define uint32  unsigned int

#endif

/*************************************************************************
	> File Name: image_reader.h
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> Blog: http://blog.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月22日 星期二 13时16分29秒
 ************************************************************************/

#ifndef _MY_IMAGE_READER_H_
#define _MY_IMAGE_READER_H_

#include "basetype.h"
#include <iostream>
#include <stdint.h>

namespace tfs {

    namespace client {

        class TfsClient;
    }
}

namespace image {

    class ImageReader {

        private:
            tfs::client::TfsClient  *tfs_client_;
            int fd_;

        public:
            ImageReader ();
            ~ImageReader ();

            bool Connect (const char*ns_ip_port) const ;
            bool Open (const char *TFS_filename);
            int64_t GetSize (void) const ;
            int64_t Read (void *buffer, const int64_t buffer_size) const ;
            void Close (void);

            /**
            * @brief : read whole file include open and close operator
            *
            * @param TFS_filename : file in tfs 
            * @param buffer : buffer
            *
            * @return : size to be read , -1 other
            */
            bool ReadWholeFile (const char *TFS_filename, std::string *buffer);

            void Disconnect (void);
    };
}

#endif

/*************************************************************************
	> File Name: image_writer.h
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> Blog: http://blog.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月21日 星期一 17时18分28秒
 ************************************************************************/

#ifndef _IMAGE_WRITER_H_
#define _IMAGE_WRITER_H_

#include "basetype.h"
#include <stdint.h>
#include <iostream>

namespace tfs {

    namespace client {
        class TfsClient;
    }
}

namespace image {

    class ImageWriter {

    private:
        tfs::client::TfsClient  *tfs_client_;


        /**
        * @brief : internal write, private 
        *
        * @param fd : file descriptor 
        * @param buffer : will be write content
        * @param length : length of buffer 
        *
        * @return : failuer -1 or size that was writen
        */
        int32 WriteInternal (const int fd, const char* buffer, uint32 length) const ;

    public:
        ImageWriter ();
        ~ImageWriter ();

        /**
        * @brief : connect to nameserver
        *
        * @param ns_ip_port : ip and port of nameserver 
        *
        * @return : success true or false
        */
        bool Connect (const char *ns_ip_port) const;

        /**
        * @brief : write content to tfs 
        *
        * @param TFS_filename : tfs filename 
        * @param ret_filename : filename generated by tfs
        * @param buffer : content to be writen to tfs 
        * @param length : content length
        *
        * @return : the content length if success, -1 otherwise
        */
        int32 Write (const char *TFS_filename, std::string *ret_filename, const char *buffer, uint32 length) const;

        int32 Write (std::string *ret_filename, const char *buffer, uint32 length) const {
            
            return Write (NULL, ret_filename, buffer, length);
        }

        int32 Write (std::string *ret_filename, const std::string &buffer) const {

            return Write (NULL, ret_filename, buffer.c_str (), buffer.length ());
        }

        int32 WriteWholeFile (const char *TFS_filename, std::string *ret_filename, const char *filename) const;

        /**
        * @brief : overwrite file new content 
        *
        * @param TFS_filename : tfs filename 
        * @param buffer : content to be writen to tfs 
        * @param length : content length
        *
        * @return : the length if success, -1 otherwise
        */
        int32 Overwrite (const std::string &TFS_filename, const char*buffer, uint32 length) const {
        
            return Write (TFS_filename.c_str (), NULL, buffer, length);
        }

        int32 Overwrite (const std::string &TFS_filename, const std::string &buffer) const {
            
            return Overwrite (TFS_filename, buffer.c_str (), buffer.length ());
        }


        /**
        * @brief : delete file from tfs 
        *
        * @param TFS_filename : TFS filename
        *
        * @return : success or fail
        */
        int32 Remove (const std::string &TFS_filename) const ; 

        void Disconnect (void);
    };
}

#endif

/*************************************************************************
	> File Name: logging.h
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> Blog: http://blog.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月21日 星期一 19时45分09秒
 ************************************************************************/

#ifndef _LOGGING_H_
#define _LOGGING_H_

#include <iostream>
#include <fstream>
#include <ctime>

// ofstream    log_out_("/usr/local/tfs/logs/mylog.log");

#define ERROR   cout
#define LOG(out)    (std::out)

#endif

src 代码


/*************************************************************************
	> File Name: image_reader.cpp
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> Blog: http://blog.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月22日 星期二 13时23分02秒
 ************************************************************************/

#include <iostream>
#include <stdint.h>
#include "image_reader.h"
#include "logging.h"
#include "/var/yr/tfs_ds/dxg/include/tfs/new_client/tfs_client_api.h"

namespace image {

    ImageReader::ImageReader () {

        tfs_client_ = tfs::client::TfsClient::Instance ();
        fd_ = -1;
    }

    ImageReader::~ImageReader () {

        Disconnect ();
    }

    bool ImageReader::Connect (const char *ns_ip_port) const {

        int ret = tfs_client_->initialize (ns_ip_port);
        if (tfs::common::TFS_SUCCESS != ret) {

            LOG(ERROR) << "tfs initialize failure: " << ret << std::endl;
            return false;
        }
        return true;
    }

    bool ImageReader::Open (const char *TFS_filename) {

        int fd = tfs_client_->open (TFS_filename, NULL, tfs::common::T_READ);
        if (0 > fd) {
            
            LOG (ERROR) << "failed to open for reading." << TFS_filename << std::endl;
            return false;
        }

        fd_ = fd;
        return true;
    }

    int64_t ImageReader::GetSize (void) const {

        int64_t ret = tfs_client_->get_file_length (fd_);
        if (0 > ret) {

            LOG (ERROR) << "failed to get file size ret=" << ret;
        }
        return ret;
    }

    int64_t ImageReader::Read (void *buffer, const int64_t buffer_size) const {

        char    *buf = reinterpret_cast<char*>(buffer);
        int64_t read = 0;

        while (buffer_size > read) {
            
            int ret = tfs_client_->read (fd_, buf + read, buffer_size - read);

            if (0 > ret) {
                
                LOG (ERROR) << "failed to read" << std::endl;
                return -1;
            }else {

                read += ret;
            }
        }
        return read;
    }

    void ImageReader::Close (void) {
        
        if (-1 != fd_) {

            tfs_client_->close (fd_);
            fd_ = -1;
        }
    }

    bool ImageReader::ReadWholeFile (const char*TFS_filename, std::string *buffer) {

        if (false == Open (TFS_filename)) {

            return false;
        }

        const int64_t max_to_read = GetSize ();
        if (0 >= max_to_read) {

            return false;
        }

        buffer->resize (max_to_read, 0); 
        int64_t read = Read (const_cast<char*>(buffer->data ()), max_to_read);

        Close ();

        if (max_to_read != read) {
            
            buffer->clear ();
            return false;
        }
        return true;
    }

    void ImageReader::Disconnect (void) {
        
        tfs_client_->destroy ();
    }
}

/*************************************************************************
	> File Name: image_writer.cpp
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> BLOG: http://bLOG.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月21日 星期一 17时53分50秒
 ************************************************************************/

#include <iostream>
#include <fstream>
#include "/var/yr/tfs_ds/dxg/include/tfs/new_client/tfs_client_api.h"
#include "image_writer.h"
#include "logging.h"

namespace image {

    ImageWriter::ImageWriter () {

        /** 创建一个tfs端口 */
        tfs_client_ = tfs::client::TfsClient::Instance ();
    }

    ImageWriter::~ImageWriter () {

        Disconnect ();
    }

    bool ImageWriter::Connect (const char *ns_ip_port) const {
        
        int ret = tfs_client_->initialize (ns_ip_port);
        if (tfs::common::TFS_SUCCESS != ret) {

            LOG(ERROR) << "tfs initialize failure: " << ret << std::endl;
            return false;
        }

        return true;
    }

    int32 ImageWriter::Write (const char*TFS_filename, std::string *ret_filename, const char*buffer, uint32 length) const {

        if (NULL == tfs_client_) {
            
            LOG(ERROR) << "Write : tfsclient initialize fial." << std::endl;
            return -1;
        }

        if (NULL != ret_filename) {
            
            ret_filename->clear ();
        }

        int fd = tfs_client_->open (TFS_filename, NULL, tfs::common::T_WRITE);
        if (0 > fd) {
            
            LOG (ERROR) << "failed to open for writing." << std::endl;
            return -1;
        }

        uint32 written = WriteInternal (fd, buffer, length); 

        if (0 > written) {
            
            tfs_client_->close (fd);
            LOG (ERROR) << "failed to write." << std::endl;
            return -1;
        }

        if (NULL != ret_filename) {

            const int file_name_len = 20;
            char file_name[file_name_len+1] = {0};
            int ret = tfs_client_->close (fd, file_name, file_name_len);

            if (ret != tfs::common::TFS_SUCCESS) {
                
                LOG (ERROR) << "fialed to close" << std::endl;
                return -1;
            }

            ret_filename->assign (file_name);
        }else {
            
            int ret = tfs_client_->close (fd);
            if (ret != tfs::common::TFS_SUCCESS) {
                
                LOG (ERROR) << "fialed to close" << std::endl;
                return -1;
            }
        }
        return written;
    }

    int32 ImageWriter::WriteWholeFile (const char *TFS_filename, std::string *ret_filename, const char *filename) const {
        
        std::ifstream fin;
        fin.open (filename, std::ios::in|std::ios::ate);

        if ( !fin ) {
            
            LOG (ERROR) << "failed to open file." << std::endl;
            return -1;
        }

        /* 读取文件中实际的字符数,-1 去掉文件结束符 */
        int64_t file_size = fin.tellg ();
        file_size -= 1;
        if (0 >= file_size) {
            
            LOG (ERROR) << "failed to tellg." << std::endl;
            fin.close ();
            return -1;
        }
        fin.seekg (std::ios::beg);

        std::string buffer (file_size+1, 0);
        if (!fin.read (const_cast<char*>(buffer.data ()), file_size)) {
            
            fin.close ();
            return -1;
        }
        fin.close ();
    //    std::cout << "buffer : " << buffer.c_str () << std::endl;

        int32 ret = Write (TFS_filename, ret_filename, buffer.c_str (), file_size);
        if (0 > ret) {
            
            LOG (ERROR) << "failed to write to tfs file." << std::endl;
            return -1;
        }
        return ret;
    }

    int32 ImageWriter::WriteInternal (const int fd, const char* buffer, uint32 length) const {
        
        int     ret = -1;
        uint32  written = 0;
        
        while (written < length) {
            
            ret = tfs_client_->write (fd, buffer + written, length-written);
        //    std::cout << "buffer : " << buffer << "size : " << length-written<< std::endl;
            if (0 > ret) {

                return ret;
            }else {
                
                written += ret;
            }
        }
        return written;
    }

    int32 ImageWriter::Remove (const std::string &TFS_filename) const {

        int64_t filesize = 0;
        int     ret = tfs_client_->unlink (filesize, TFS_filename.c_str (), NULL, tfs::common::DELETE);

        if (ret != tfs::common::TFS_SUCCESS) {
            
            LOG (ERROR) << "filed to unlink." << std::endl;
            return 0;
        }

        return ret;
    }

    void ImageWriter::Disconnect (void) {
        
        tfs_client_->destroy ();
    }
}

测试

/*************************************************************************
	> File Name: test.cpp
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> Blog: http://blog.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月22日 星期二 11时40分32秒
 ************************************************************************/

#include <iostream>
#include <string>
#include "image_writer.h"

using namespace std;
using image::ImageWriter;

const char  *ip_port = "10.224.12.61:8108";

int write_file (const ImageWriter tfs, string &ret_filename)
{
    string  buffer("hello world!");
    int     ret = tfs.Write (&ret_filename, buffer);

    if (-1 == ret) {

        cout << "failure (write): " << endl;
        return -1;
    }

    cout << "success : " << ret_filename << endl;
    return ret;
}

int main (int argc, char **argv)
{
    ImageWriter     tfs_writer;

    tfs_writer.Connect (ip_port);

    string  ret_filename;
    string  buffer;

    tfs_writer.WriteWholeFile ((char*)NULL, &ret_filename, argv[1]);
    cout << "success : " << ret_filename << endl;
    

//    write_file (tfs_writer, ret_filename);

    return 0;
}

/*************************************************************************
	> File Name: test.cpp
	> Author: zhanglp
	> Mail: shixi_lipeng@staff.sina.com.cn 
	> Blog: http://blog.csdn.net/zhanglpql
	> Github: https://github.com/zhanglp92
	> Created Time: 2014年07月22日 星期二 11时40分32秒
 ************************************************************************/

#include <iostream>
#include <string>
#include <fstream>
#include "image_reader.h"

using namespace std;
using image::ImageReader;

const char  *ip_port = "10.224.12.61:8108";

int read_file (ImageReader tfs, const char *file_name)
{
    string  buffer;
    int ret = tfs.ReadWholeFile (file_name, &buffer);

    if (0 > ret) {

        cout << "failure (read): " << endl;
        return ret;
    }

    string  out_file ("./image/");
    out_file.append (file_name);
    ofstream    ofile;
    ofile.open (out_file.c_str (), ios::out);
    ofile.write (buffer.c_str (), buffer.length ());

    ofile.close ();
    return ret;
}

int main (int argc, char **argv)
{
    ImageReader     tfs_reader;

    tfs_reader.Connect (ip_port);

    read_file (tfs_reader, argv[1]);

    return 0;
}

修改一下也可以成功传送文件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值