Esempio n. 1
0
int main() {
	unsigned int indexOrder[MAX_INDEXES];
	unsigned int used;
	getOrderFromFile(indexOrder, &used);
	unsigned int chunksize = indexOrder[used-1];
	used--;
	
    unsigned char  *unziped   = "compressed_file_with_mc_gzip";
	unsigned char *hostFileBuffer;
	unsigned long long int filesize = getFilesize(unziped);
	hostFileBuffer = (unsigned char *) malloc(filesize+1);
    getFileContent(unziped, hostFileBuffer);
	
	FILE *file;
    file = fopen((const char *)"decompressed_file", "w");
    assert(file != NULL);

    for(int i = 0; i < used; i++) {
        unsigned int jumbledPos = getPosof(indexOrder, used, i);
        unsigned int lastChunkPos = getPosof(indexOrder, used, used-1);
        unsigned int bytesToWrite = min(chunksize, filesize - i * chunksize);
        unsigned int offset = 0;
        if(lastChunkPos < jumbledPos) {
            offset =  chunksize - min(chunksize, filesize - (used-1) * chunksize);
        }
        unsigned int written = fwrite(hostFileBuffer + jumbledPos * chunksize - offset, 1, bytesToWrite, file);
        assert(written == bytesToWrite);
        assert(ferror(file) == 0);
    }

    fclose(file);
	return 0;
}
bool BinaryDataMMAP::createMMAP(string filename)
{
   size_ = getFilesize(filename);
   if( size_==FILE_DOES_NOT_EXIST )
      return false;

   fileDescriptor_ = open(filename.c_str(), O_RDONLY);
   if(fileDescriptor_ == -1)
   {
      cout << "***ERROR: file to be mmap'd exists, but could not be read" << endl;
      return false;
   }

   ptr_ = (uint8_t*)mmap(NULL, size_, PROT_READ, MAP_SHARED, fileDescriptor_, 0);
   if(ptr_ == MAP_FAILED)
   {
      close(fileDescriptor_);
      cout << "***ERROR: mmap'ing file failed, despite exist w/ read access." << endl;
      return false;
   }
   
   // Usually we will be scanning the whole file, sequentially, after mmapping
   setAdvice(MADV_SEQUENTIAL);
   return true; 
}
Esempio n. 3
0
void * md5sumThreadFunc(void * arg)
{
	ThreadArg * ptarg = (ThreadArg *)arg;
	const char * pathname = ptarg->buf;
	//Database db = *(ptarg->pdb);
	Database db(DBFILENAME);
	printf("************md5sumThreadFunc: %s\n", pathname);   
	string md5str = md5sum(pathname);
	string sizestr = getFilesize(string(pathname));
	cout << "md5sumThreadFunc # filepath: " << pathname << "md5str: " << md5str << "sizestr: " << sizestr << endl;
	
	if (!md5str.empty() && !sizestr.empty())
	{
		std::map<string, string> insertParamMap = {    {"MD5SUM", md5str},
	                                                   {"MD5RAND", "NULL"},
	                                                   {"ABSPATH", pathname},
	                                                   {"SIZE", sizestr} };
		if (db.insert("file", insertParamMap))
        {
			Error::msg("Success: insert new file MD5SUM");
        } else {
           Error::msg("\033[31mDatabase insert error\033[0m");
        }   
	}

	delete ptarg; 
    return(NULL);
}
Esempio n. 4
0
void getOrderFromFile(unsigned int *hScores, unsigned int *used) {
    FILE *file;
    size_t nread;

    char *fn = (char *) filename;
    file = fopen((const char *)fn, "r");
    assert(file != NULL);

    unsigned long long int filesize = getFilesize(fn);

    char *buffer = (char *) malloc(filesize+1);
    assert(buffer != NULL);
    nread = fread(buffer, 1, filesize, file);
    assert(nread == filesize);
    assert(ferror(file) == 0);

    int startIdx = 0, k = 0;
    for(int i=0; i<nread; i++) {
        if(buffer[i] == ' ') {
            buffer[i] = '\0';
            hScores[k++] = atoi(buffer+startIdx);
            startIdx = i + 1;           
        }

		assert(k<MAX_INDEXES);
    }
    hScores[k++] = atoi(buffer+startIdx);
	*used = k;
    fclose(file);
}
Esempio n. 5
0
static int openAviFile(char * filename)
{
	int i;
	curFile = fopen(filename, "r");
	if (curFile) {
	new_message_window("opened");
		filesize = getFilesize(curFile);
		new_message_window(filesize);
		for (i = 0; i < VIDEO_FILEBUFFER_COUNT; i++) {
			fileBuffers[i].buffer = malloc(VIDEO_FILEBUFFER_SIZE);
			fileBuffers[i].filesize = filesize;
			fileBuffers[i].file = curFile;	
			if (fileBuffers[i].buffer == NULL) {
				for (i = i-1; i >= 0; i--) {
					free(fileBuffers[i].buffer);
				}
				exit(1);	
			}

		}
		return 0;
	}

	return -1;
}
int entrypoint() {
        struct DIS_fixed disasm_data;
        uint32_t ep = getEntryPoint();
        uint32_t remaining_size = getFilesize() - ep;
        uint32_t current_offset = DisassembleAt(&disasm_data, ep, remaining_size);


        return 0;
}
void StatusResMsg:: create_payload_file_data(char* homeDir, uint32_t file_index)
{
	char metaDataFileName[256];
	memset(metaDataFileName , 0, 256);
	create_metaDataFilename(metaDataFileName, homeDir, file_index);
	uint32_t recordLen = getFilesize(metaDataFileName);
	header.dataLen += 4;
	header.dataLen += recordLen;	
	recordLengths.push_back(recordLen);
	fileIndexes.push_back(file_index);
}
Esempio n. 8
0
void getFileContent(unsigned char * filename, unsigned char * buffer) {
    FILE *file;
    size_t nread;

    file = fopen((const char *)filename, "r");
    assert(file != NULL);

    unsigned long long int filesize = getFilesize(filename);

    nread = fread(buffer, 1, filesize, file);
    assert(nread == filesize);
    assert(ferror(file) == 0);

    fclose(file);
}
Esempio n. 9
0
/*
 * 读取sstable文件的Footer
 */
