示例#1
0
void Plugin::handleStatus(const std::string& name, const std::string& value)
{
    // Checking status of changed file that is tracked by git-bin
    File f(Plugin::GIT_CONFIG);
    if (!f.exists())
    {
        logger().error("Git bin has not been initialized");
        return;
    } 
    readIndex();
    for (auto it = _index.begin(); it != _index.end(); it++)
    {
        std::cout << (*it).filepath.c_str();
        std::string filepath(Plugin::GIT_CACHE_DIR);
        filepath.append("/wf/").append((*it).uuid);
        std::cout << (*it).md5.c_str() << std::endl;
        std::cout << getFileMd5(filepath) << std::endl;
        if ((*it).md5 != getFileMd5(filepath))
        {
            std::cout << " > Modified" << std::endl;
        } 
        else
        {
            std::cout << " > Not changed" << std::endl;
        }
    }
}
示例#2
0
void sendProjectGetList() {
   Buffer b;
   b.writeInt(MSG_PROJECT_LIST);
   unsigned char md5[MD5_LEN];
   if (getFileMd5(md5, sizeof(md5))) {
      b.write(md5, sizeof(md5));
      send_data(b);
   }
}
示例#3
0
void sendNewProjectCreate(char *description) {
   Buffer b;
   b.writeInt(MSG_PROJECT_NEW_REQUEST);
   unsigned char md5[MD5_LEN];
   if (getFileMd5(md5, sizeof(md5))) {
      b.write(md5, sizeof(md5));
      b.writeUTF8(description);
      b.writeLong(userOpts.pub);
      b.writeLong(userOpts.sub);
      send_data(b);
   }
}
示例#4
0
bool TcpServer::setFileInfo(const string &strFileName)
{
    Json::Value root;
    root["name"] = Json::Value(getFileName(strFileName));
    root["size"] = Json::Value((uint64_t)getFileSize(strFileName));
    root["md5"] = Json::Value(getFileMd5(strFileName));

    Json::FastWriter writer;
    m_strFileInfo = writer.write(root);

    FILE* fp = fopen(strFileName.c_str(), "rb");
    if (nullptr != fp)
    {
        m_pFile = shared_ptr<FILE>(fp, ::fclose);
    }
    return nullptr != fp;
}
示例#5
0
void Plugin::addFile(const std::string filepath)
{
    logger().debug("Adding file");
    // Check if file exists and it's not a directory
    File file(filepath);
    if (!file.exists())
    {
        std::cout << "Failed to add file " << filepath.c_str() << ": file does not exist" << std::endl;
        return;
    }
    if (file.isDirectory())
    {
        std::cout << "Failed to add file " << filepath.c_str() << ": file is a directory" << std::endl;
        return;

    }
    File index(Plugin::GIT_BIN_INDEX);
    if (!index.exists())
    {
        index.createFile();
    }
    // Find this file in the index
    if (isFileIndexed(filepath) && file.isLink())
    {
        logger().debug("File already in cache");
        auto entry = getIndexEntry(filepath);
        if (entry == nullptr)
        {
            std::cout << "Failed to retrieve index meta data for " << filepath << std::endl;
            return;
        }
        // Not fresh. Update existing index file
        // Remove file from index before recalculating everything
        for (auto it = _index.begin(); it != _index.end();) 
        {
            if ((*it).filepath == filepath)
            {
                // Remove link
                std::cout << "Replacing link with original file" << std::endl; 
                file.remove();
                Process::Args restoreArgs;
                std::string restorePath(Plugin::GIT_CACHE_DIR);
                restorePath.append("/wf/");
                restorePath.append((*it).uuid);
                restoreArgs.push_back(restorePath);
                restoreArgs.push_back(filepath);
                Process::launch("mv", restoreArgs, 0, 0, 0);
                it = _index.erase(it); 
            } else {
                it++;
            }
        } 

    }
    // Add new file into index
    UUIDGenerator gen;
    IndexEntry e;
    e.filepath = filepath;
    e.md5 = getFileMd5(filepath);
    e.uuid = gen.createRandom().toString();
    do 
    {
        e.uuid = gen.createRandom().toString();
    }
    while (!isUuidUnique(e.uuid));
    _index.push_back(e);
    writeIndex();

    // Place two copies of this file into cache
    // One copy is original file and don't tracked in any way
    // Second copy is a file we will create link to

    Process::Args args;
    args.push_back(filepath);
    std::string origPath(Plugin::GIT_CACHE_DIR);
    origPath.append("/of/").append(e.uuid);
    args.push_back(origPath);
    Poco::ProcessHandle copyProcess = Process::launch("cp", args, 0, 0, 0);
    if (copyProcess.wait() != 0)
    {
        std::cout << "Failed to move file into git-bin cache" << std::endl;
        return;
    }
    // Second stage copy
    args.clear();
    args.push_back(filepath);
    std::string workPath(Plugin::GIT_CACHE_DIR);
    workPath.append("/wf/").append(e.uuid);
    args.push_back(workPath);
    copyProcess = Process::launch("mv", args, 0, 0, 0);
    if (copyProcess.wait() != 0)
    {
        std::cout << "Failed to move file on stage 2" << std::endl;
        return;
    }

    // Make a link
    args.clear();
    args.push_back("-s");
    Path rel(filepath);
    std::string relPath;
    for (int i = 0; i < rel.depth(); i++)
    {
        relPath.append("../");
    }
    relPath.append(workPath);
    args.push_back(relPath);
    args.push_back(filepath);
    Poco::ProcessHandle linkProcess = Process::launch("ln", args, 0, 0, 0); 
    if (linkProcess.wait() != 0)
    {
        std::cout << "Failed to create link to file" << std::endl;
        return;
    }

    args.clear();
    args.push_back("add");
    args.push_back(filepath);
    Poco::ProcessHandle gitAddProcess = Process::launch("git", args, 0, 0, 0);
    if (gitAddProcess.wait() != 0)
    {
        std::cout << "Failed to add file into git index (git add)" << std::endl;
        return;
    }
}