int readFooter(sequentialFile* psFile,Footer* pfooter)
{
	unsigned char footerSpace[48];
	long filesize;
	Slice r;
	initSlice(&r,20);
	int i;
	filesize = getFilesize(psFile);
	readSFile(48,filesize-48,psFile,footerSpace);
	setSlice(&r,footerSpace,48);
	decodeFooter(pfooter,&r);
	for(i = 0;i < 48;i++){
		printXchar(r.data_[i]);
	}
	freeSlice(&r);
	printf("file size = %ld,footersize = %zd\n",filesize,r.size_);
	return 0;
}
Esempio n. 10
0
bool TruncFileMsgEx::processIncoming(struct sockaddr_in* fromAddr, Socket* sock,
   char* respBuf, size_t bufLen, HighResolutionStats* stats)
{
   #ifdef BEEGFS_DEBUG
      const char* logContext = "TruncFileMsgEx incoming";

      std::string peer = fromAddr ? Socket::ipaddrToStr(&fromAddr->sin_addr) : sock->getPeername();
      LOG_DEBUG(logContext, Log_DEBUG, std::string("Received a TruncFileMsg from: ") + peer);
   #endif // BEEGFS_DEBUG

   int64_t fileSize = getFilesize();
   EntryInfo* entryInfo = getEntryInfo();

   bool useQuota = isMsgHeaderFeatureFlagSet(TRUNCFILEMSG_FLAG_USE_QUOTA);

   FhgfsOpsErr truncRes = MsgHelperTrunc::truncFile(
      entryInfo, fileSize, useQuota, getMsgHeaderUserID() );

   // send response

   if(unlikely(truncRes == FhgfsOpsErr_COMMUNICATION) )
   { // forward comm error as indirect communication error to client
      GenericResponseMsg respMsg(GenericRespMsgCode_INDIRECTCOMMERR,
         "Communication with storage target failed");
      respMsg.serialize(respBuf, bufLen);
      sock->sendto(respBuf, respMsg.getMsgLength(), 0,
         (struct sockaddr*)fromAddr, sizeof(struct sockaddr_in) );
   }
   else
   { // normal response
      TruncFileRespMsg respMsg(truncRes);
      respMsg.serialize(respBuf, bufLen);
      sock->sendto(respBuf, respMsg.getMsgLength(), 0,
         (struct sockaddr*)fromAddr, sizeof(struct sockaddr_in) );
   }

   // update operation counters
   Program::getApp()->getNodeOpStats()->updateNodeOp(sock->getPeerIP(), MetaOpCounter_TRUNCATE,
      getMsgHeaderUserID() );

   return true;      
}
Esempio n. 11
0
void convertLineEndings(std::string outfilename){
    std::ifstream readback;
    uint64_t filesize=0;
    getFilesize(outfilename, filesize);
    unsigned char* rbuff=new unsigned char [filesize];
    readback.open(outfilename, std::ios::in | std::ios::binary);
    readback.read(reinterpret_cast<char*>(rbuff), filesize);
    readback.close();
    std::vector<unsigned char> midBuff;
    for (uint64_t k=0; k<filesize; k++){
        if ((int)rbuff[k]!=13) midBuff.push_back(rbuff[k]);
    }
    for (uint64_t i=0; i<midBuff.size(); i++){
        rbuff[i]=midBuff[i];
    }
    std::ofstream writeback;
    writeback.open(outfilename, std::ios::out | std::ios::binary);
    writeback.write(reinterpret_cast<char*>(rbuff), midBuff.size());
    writeback.close();
    delete [] rbuff;
}
Esempio n. 12
0
void TableConversionWorker::process(){

    Gif* res = NULL;
	ConversionStatistics* stat = new ConversionStatistics;

	bool lct = false;
	for (int i = 0; i < m_gif->getSizeOfFrames(); ++i) {
		if(m_gif->getFrame(i)->getLctFlag() == 1){
			lct = true;
			break;
		}
	}
	m_mode = lct?Mode::Local_to_Global:Mode::Global_to_Local;




	std::stringstream oss, lss;
	oss << "original" << time(NULL) << ".gif";
	std::string orgFileName = oss.str();

    lss << "./generated/converted" << time(NULL) << ".gif";
	std::string lzwFileName = lss.str();


	IO orgIOFile(orgFileName);
	IO lzwIOFile(lzwFileName);

	#if defined(_WIN32)
	_mkdir("generated");
	#else
	mkdir("generated", 0777);
	#endif

	stat->mode = m_mode;

	emit modeOut(&m_mode);

    switch (m_mode) {
	case Global_to_Local:{

		clock_t cBegin = clock();
		res = TableConverter::globalToLocal(m_gif);
		clock_t cEnd = clock();
		double cElapsedSecs = double(cEnd - cBegin)*1000 / CLOCKS_PER_SEC;
		stat->conversionTime = cElapsedSecs;

		emit conversionDone(res);

		clock_t lBegin = clock();
		lzwIOFile.saveGif(*res);
		clock_t lEnd = clock();
		double lElapsedSecs = double(lEnd - lBegin)*1000 / CLOCKS_PER_SEC;
		stat->newLZWTime = lElapsedSecs;

		clock_t oBegin = clock();
		orgIOFile.saveGif(*m_gif);
		clock_t oEnd = clock();
		double oElapsedSecs = double(oEnd - oBegin)*1000 / CLOCKS_PER_SEC;
		stat->orgLZWTime = oElapsedSecs;

		stat->newSize = getFilesize(lzwFileName);
		stat->orgSize = getFilesize(orgFileName);

		remove(orgFileName.c_str());

	   } break;
	case Local_to_Global:{

		clock_t cBegin = clock();
		res = TableConverter::localToGlobal(m_gif);
		clock_t cEnd = clock();
		double cElapsedSecs = double(cEnd - cBegin)*1000 / CLOCKS_PER_SEC;
		stat->conversionTime = cElapsedSecs;

		emit conversionDone(res);

		clock_t lBegin = clock();
		lzwIOFile.saveGif(*res);
		clock_t lEnd = clock();
		double lElapsedSecs = double(lEnd - lBegin)*1000 / CLOCKS_PER_SEC;
		stat->newLZWTime = lElapsedSecs;

		clock_t oBegin = clock();
		orgIOFile.saveGif(*m_gif);
		clock_t oEnd = clock();
		double oElapsedSecs = double(oEnd - oBegin)*1000 / CLOCKS_PER_SEC;
		stat->orgLZWTime = oElapsedSecs;

		stat->newSize = getFilesize(lzwFileName);
		stat->orgSize = getFilesize(orgFileName);

		remove(orgFileName.c_str());

	   } break;
    default:
        emit error(QString("Invalid mode. (") + m_mode + ")");
        break;
    }

    if(res == NULL)
        emit error("Conversion failed.");

	emit statisticsOut(stat);
    emit finished();

}
Esempio n. 13
0
void config_parse(char *filename) {
	int fd
	  , nameStart
	  , nameStop
	  , valueStart
	  , valueStop
	  , lineR	// Counting \r's
	  , lineN	// Counting \n's
	  , lineT	// Highest of \r's and \n's
	  , lineC	// Character in line
	  , pos;	// Byte position in file
	char *name
	   , *value
	   , *config;
	state state;
	letter l;
	size_t filesize;

	filesize = getFilesize(filename);

	/* Open file */
	fd = open(filename, O_RDONLY, 0);
	if (fd == -1) {
		/* Do not throw an error on EACCES, to allow blindly reading
		 * the 'global' followed by the 'root only' configurations to
		 * simplify overall code.
		 */
		if (errno != EACCES) {
			parser_error("readable file", "unreadable file", -1, -1);
		}

		return;
	}

	/* Execute mmap */
	config = mmap(NULL, filesize, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
	if (config == MAP_FAILED) {
		parser_error("mmapable file", "error mmapping file", -1, -1);
		goto abort_mmap;
	}

	name = value = NULL;
	nameStart = nameStop = valueStart = valueStop = pos = 0;
	lineR = lineN = lineC = lineT = 1;
	state = STATE_START_OF_LINE;
	do {
		switch(config[pos]) {
			case '\r':
				lineR++;
				lineT = max(lineR, lineN);
				lineC = 1;
				l = LETTER_END_OF_LINE;
				break;
			case '\n':
				lineN++;
				lineT = max(lineR, lineN);
				lineC = 1;
				l = LETTER_END_OF_LINE;
				break;
			case ' ':
			case '\t':
				l = LETTER_WHITESPACE;
				break;
			case '=':
				l = LETTER_EQUAL;
				break;
			case '#':
				l = LETTER_COMMENT;
				break;
			default:
				if ((config[pos] < ' ')
				 || (config[pos] > '~')) {
					l = LETTER_INVALID;
					parser_error("valid character", "invalid character", lineT, lineC);
					state = STATE_COMMENT_OR_ERROR;
					break;
				}
				l = LETTER_LETTER;
				break;
		}

		switch(state) {
		case STATE_COMMENT_OR_ERROR: switch(l) {
			case LETTER_END_OF_LINE:
				state = STATE_START_OF_LINE;
				break;
			default:
				break;
			} break;
		case STATE_START_OF_LINE: switch(l) {
			case LETTER_END_OF_LINE:
			case LETTER_WHITESPACE:
				break;
			case LETTER_EQUAL:
				parser_error("option name", "=", lineT, lineC);
				state = STATE_COMMENT_OR_ERROR;
				break;
			case LETTER_COMMENT:
				state = STATE_COMMENT_OR_ERROR;
				break;
			case LETTER_LETTER:
			default:
				nameStart = pos;
				nameStop = pos;
				state = STATE_NAME;
				break;
			} break;
		case STATE_NAME: switch(l) {
			case LETTER_END_OF_LINE:
				parser_error("=", "end of line", lineT, lineC);
				state = STATE_COMMENT_OR_ERROR;
				break;
			case LETTER_WHITESPACE:
				state = STATE_PRE_EQUAL;
				break;
			case LETTER_EQUAL:
				state = STATE_POST_EQUAL;
				break;
			case LETTER_COMMENT:
				parser_error("=", "comment", lineT, lineC);
				state = STATE_COMMENT_OR_ERROR;
				break;
			case LETTER_LETTER:
			default:
				nameStop = pos;
				break;
			} break;
		case STATE_PRE_EQUAL: switch(l) {
			case LETTER_WHITESPACE:
				break;
			case LETTER_EQUAL:
				state = STATE_POST_EQUAL;
				break;
			default:
				parser_error("= or more whitespace", "gibberish", lineT, lineC);
				state = STATE_COMMENT_OR_ERROR;
				break;
			} break;
		case STATE_POST_EQUAL: switch(l) {
			case LETTER_END_OF_LINE:
				parser_error("option value or more whitespace", "end of line", lineT, lineC);
				state = STATE_COMMENT_OR_ERROR;
				break;
			case LETTER_WHITESPACE:
				break;
			case LETTER_EQUAL:
			case LETTER_COMMENT:
			case LETTER_LETTER:
			default:
				valueStart = pos;
				valueStop = pos;
				state = STATE_VALUE;
				break;
			} break;
		case STATE_VALUE: switch(l) {
			case LETTER_END_OF_LINE:
				name = strndup((char *)config + nameStart, nameStop - nameStart + 1);
				value = strndup((char *)config + valueStart, valueStop - valueStart + 1);
				option_set(name, value);
				free(name);
				free(value);
				state = STATE_START_OF_LINE;
				break;
			case LETTER_WHITESPACE:
			case LETTER_EQUAL:
			case LETTER_COMMENT:
			case LETTER_LETTER:
			default:
				valueStop = pos;
				break;
			} break;
		}
		pos++;
		lineC++;
	} while (pos < filesize);

	/* End-of-File handling. */
	switch(state) {
	case STATE_NAME:
	case STATE_PRE_EQUAL:
		parser_error("=", "end of file", lineT, lineC);
		break;
	case STATE_POST_EQUAL:
		parser_error("option value", "end of file", lineT, lineC);
		break;
	case STATE_START_OF_LINE:
	case STATE_COMMENT_OR_ERROR:
	case STATE_VALUE:
	default:
		/* Valid end of file location, no issue. */
		break;
	}

	/* Cleanup */

	munmap(config, filesize);

abort_mmap:
	close(fd);
}
Esempio n. 14
0
void Database::traverseFiles(string dirpath)
{
   DIR * dir= opendir(dirpath.c_str());

   if(!dir)
   {
      Error::ret("opendir");
      return;
   }

   if (dirpath.back() != '/')
   {
      dirpath += "/";
   }
   struct dirent* e;
   while( (e = readdir(dir)) )
   {
      if(e->d_type == 4 && strcmp(e->d_name, ".") && strcmp(e->d_name, ".."))
      {
         traverseFiles(dirpath + e->d_name);
      }
      else if(e->d_type == 8)
      {
         string filepath = dirpath + e->d_name;

         string inode = getInode(filepath.c_str());

         cout << "\n\nmd5sum: " << filepath << " ..." << endl;
         string md5str = md5sum(filepath.c_str());
         string sizestr = getFilesize(filepath);
         cout << "filepath: " << filepath << "md5str: " << md5str << "sizestr: " << sizestr << endl;

         string   ghostfilename;
                  ghostfilename += getCurrentTime();
                  ghostfilename += "_" + md5str + "_";
                  ghostfilename += e->d_name;
         string   ghostPath = GHOSTDIR + ghostfilename;

         if (!md5str.empty() && !sizestr.empty())
         {
            std::map<string, string> selectParamMap = {  {"MD5SUM", md5str} };
            if (select("file", selectParamMap))
            {
               vector< map<string ,string> > resultMapVector = getResult();
               if (resultMapVector.empty())
               {
                  std::map<string, string> insertParamMap = {  {"MD5SUM", md5str},
                                                               {"MD5RAND", "NULL"},
                                                               {"ABSPATH", ghostPath},
                                                               {"FILENAME", ghostfilename},
                                                               {"INODE", inode},
                                                               {"SIZE", sizestr} };
                  if (insert("file", insertParamMap))
                  {
                     Error::msg("Success: insert new file MD5SUM");

                     if (link(filepath.c_str(), ghostPath.c_str()) < 0)
                     {
                        Error::ret("\033[31mlink\033[0m"); 
                        cerr << filepath << ":" << ghostPath << endl;
                     }

                  } else {
                    Error::msg("\033[31mDatabase insert error\033[0m");
                  }   

               } else {
                  Error::msg("\033[31mMD5SUM already exist\033[0m");
                  printResult();
               }
            } else {
               Error::msg("\033[31mDatabase select error\033[0m");
            }   
         }   
      }
   }
   closedir(dir);
}
Esempio n. 15
0
void SrvDTP::insertNewFileMD5SUM(const char * pathname, Database *pdb)
{ 
	
	string inode = getInode(pathname);

	cout << "md5sum computing... " << endl;
	string md5str = md5sum(pathname);
	string sizestr = getFilesize(string(pathname));
	cout << "insertNewFileMD5SUM # filepath: " << pathname << "md5str: " << md5str << "sizestr: " << sizestr << endl;
	
	string 	ghostfilename;
			ghostfilename += getCurrentTime();
			ghostfilename += "_" + md5str + "_";
			ghostfilename += psrvPI->getFilename();
	string 	ghostPath = GHOSTDIR + ghostfilename;

	if (!md5str.empty() && !sizestr.empty())
	{
		std::map<string, string> selectParamMap = {  {"MD5SUM", md5str} };
		if (pdb->select("file", selectParamMap))
		{
			vector< map<string ,string> > resultMapVector = pdb->getResult();
			if (resultMapVector.empty())
			{
				std::map<string, string> insertParamMap = {    {"MD5SUM", md5str},
			                                                   {"MD5RAND", "NULL"},
			                                                   {"ABSPATH", ghostPath},
			                                                   {"FILENAME", ghostfilename},
			                                                   {"INODE", inode},
			                                                   {"SIZE", sizestr} };
				if (pdb->insert("file", insertParamMap))
		        {
					Error::msg("Success: insert new file MD5SUM");

					if (link(pathname, ghostPath.c_str()) < 0)
					{
						Error::ret("\033[31mlink\033[0m"); 
						cerr << pathname << ":" << ghostPath << endl;
					}

		        } else {
		           Error::msg("\033[31mDatabase insert error\033[0m");
		        }   
			} else {
				Error::msg("\033[31mThis MD5SUM already exists\033[0m");
				/*std::map<string, string> whereParamMap = { {"MD5SUM", md5sum(pathname)} };
	    		std::map<string, string> updateParamMap = { {"VALID", "1"} };

				if (pdb->update("file", whereParamMap, updateParamMap))
		        {
		        	vector< map<string ,string> > resultMapVector = pdb->getResult();
					if (!resultMapVector.empty())
					{
						printf("Success: update VALID=1\n");
					} else {
						printf("update: not find record\n");
					}
		        } else {
		           Error::msg("\033[31mDatabase update error\033[0m");
		        }*/
			}
		} else {
			 Error::msg("\033[31mDatabase select error\033[0m");
		}
		
	}   
}
Esempio n. 16
0
/**
 * @brief TEST_F SdkTestTransfers
 *
 * It performs different operations related to transfers in both directions: up and down.
 *
 * - Starts an upload transfer and cancel it
 * - Starts an upload transfer, pause it, resume it and complete it
 * - Get node by fingerprint
 * - Get size of a node
 * - Download a file
 */
TEST_F(SdkTest, SdkTestTransfers)
{
    MegaNode *rootnode = megaApi->getRootNode();
    string filename1 = UPFILE;
    createFile(filename1);


    // --- Cancel a transfer ---

    transfersCancelled = false;
    megaApi->startUpload(filename1.data(), rootnode);
    megaApi->cancelTransfers(MegaTransfer::TYPE_UPLOAD);
    waitForResponse(&transfersCancelled);

    EXPECT_EQ(MegaError::API_OK, lastError) << "Transfer cancellation failed (error: " << lastError << ")";


    // --- Upload a file (part 1) ---

    uploadFinished = false;
    megaApi->startUpload(filename1.data(), rootnode);
    // do not wait yet for completion


    // --- Pause a transfer ---

    transfersPaused = false;
    megaApi->pauseTransfers(true, MegaTransfer::TYPE_UPLOAD);
    waitForResponse(&transfersPaused);

    EXPECT_EQ(MegaError::API_OK, lastError) << "Cannot pause transfer (error: " << lastError << ")";
    EXPECT_TRUE(megaApi->areTransfersPaused(MegaTransfer::TYPE_UPLOAD)) << "Upload transfer not paused";


    // --- Resume a transfer ---

    transfersPaused = false;
    megaApi->pauseTransfers(false, MegaTransfer::TYPE_UPLOAD);
    waitForResponse(&transfersPaused);

    EXPECT_EQ(MegaError::API_OK, lastError) << "Cannot resume transfer (error: " << lastError << ")";
    EXPECT_FALSE(megaApi->areTransfersPaused(MegaTransfer::TYPE_UPLOAD)) << "Upload transfer not resumed";


    // --- Upload a file (part 2) ---

    waitForResponse(&uploadFinished);

    ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot upload file (error: " << lastError << ")";

    MegaNode *n1 = megaApi->getNodeByHandle(h);
    bool null_pointer = (n1 == NULL);

    ASSERT_FALSE(null_pointer) << "Cannot upload file (error: " << lastError << ")";
    ASSERT_STREQ(filename1.data(), n1->getName()) << "Uploaded file with wrong name (error: " << lastError << ")";


    // --- Get node by fingerprint (needs to be a file, not a folder) ---

    char *fingerprint = megaApi->getFingerprint(n1);
    MegaNode *n2 = megaApi->getNodeByFingerprint(fingerprint);

    null_pointer = (n2 == NULL);
    EXPECT_FALSE(null_pointer) << "Node by fingerprint not found";
//    ASSERT_EQ(n2->getHandle(), n4->getHandle());  This test may fail due to multiple nodes with the same name

    delete fingerprint;


    // --- Get the size of a file ---

    int filesize = getFilesize(filename1);
    int nodesize = megaApi->getSize(n2);
    EXPECT_EQ(filesize, nodesize) << "Wrong size of uploaded file";


    // --- Download a file ---

    string filename2 = "./" + DOWNFILE;

    downloadFinished = false;
    megaApi->startDownload(n2, filename2.c_str());
    waitForResponse(&downloadFinished);

    ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot download the file (error: " << lastError << ")";

    MegaNode *n3 = megaApi->getNodeByHandle(h);
    null_pointer = (n3 == NULL);

    ASSERT_FALSE(null_pointer) << "Cannot download node";
    ASSERT_EQ(n2->getHandle(), n3->getHandle()) << "Cannot download node (error: " << lastError << ")";

    delete rootnode;
    delete n1;
    delete n2;
    delete n3;
}
Esempio n. 17
0
void SrvDTP::sendFile(const char *pathname, uint32_t nslice, uint32_t sindex, uint16_t slicecap)
{
	//cout << endl  << endl << pathname << endl << endl;
	Packet & packet = *(this->ppacket);
	char buf[MAXLINE];
	Database * pdb = psrvPI->getPDB();
	string inode = getInode(pathname);
	std::map<string, string> selectParamMap = {  {"INODE", inode} };
    if (pdb->select("file", selectParamMap))
    {
       vector< map<string ,string> > resultMapVector = pdb->getResult();
       if (!resultMapVector.empty())
       {
       		string dbAccess = resultMapVector[0]["ACCESS"];
       		unsigned long long access = std::stoull(dbAccess) + 1;
       		snprintf(buf, MAXLINE, "%llu", access);
       		dbAccess = buf;

       		std::map<string, string> updateParamMap = { {"ACCESS", dbAccess} };
       		if (pdb->update("file", resultMapVector[0]["ID"], updateParamMap))
			{
				cout << "update ACCESS+1 ok" <<endl;
			} else {
				printf("\033[31mupdate ACCESS+1 error\033[0m\n");
			}

       } else {
          printf("\033[31mINODE not exist\033[0m\n");
       }
    } else {
       Error::msg("\033[31mDatabase select error\033[0m\n");
    }   

	int n;
	//uint32_t nslice =0, sindex = 0;

	// off64_t curpos = sindex * slicecap;
	// if ( lseek64(fileno(psrvPI->getFp()), curpos, SEEK_SET) < 0)
	// {
	// 	packet.sendSTAT_ERR(strerror_r(errno, buf, MAXLINE));
	// 	return;
	// } else {
	// 	printf("Recv file [%s %u/%u] now\n", pathname, sindex, nslice);
	// 	// send STAT_OK
	// 	packet.sendSTAT_OK();
	// }
	
	string sizestr = getFilesize(string(pathname));
	if (sizestr.empty())
	{
		packet.sendSTAT_ERR("getFilesize() failed");
		return;
	}
	// confirm enough space to write on client host
	packet.sendSTAT(STAT_SIZE, sizestr);

	psrvPI->recvOnePacket();
	if (packet.getTagid() == TAG_STAT && packet.getStatid() == STAT_ERR) 
	{
		return;
	} else if (packet.getTagid() == TAG_STAT && packet.getStatid() == STAT_OK)
	{
		;
	} else {
		Error::msg("unknown packet");
		packet.print();
		return;
	}

	//if ( (fp = fopen(pathname, "rb")) == NULL)
	if ( psrvPI->setFp(fopen(pathname, "rb")) == NULL)
	{
		// send STAT_ERR Response
		// GNU-specific strerror_r: char *strerror_r(int errnum, char *buf, size_t buflen);
		packet.sendSTAT_ERR(strerror_r(errno, buf, MAXLINE));
		return;
	} else if ( (n = getFileNslice(pathname, &nslice)) <= 0)  {
		if ( n == 0) {
			printf("EOF[%s]: 0 bytes\n", pathname);
			Fclose(&psrvPI->getFp());
			packet.sendSTAT_OK();
			packet.sendDATA_TEXT(getFileSizeString(pathname));
			packet.sendDATA_FILE(0, 0, 0, NULL);
			packet.sendSTAT_EOF("EOF: 0 bytes");
			return;
		} else if ( n == -2) {
			snprintf(buf, MAXLINE, "Too large file size");
			packet.sendSTAT_ERR(buf);
		} else {
			snprintf(buf, MAXLINE, "File stat error");
			packet.sendSTAT_ERR(buf);
		}
		return;
	} else {
		// send STAT_OK
		packet.sendSTAT_OK();
	}

	packet.sendDATA_TEXT(getFileSizeString(pathname));

	char body[PBODYCAP];
	printf("Send [%s] now\n", pathname);

	while( (n = fread(body, sizeof(char), PBODYCAP, psrvPI->getFp())) >0 )
	{
		packet.sendDATA_FILE(nslice, ++sindex, n, body);
	}
	
	// send EOF
	Fclose(&psrvPI->getFp());
	printf("EOF [%s]\n", pathname);
	packet.sendSTAT_EOF();
}
Esempio n. 18
0
// 
// Handle command line arguments
//
void parseIndexOptions(int argc, char** argv)
{
    bool die = false;
    for (char c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;) 
    {
        std::istringstream arg(optarg != NULL ? optarg : "");
        switch (c) 
        {
            case 'p': arg >> opt::prefix; break;
            case '?': die = true; break;
            case 'c': opt::validate = true; break;
            case 'd': opt::bDiskAlgo = true; arg >> opt::numReadsPerBatch; break;
            case 't': arg >> opt::numThreads; break;
            case 'g': arg >> opt::gapArrayStorage; break;
            case 'a': arg >> opt::algorithm; break;
            case 'v': opt::verbose++; break;
            case OPT_NO_REVERSE: opt::bBuildReverse = false; break;
            case OPT_NO_FWD: opt::bBuildForward = false; break;
            case OPT_NO_SAI: opt::bBuildSAI = false; break;
            case OPT_HELP:
                std::cout << INDEX_USAGE_MESSAGE;
                exit(EXIT_SUCCESS);
            case OPT_VERSION:
                std::cout << INDEX_VERSION_MESSAGE;
                exit(EXIT_SUCCESS);
        }
    }

    // Transform algorithm parameter to lower case
    std::transform(opt::algorithm.begin(), opt::algorithm.end(), opt::algorithm.begin(), ::tolower);
    
    if (argc - optind < 1) 
    {
        std::cerr << SUBPROGRAM ": missing arguments\n";
        die = true;
    } 
    else if (argc - optind > 1) 
    {
        std::cerr << SUBPROGRAM ": too many arguments\n";
        die = true;
    }

    if(opt::gapArrayStorage != 4 && opt::gapArrayStorage != 8 &&
       opt::gapArrayStorage != 16 && opt::gapArrayStorage != 32)
    {
        std::cerr << SUBPROGRAM ": invalid argument, --gap-array,-g must be one of 4,8,16,32 (found: " << opt::gapArrayStorage << ")\n";
        die = true;
    }

    if(opt::numThreads <= 0)
    {
        std::cerr << SUBPROGRAM ": invalid number of threads: " << opt::numThreads << "\n";
        die = true;
    }

    if(opt::algorithm != "sais" && opt::algorithm != "bcr" && opt::algorithm != "ropebwt")
    {
        std::cerr << SUBPROGRAM ": unrecognized algorithm string " << opt::algorithm << ". --algorithm must be sais, bcr or ropebwt\n";
        die = true;
    }

    if(opt::algorithm == "ropebwt" && opt::bDiskAlgo)
    {
        std::cerr << SUBPROGRAM ": the options -a ropebwt and -d are not compatible, please only use one.\n";
        die = true;
    }

    if (die) 
    {
        std::cout << "\n" << INDEX_USAGE_MESSAGE;
        exit(EXIT_FAILURE);
    }

    // Parse the input filenames
    opt::readsFile = argv[optind++];
    if(opt::prefix.empty())
        opt::prefix = stripFilename(opt::readsFile);

    // Check if input file is empty
    size_t filesize = getFilesize(opt::readsFile);
    if(filesize == 0)
    {
        std::cerr << SUBPROGRAM ": input file is empty\n";
        exit(EXIT_FAILURE);
    }
}
Esempio n. 19
0
//
// Handle command line arguments
//
void parseScanOptions(int argc, char** argv)
{

	std::string bamlistfile =  "";
	std::string rev = "";

	Headers hd;
    for (char c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;)
    {
        std::istringstream arg(optarg != NULL ? optarg : "");
        switch (c)
        {
            case 'f':
            	arg >> bamlistfile; break;
            case 'o':
            	arg >> opt::outputfile; break;
            case 'H':
            	opt::writerheader=false; break;
            case 'm':
                opt::mergerg = true; break;
            case 'u':
                opt::ignorerg = true; break;
            case 'w':
                opt::onebam = true; break;
            case 'h':
        		for(size_t h=0; h<hd.headers.size();h++){
        			std::cout << hd.headers[h] << ScanParameters::FIELD_SEP;
        		}
        		std::cout << "\n";
        		exit(EXIT_SUCCESS);
            case 'k':
            	arg >> opt::tel_k;
            	break;
            case 'r':
				arg >> ScanParameters::READ_LENGTH;
				if(ScanParameters::READ_LENGTH <= 0 || ScanParameters::READ_LENGTH > 100000){
					std::cerr << "please specify valid read length that is greater than 0 and length than 100kb" << "\n";
					exit(EXIT_FAILURE);
				}
				break;
            case 'p':

				break;
            case 'z':
            	arg >> ScanParameters::PATTERN;
				ScanParameters::PATTERN_REVCOMP = reverseComplement(ScanParameters::PATTERN);
				std::cerr << "use user specified pattern " <<  ScanParameters::PATTERN << "\n";
				std::cerr << "reverse complement " <<  ScanParameters::PATTERN_REVCOMP << "\n";
            	break;
            case 'e':
            	arg >> opt::exomebedfile;
            	opt::exomebed = readBedAsVector(opt::exomebedfile);
            	std::cout << "loaded "<< opt::exomebed.size() << " exome regions \n"<< std::endl;
//            	std::cout << opt::exomebed << "\n";
            	break;

            case OPT_HELP:
                std::cout << TELSEQ_USAGE_MESSAGE;
                exit(EXIT_SUCCESS);
            case OPT_VERSION:
                std::cout << TELSEQ_VERSION_MESSAGE;
                exit(EXIT_SUCCESS);
        }
    }

    update_pattern();

    // deal with cases of API usage:
    // telseq a.bam b.bam c.bam ...
    // | telseq
    // telseq -f

    if (argc - optind < 1) // no argument specified
    {
    	// check if it is from pipe
		if(!isatty(fileno(stdin))){
			std::string line;
			while (std::getline(std::cin, line))
			{
				if(line.empty()){
					continue;
				}
				opt::bamlist.push_back(line);
			}
		}else if(bamlistfile.empty() ){ // check if not from a pipe, -f must be spceified
//    		std::cerr << SUBPROGRAM ": No BAM specified. Please specify BAM either directly, by using -f option or piping BAM file path.\n";
    		std::cout << TELSEQ_USAGE_MESSAGE;
    		exit(EXIT_FAILURE);
    	}
    }

    else if (argc - optind >= 1) // if arguments are specified
    {
    	// -f has higher priority, when specified, ignore arguments.
    	if(bamlistfile.empty()){
    		for(int i = optind; i < argc; ++i ){
    		    opt::bamlist.push_back(argv[i]);
    		}
    	}
    }

    // read in bamlist
    if(!bamlistfile.empty()){
        size_t filesize = getFilesize(bamlistfile);
        if(filesize == 0)
        {
            std::cerr << PROGRAM_BIN ": BAMLIST file specified by -f is empty\n";
            exit(EXIT_FAILURE);
        }

        std::istream* pReader = createReader(bamlistfile);
        std::string line;

        while(getline(*pReader, line))
        {
        	if(line.empty()){
        		continue;
        	}
            opt::bamlist.push_back(line);
        }

        size_t bamsize = opt::bamlist.size();
        if(bamsize == 0 ){
            std::cerr << PROGRAM_BIN ": Could find any sample in BAMLIST file specified.\n";
            exit(EXIT_FAILURE);
        }
        delete pReader;
    }

    // check output
//    for(size_t i = 0; i < opt::bamlist.size(); ++i ){
//    	std::cerr << "opt::bamlist:" << opt::bamlist[i] << "\n";
//    }
//    std::cerr << "opt::bamlistfile:" << bamlistfile << "\n";
//    std::cerr << "opt::writerheader:" << opt::writerheader << "\n";
//    std::cerr << "opt::tel_k:" << opt::tel_k << "\n";
//    std::cerr << "opt::outputdir:" << opt::outputfile << "\n";

}