示例#1
0
文件: birom16.c 项目: paurinn/kuji16
int birom16_new(struct birom16_state **state, struct chipdef16 *chip, struct serial *serial) {
	char path[256];
	int rc;

	assert(state);

	*state = calloc(1, sizeof(struct birom16_state));
	assert(*state);

	(*state)->chip = chip;
	(*state)->serial = serial;

	LOGD("User says MCU is '%s' on '%s'.", chip->name, serial->address);

	memset(path, 0x00, sizeof(path));
	snprintf(path, sizeof(path) - 1, "kernal16/%s", chip->kernal);

	(*state)->kernalsize = filedata(path, &(*state)->kernaldata);
	if ((*state)->kernalsize <= 0 || (*state)->kernaldata == NULL) {
		LOGE("Could not read contents of '%s'.", path);
		return E_READ;
	}

	LOGD("Loaded '%s' (0x%02X bytes).", path, (*state)->kernalsize);

	return E_NONE;
}
示例#2
0
static int process_new(struct sbuf *p1b, FILE *ucfp, const char *currentdata, const char *datadirtmp, const char *deltmppath, struct dpth *dpth, int *resume_partial, struct cntr *cntr, struct config *cconf)
{
	if(*resume_partial
	  && p1b->cmd==CMD_FILE
	  && cconf->librsync
	  && p1b->compression==cconf->compression)
	{
		if(resume_partial_new_file(p1b,
			cntr, currentdata, datadirtmp, deltmppath,
			dpth, cconf)) return -1;

		// Burp only transfers one file at a time, so
		// if there was an interruption, there is only
		// a possibility of one partial file to resume.
		*resume_partial=0;
	}
	if(filedata(p1b->cmd))
	{
		//logp("need to process new file: %s\n", p1b->path);
		// Flag the things that need to be sent (to the client)
		p1b->sendstat++;
		p1b->sendpath++;
	}
	else
	{
		new_non_file(p1b, ucfp, p1b->cmd, cntr);
	}
	return 0;
}
示例#3
0
bool FileHelper::ReadAllText(const String & fileName, String & content)
{
    Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
    if(hFile == INVALID_HANDLE_VALUE)
        return false;
    unsigned int filesize = GetFileSize(hFile, 0);
    if(!filesize)
    {
        content.clear();
        return true;
    }
    Memory<char*> filedata(filesize + 1, "FileReader::ReadAllText:filedata");
    DWORD read = 0;
    if(!ReadFile(hFile, filedata(), filesize, &read, 0))
        return false;
    content = String(filedata());
    return true;
}
示例#4
0
int main(int argc, char* argv[]){
	if(argc != 2){
		fprintf(stderr, "Usage: [filename]\n");
		exit(1);
	}

	filedata(argv[1]);

	return 0;
}
示例#5
0
  std::vector<uint8_t> RARCFile::corrupt()
  {
    //  Commented section corrupts individual files passed on the command line
    //  which won't be used until the GUI has a better design
    /*
    for (auto& path : info->files())
    {
      if (entries.count(path) == 0)
      {
        //  File doesn't exist as it was given, print error and go to next iteration
        debug::cerr << "Entry doesn't exist: " << path << std::endl;
        continue;
      }

      detail::FileEntry entry = entries.at(path);
      uint32_t offset = header->start() + entry.offset();
      std::vector<uint8_t> filedata(data.begin() + offset, data.begin() + offset + entry.size());

      std::ofstream ofs(entry.name(), std::ios::binary);
      ofs.write(reinterpret_cast<char *>(&filedata[0]), entry.size());
      ofs.close();

      //  Since RARC files are archives of Nintendo files, corrupt it with Nintendo file protection
      NintendoFile::start(filedata, arguments);

      std::copy(filedata.begin(), filedata.begin() + entry.size(), data.begin() + offset);
    }
    */

    for (auto& e : entries)
    {
      detail::FileEntry entry = e.second;
      uint32_t offset = header->start() + entry.offset();

      if (data.size() < offset + entry.size())
      {
        continue;
      }

      std::vector<uint8_t> filedata(data.begin() + offset, data.begin() + offset + entry.size());

      //  If the file is Yay0 or Yaz0 compressed then ignore it since the sizes most likely won't match
      if (filedata.size() > 4 && util::read(filedata, 0, 4) != "Yaz0" && util::read(filedata, 0, 4) != "Yay0")
      {
        //  Since RARC files are archives of Nintendo files, corrupt it with Nintendo file protection
        NintendoFile::start(filedata, arguments);

        //  Copy the corrupted bytes back into place
        //  NOTE: This does not account for differing sizes for Yaz0 compressed files!
        std::copy(filedata.begin(), filedata.begin() + entry.size(), data.begin() + offset);
      }
    }

    return data;
  }
示例#6
0
文件: main.cpp 项目: nathanvy/nzip
int main(int argc, char* argv[]) {
  if( argc != 2){
    std::cout<< "Usage: "<<argv[0]<<" <path to file>" << std::endl;
    return 1;
  }

  std::string outputstr = "";
  std::ostringstream oss;
  
  //change this to actually read a file from disk instead of stdin
  mpz_class filedata(argv[1]);

  //if compiler supported c++11 we could use this to search for filedata in TWOS[], but I'm on debian stable w gcc 4.6 :/
  //bool exists = std::find(std::begin(TWOS), std::end(TWOS), filedata) != std::end(TWOS);

  int exponent = powerOfTwo(filedata);
  mpz_class remainder( filedata - TWOS[exponent]);

  if( remainder == 0 ){
    oss << exponent;
    outputstr = "2^" + oss.str();
    std::cout << outputstr << std::endl;
    return 0;
  }

  std::cout << "exponent is " << exponent << std::endl;
  std::cout << "twos[exponent] is " << TWOS[exponent] << std::endl;
  std::cout << TWOS[exponent] << std:: endl;
  std::cout << filedata <<  std::endl;
  std::cout << "iterating on remainder which equals" << remainder << std::endl;
  while(remainder >= 128){
    //can't just concatenate an int to a string
    oss << exponent;
    outputstr+="2^" +oss.str()+ "+";

    //see if the remainder is near any powers of two
    exponent = powerOfTwo(remainder);
    remainder = remainder - TWOS[exponent];
    std::cout << exponent << std::endl;

    //set stringstream to empty string and then clear error flag(s)
    oss.str("");
    oss.clear();
  }
  oss.str("");
  oss.clear();
  oss << remainder;
  outputstr += oss.str();

  std::cout << outputstr << std::endl;
  
  return 0;
}
示例#7
0
	std::vector<std::string> OpenFileDialog(const char * filter)
	{
		char curDir[MAX_PATH];
		GetCurrentDirectoryA(MAX_PATH,curDir);
		std::vector<char> filedata(MAX_PATH * 1000);
		char * szFileName = &filedata[0];
		OPENFILENAMEA opf;
		opf.hwndOwner = 0;
		opf.lpstrFilter = filter;
		opf.lpstrCustomFilter = 0;
		opf.nMaxCustFilter = 0L;
		opf.nFilterIndex = 1L;
		opf.lpstrFile = szFileName;
		opf.nMaxFile = (DWORD)filedata.size();
		opf.lpstrFileTitle = 0;
		opf.nMaxFileTitle=50;
		opf.lpstrInitialDir = 0;
		opf.lpstrTitle = "Open";
		opf.nFileOffset = 0;
		opf.nFileExtension = 2;
		opf.lpstrDefExt = "*.*";
		opf.lpfnHook = NULL;
		opf.lCustData = 0;
		opf.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_EXPLORER;
		//opf.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
		opf.lStructSize = sizeof(OPENFILENAMEA);
		std::vector<std::string> retval;
		if(GetOpenFileNameA(&opf))
		{
			if(opf.nFileOffset > strlen(szFileName))
			{
				std::string path = std::string(szFileName) + "\\";
				unsigned startOffset = opf.nFileOffset;
				while(szFileName[startOffset])
				{
					retval.push_back( path + std::string(&szFileName[startOffset]));
					startOffset += (unsigned int) strlen(&szFileName[startOffset])+1;
				}
			}
			else
				retval.push_back(szFileName);
		}
		else
		{
			DWORD retval = CommDlgExtendedError();
			while(0);
		}
		SetCurrentDirectoryA(curDir);
		return retval;
	}
示例#8
0
static int process_new(struct sdirs *sdirs, struct conf *cconf,
	struct sbuf *p1b, FILE *ucfp, struct dpthl *dpthl)
{
	if(!p1b->path.buf) return 0;
	if(filedata(p1b->path.cmd))
	{
		//logp("need to process new file: %s\n", p1b->path);
		// Flag the things that need to be sent (to the client)
		p1b->flags |= SBUFL_SEND_STAT;
		p1b->flags |= SBUFL_SEND_PATH;
	}
	else
	{
		new_non_file(p1b, ucfp, cconf);
	}
	return 0;
}
示例#9
0
    void ReadFile(RobotBasePtr& probot, const std::string& filename, const AttributesList& atts)
    {
        std::ifstream f(filename.c_str());
        if( !f ) {
            throw OPENRAVE_EXCEPTION_FORMAT("failed to read %s filename",filename,ORE_InvalidArguments);
        }
        f.seekg(0,ios::end);
        std::vector<char> filedata(static_cast<size_t>(f.tellg())+1, 0); // need a null-terminator
        f.seekg(0,ios::beg);
        f.read(&filedata[0], filedata.size());
        Read(probot,filedata,atts);
        probot->__struri = filename;
#if defined(HAVE_BOOST_FILESYSTEM) && BOOST_VERSION >= 103600 // stem() was introduced in 1.36
        boost::filesystem::path bfpath(filename);
#if defined(BOOST_FILESYSTEM_VERSION) && BOOST_FILESYSTEM_VERSION >= 3
        probot->SetName(utils::ConvertToOpenRAVEName(bfpath.stem().string()));
#else
        probot->SetName(utils::ConvertToOpenRAVEName(bfpath.stem()));
#endif
#endif
    }
示例#10
0
static bool scriptcreatelinemap(const char* filename)
{
    Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(filename).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
    if(hFile == INVALID_HANDLE_VALUE)
    {
        GuiScriptError(0, "CreateFile failed...");
        return false;
    }
    unsigned int filesize = GetFileSize(hFile, 0);
    if(!filesize)
    {
        GuiScriptError(0, "Empty script...");
        return false;
    }
    Memory<char*> filedata(filesize + 1, "createlinemap:filedata");
    DWORD read = 0;
    if(!ReadFile(hFile, filedata(), filesize, &read, 0))
    {
        GuiScriptError(0, "ReadFile failed...");
        return false;
    }
    hFile.Close();
    int len = (int)strlen(filedata());
    char temp[256] = "";
    LINEMAPENTRY entry;
    memset(&entry, 0, sizeof(entry));
    std::vector<LINEMAPENTRY>().swap(linemap);
    for(int i = 0, j = 0; i < len; i++) //make raw line map
    {
        if(filedata()[i] == '\r' && filedata()[i + 1] == '\n') //windows file
        {
            memset(&entry, 0, sizeof(entry));
            int add = 0;
            while(temp[add] == ' ')
                add++;
            strcpy_s(entry.raw, temp + add);
            *temp = 0;
            j = 0;
            i++;
            linemap.push_back(entry);
        }
        else if(filedata()[i] == '\n') //other file
        {
            memset(&entry, 0, sizeof(entry));
            int add = 0;
            while(temp[add] == ' ')
                add++;
            strcpy_s(entry.raw, temp + add);
            *temp = 0;
            j = 0;
            linemap.push_back(entry);
        }
        else if(j >= 254)
        {
            memset(&entry, 0, sizeof(entry));
            int add = 0;
            while(temp[add] == ' ')
                add++;
            strcpy_s(entry.raw, temp + add);
            *temp = 0;
            j = 0;
            linemap.push_back(entry);
        }
        else
            j += sprintf(temp + j, "%c", filedata()[i]);
    }
    if(*temp)
    {
        memset(&entry, 0, sizeof(entry));
        strcpy_s(entry.raw, temp);
        linemap.push_back(entry);
    }
    int linemapsize = (int)linemap.size();
    while(!*linemap.at(linemapsize - 1).raw) //remove empty lines from the end
    {
        linemapsize--;
        linemap.pop_back();
    }
    for(int i = 0; i < linemapsize; i++)
    {
        LINEMAPENTRY cur = linemap.at(i);

        //temp. remove comments from the raw line
        char line_comment[256] = "";
        char* comment = strstr(&cur.raw[0], "//");
        if(comment && comment != cur.raw) //only when the line doesnt start with a comment
        {
            if(*(comment - 1) == ' ') //space before comment
            {
                strcpy_s(line_comment, comment);
                *(comment - 1) = '\0';
            }
            else //no space before comment
            {
                strcpy_s(line_comment, comment);
                *comment = 0;
            }
        }

        int rawlen = (int)strlen(cur.raw);
        if(!rawlen) //empty
        {
            cur.type = lineempty;
        }
        else if(!strncmp(cur.raw, "//", 2)) //comment
        {
            cur.type = linecomment;
            strcpy_s(cur.u.comment, cur.raw);
        }
        else if(cur.raw[rawlen - 1] == ':') //label
        {
            cur.type = linelabel;
            sprintf(cur.u.label, "l %.*s", rawlen - 1, cur.raw); //create a fake command for formatting
            strcpy_s(cur.u.label, StringUtils::Trim(cur.u.label).c_str());
            char temp[256] = "";
            strcpy_s(temp, cur.u.label + 2);
            strcpy_s(cur.u.label, temp); //remove fake command
            if(!*cur.u.label || !strcmp(cur.u.label, "\"\"")) //no label text
            {
                char message[256] = "";
                sprintf(message, "Empty label detected on line %d!", i + 1);
                GuiScriptError(0, message);
                std::vector<LINEMAPENTRY>().swap(linemap);
                return false;
            }
            int foundlabel = scriptlabelfind(cur.u.label);
            if(foundlabel) //label defined twice
            {
                char message[256] = "";
                sprintf(message, "Duplicate label \"%s\" detected on lines %d and %d!", cur.u.label, foundlabel, i + 1);
                GuiScriptError(0, message);
                std::vector<LINEMAPENTRY>().swap(linemap);
                return false;
            }
        }
        else if(scriptgetbranchtype(cur.raw) != scriptnobranch) //branch
        {
            cur.type = linebranch;
            cur.u.branch.type = scriptgetbranchtype(cur.raw);
            char newraw[MAX_SCRIPT_LINE_SIZE] = "";
            strcpy_s(newraw, StringUtils::Trim(cur.raw).c_str());
            int len = (int)strlen(newraw);
            for(int i = 0; i < len; i++)
                if(newraw[i] == ' ')
                {
                    strcpy_s(cur.u.branch.branchlabel, newraw + i + 1);
                    break;
                }
        }
        else
        {
            cur.type = linecommand;
            strcpy_s(cur.u.command, cur.raw);
        }

        //append the comment to the raw line again
        if(*line_comment)
            sprintf(cur.raw + rawlen, " %s", line_comment);
        linemap.at(i) = cur;
    }
    linemapsize = (int)linemap.size();
    for(int i = 0; i < linemapsize; i++)
    {
        auto & currentLine = linemap.at(i);
        if(currentLine.type == linebranch)  //invalid branch label
        {
            int labelline = scriptlabelfind(currentLine.u.branch.branchlabel);
            if(!labelline) //invalid branch label
            {
                char message[256] = "";
                sprintf(message, "Invalid branch label \"%s\" detected on line %d!", currentLine.u.branch.branchlabel, i + 1);
                GuiScriptError(0, message);
                std::vector<LINEMAPENTRY>().swap(linemap);
                return false;
            }
            else //set the branch destination line
                currentLine.u.branch.dest = scriptinternalstep(labelline);
        }
    }
    if(linemap.at(linemapsize - 1).type == linecomment || linemap.at(linemapsize - 1).type == linelabel) //label/comment on the end
    {
        memset(&entry, 0, sizeof(entry));
        entry.type = linecommand;
        strcpy_s(entry.raw, "ret");
        strcpy_s(entry.u.command, "ret");
        linemap.push_back(entry);
    }
    return true;
}
示例#11
0
// return 1 to say that a file was processed
static int maybe_process_file(struct sbuf *cb, struct sbuf *p1b, FILE *ucfp, const char *currentdata, const char *datadirtmp, const char *deltmppath, struct dpth *dpth, int *resume_partial, struct cntr *cntr, struct config *cconf)
{
	int pcmp;
//	logp("in maybe_proc %s\n", p1b->path);
	if(!(pcmp=sbuf_pathcmp(cb, p1b)))
	{
		int oldcompressed=0;

		// If the file type changed, I think it is time to back it
		// up again (for example, EFS changing to normal file, or
		// back again).
		if(cb->cmd!=p1b->cmd)
			return process_new_file(cb, p1b, ucfp, currentdata,
				datadirtmp, deltmppath,
				dpth, resume_partial,
				cntr, cconf);

		// mtime is the actual file data.
		// ctime is the attributes or meta data.
		if(cb->statp.st_mtime==p1b->statp.st_mtime
		  && cb->statp.st_ctime==p1b->statp.st_ctime)
		{
			// got an unchanged file
			//logp("got unchanged file: %s %c %c\n", cb->path, cb->cmd, p1b->cmd);
			return process_unchanged_file(cb, ucfp, cntr);
		}

		if(cb->statp.st_mtime==p1b->statp.st_mtime
		  && cb->statp.st_ctime!=p1b->statp.st_ctime)
		{
			// File data stayed the same, but attributes or meta
			// data changed. We already have the attributes, but
			// may need to get extra meta data.
			if(cb->cmd==CMD_ENC_METADATA
			  || p1b->cmd==CMD_ENC_METADATA
			// TODO: make unencrypted metadata use the librsync
			  || cb->cmd==CMD_METADATA
			  || p1b->cmd==CMD_METADATA
			  || cb->cmd==CMD_VSS
			  || p1b->cmd==CMD_VSS
			  || cb->cmd==CMD_ENC_VSS
			  || p1b->cmd==CMD_ENC_VSS
			  || cb->cmd==CMD_VSS_T
			  || p1b->cmd==CMD_VSS_T
			  || cb->cmd==CMD_ENC_VSS_T
			  || p1b->cmd==CMD_ENC_VSS_T
			  || cb->cmd==CMD_EFS_FILE
			  || p1b->cmd==CMD_EFS_FILE)
				return process_new_file(cb,
					p1b, ucfp, currentdata,
					datadirtmp, deltmppath,
					dpth, resume_partial,
					cntr, cconf);
			else
				return process_unchanged_file(cb, ucfp, cntr);
		}

		// Got a changed file.
		//logp("got changed file: %s\n", p1b->path);

		// If either old or new is encrypted, or librsync is off,
		// we need to get a new file.
		if(!cconf->librsync
		  || cb->cmd==CMD_ENC_FILE
		  || p1b->cmd==CMD_ENC_FILE
		  || cb->cmd==CMD_ENC_METADATA
		  || p1b->cmd==CMD_ENC_METADATA
		  || cb->cmd==CMD_EFS_FILE
		  || p1b->cmd==CMD_EFS_FILE
		// TODO: make unencrypted metadata use the librsync
		  || cb->cmd==CMD_METADATA
		  || p1b->cmd==CMD_METADATA
		  || cb->cmd==CMD_VSS
		  || p1b->cmd==CMD_VSS
		  || cb->cmd==CMD_ENC_VSS
		  || p1b->cmd==CMD_ENC_VSS
		  || cb->cmd==CMD_VSS_T
		  || p1b->cmd==CMD_VSS_T
		  || cb->cmd==CMD_ENC_VSS_T
		  || p1b->cmd==CMD_ENC_VSS_T)
			return process_new_file(cb, p1b, ucfp, currentdata,
				datadirtmp, deltmppath,
				dpth, resume_partial,
				cntr, cconf);

		// Get new files if they have switched between compression on
		// or off.
		if(cb->datapth && dpth_is_compressed(cb->compression, cb->datapth))
			oldcompressed=1;
		if( ( oldcompressed && !cconf->compression)
		 || (!oldcompressed &&  cconf->compression))
			return process_new_file(cb, p1b, ucfp, currentdata,
				datadirtmp, deltmppath,
				dpth, resume_partial,
				cntr, cconf);

		// Otherwise, do the delta stuff (if possible).
		if(filedata(p1b->cmd))
		{
			if(process_changed_file(cb, p1b, currentdata,
				datadirtmp, deltmppath, resume_partial,
				cntr, cconf)) return -1;
		}
		else
		{
			if(changed_non_file(p1b, ucfp, p1b->cmd, cntr))
				return -1;
		}
		free_sbuf(cb);
		return 1;
	}
	else if(pcmp>0)
	{
		//logp("ahead: %s\n", p1b->path);
		// ahead - need to get the whole file
		if(process_new(p1b, ucfp, currentdata, datadirtmp, deltmppath,
			dpth, resume_partial, cntr, cconf)) return -1;
		// do not free
		return 1;
	}
	else
	{
		//logp("behind: %s\n", p1b->path);
		// behind - need to read more from the old
		// manifest
		// Count a deleted file - it was in the old manifest but not
		// the new.
		do_filecounter_deleted(cntr, cb->cmd);
	}
	return 0;
}
示例#12
0
// returns 1 for finished ok.
static int do_stuff_to_receive(struct sbuf *rb, FILE *p2fp, const char *datadirtmp, struct dpth *dpth, const char *working, char **last_requested, const char *deltmppath, struct cntr *cntr, struct config *cconf)
{
	int ret=0;
	char rcmd;
	size_t rlen=0;
	size_t wlen=0;
	char *rbuf=NULL;

	// This also attempts to write anything in the write buffer.
	if(async_rw(&rcmd, &rbuf, &rlen, '\0', NULL, &wlen))
	{
		logp("error in async_rw\n");
		return -1;
	}

	if(rbuf)
	{
		if(rcmd==CMD_WARNING)
		{
			logp("WARNING: %s\n", rbuf);
			do_filecounter(cntr, rcmd, 0);
		}
		else if(rb->fp || rb->zp)
		{
			// Currently writing a file (or meta data)
			if(rcmd==CMD_APPEND)
			{
				int app;
				//logp("rlen: %d\n", rlen);
				if((rb->zp
				  && (app=gzwrite(rb->zp, rbuf, rlen))<=0)
				|| (rb->fp
				  && (app=fwrite(rbuf, 1, rlen, rb->fp))<=0))
				{
					logp("error when appending: %d\n", app);
					async_write_str(CMD_ERROR, "write failed");
					ret=-1;
				}
				do_filecounter_recvbytes(cntr, rlen);
			}
			else if(rcmd==CMD_END_FILE)
			{
				// Finished the file.
				// Write it to the phase2 file, and free the
				// buffers.

				if(close_fp(&(rb->fp)))
				{
					logp("error closing delta for %s in receive\n", rb->path);
					ret=-1;
				}
				if(gzclose_fp(&(rb->zp)))
				{
					logp("error gzclosing delta for %s in receive\n", rb->path);
					ret=-1;
				}
				rb->endfile=rbuf;
				rb->elen=rlen;
				rbuf=NULL;
				if(!ret && rb->receivedelta
				  && finish_delta(rb, working, deltmppath))
					ret=-1;
				else if(!ret)
				{
					if(sbuf_to_manifest(rb, p2fp, NULL))
						ret=-1;
					else
					{
					  char cmd=rb->cmd;
					  if(rb->receivedelta)
						do_filecounter_changed(cntr,
							cmd);
					  else
						do_filecounter(cntr, cmd, 0);
					  if(*last_requested
					  && !strcmp(rb->path, *last_requested))
					  {
						free(*last_requested);
						*last_requested=NULL;
					  }
					}
				}

				if(!ret)
				{
					char *cp=NULL;
					cp=strchr(rb->endfile, ':');
					if(rb->endfile)
					 do_filecounter_bytes(cntr,
					  strtoull(rb->endfile, NULL, 10));
					if(cp)
					{
						// checksum stuff goes here
					}
				}

				free_sbuf(rb);
			}
			else
			{
				logp("unexpected cmd from client while writing file: %c %s\n", rcmd, rbuf);
				ret=-1;
			}
		}
		// Otherwise, expecting to be told of a file to save.
		else if(rcmd==CMD_DATAPTH)
		{
			rb->datapth=rbuf;
			rbuf=NULL;
		}
		else if(rcmd==CMD_STAT)
		{
			rb->statbuf=rbuf;
			rb->slen=rlen;
			rbuf=NULL;
		}
		else if(filedata(rcmd))
		{
			rb->cmd=rcmd;
			rb->plen=rlen;
			rb->path=rbuf;
			rbuf=NULL;

			if(rb->datapth)
			{
				// Receiving a delta.
				if(start_to_receive_delta(rb,
				  working, deltmppath, cconf))
				{
					logp("error in start_to_receive_delta\n");
					ret=-1;
				}
			}
			else
			{
				// Receiving a whole new file.
				if(start_to_receive_new_file(rb,
					datadirtmp, dpth, cntr, cconf))
				{
					logp("error in start_to_receive_new_file\n");
					ret=-1;
				}
			}
		}
		else if(rcmd==CMD_GEN && !strcmp(rbuf, "okbackupphase2end"))
		{
			ret=1;
			//logp("got okbackupphase2end\n");
		}
		else if(rcmd==CMD_INTERRUPT)
		{
			// Interrupt - forget about the last requested file
			// if it matches. Otherwise, we can get stuck on the
			// select in the async stuff, waiting for something
			// that will never arrive.
			if(*last_requested && !strcmp(rbuf, *last_requested))
			{
				free(*last_requested);
				*last_requested=NULL;
			}
		}
		else
		{
			logp("unexpected cmd from client while expecting a file: %c %s\n", rcmd, rbuf);
			ret=-1;
		}

		if(rbuf) { free(rbuf); rbuf=NULL; }
	}

	//logp("returning: %d\n", ret);
	return ret;
}
示例#13
0
// returns 1 for finished ok.
static int do_stuff_to_receive(struct asfd *asfd,
	struct sdirs *sdirs, struct conf *cconf,
	struct sbuf *rb, FILE *p2fp, struct dpthl *dpthl, char **last_requested)
{
	struct iobuf *rbuf=asfd->rbuf;

	iobuf_free_content(rbuf);
	// This also attempts to write anything in the write buffer.
	if(asfd->as->rw(asfd->as))
	{
		logp("error in async_rw\n");
		return -1;
	}

	if(!rbuf->buf) return 0;

	if(rbuf->cmd==CMD_WARNING)
	{
		logp("WARNING: %s\n", rbuf->buf);
		cntr_add(cconf->cntr, rbuf->cmd, 0);
	}
	else if(rb->burp1->fp || rb->burp1->zp)
	{
		// Currently writing a file (or meta data)
		if(rbuf->cmd==CMD_APPEND)
		{
			if(deal_with_receive_append(asfd, rb, cconf))
				goto error;
		}
		else if(rbuf->cmd==CMD_END_FILE)
		{
			if(deal_with_receive_end_file(asfd, sdirs, rb, p2fp,
				cconf, last_requested)) goto error;
		}
		else
		{
			iobuf_log_unexpected(rbuf, __func__);
			goto error;
		}
	}
	// Otherwise, expecting to be told of a file to save.
	else if(rbuf->cmd==CMD_DATAPTH)
	{
		iobuf_copy(&rb->burp1->datapth, rbuf);
		rbuf->buf=NULL;
	}
	else if(rbuf->cmd==CMD_ATTRIBS)
	{
		iobuf_copy(&rb->attr, rbuf);
		rbuf->buf=NULL;
	}
	else if(filedata(rbuf->cmd))
	{
		iobuf_copy(&rb->path, rbuf);
		rbuf->buf=NULL;

		if(rb->burp1->datapth.buf)
		{
			// Receiving a delta.
			if(start_to_receive_delta(sdirs, cconf, rb))
			{
				logp("error in start_to_receive_delta\n");
				goto error;
			}
		}
		else
		{
			// Receiving a whole new file.
			if(start_to_receive_new_file(asfd,
				sdirs, cconf, rb, dpthl))
			{
				logp("error in start_to_receive_new_file\n");
				goto error;
			}
		}
	}
	else if(rbuf->cmd==CMD_GEN
	  && !strcmp(rbuf->buf, "okbackupphase2end"))
		goto end_phase2;
	else if(rbuf->cmd==CMD_INTERRUPT)
	{
		// Interrupt - forget about the last requested file
		// if it matches. Otherwise, we can get stuck on the
		// select in the async stuff, waiting for something
		// that will never arrive.
		if(*last_requested && !strcmp(rbuf->buf, *last_requested))
		{
			free(*last_requested);
			*last_requested=NULL;
		}
	}
	else
	{
		iobuf_log_unexpected(rbuf, __func__);
		goto error;
	}

	return 0;
end_phase2:
	return 1;
error:
	return -1;
}
示例#14
0
// return 1 to say that a file was processed
static int maybe_process_file(struct asfd *asfd,
	struct sdirs *sdirs, struct conf *cconf,
	struct sbuf *cb, struct sbuf *p1b, FILE *ucfp,
	struct dpthl *dpthl)
{
	int pcmp;
	if(!(pcmp=sbuf_pathcmp(cb, p1b)))
	{
		int oldcompressed=0;

		// If the file type changed, I think it is time to back it
		// up again (for example, EFS changing to normal file, or
		// back again).
		if(cb->path.cmd!=p1b->path.cmd)
			return process_new_file(sdirs, cconf, cb, p1b, ucfp,
				dpthl);

		// mtime is the actual file data.
		// ctime is the attributes or meta data.
		if(cb->statp.st_mtime==p1b->statp.st_mtime
		  && cb->statp.st_ctime==p1b->statp.st_ctime)
		{
			// got an unchanged file
			//logp("got unchanged file: %s %c %c\n", cb->path, cb->cmd, p1b->cmd);
			return process_unchanged_file(cb, ucfp, cconf);
		}

		if(cb->statp.st_mtime==p1b->statp.st_mtime
		  && cb->statp.st_ctime!=p1b->statp.st_ctime)
		{
			// File data stayed the same, but attributes or meta
			// data changed. We already have the attributes, but
			// may need to get extra meta data.
			if(cb->path.cmd==CMD_ENC_METADATA
			  || p1b->path.cmd==CMD_ENC_METADATA
			// TODO: make unencrypted metadata use the librsync
			  || cb->path.cmd==CMD_METADATA
			  || p1b->path.cmd==CMD_METADATA
			  || cb->path.cmd==CMD_VSS
			  || p1b->path.cmd==CMD_VSS
			  || cb->path.cmd==CMD_ENC_VSS
			  || p1b->path.cmd==CMD_ENC_VSS
			  || cb->path.cmd==CMD_VSS_T
			  || p1b->path.cmd==CMD_VSS_T
			  || cb->path.cmd==CMD_ENC_VSS_T
			  || p1b->path.cmd==CMD_ENC_VSS_T
			  || cb->path.cmd==CMD_EFS_FILE
			  || p1b->path.cmd==CMD_EFS_FILE)
				return process_new_file(sdirs, cconf, cb,
					p1b, ucfp, dpthl);
			// On Windows, we have to back up the whole file if
			// ctime changed, otherwise things like permission
			// changes do not get noticed. So, in that case, fall
			// through to the changed stuff below.
			// Non-Windows clients finish here.
			else if(!cconf->client_is_windows)
				return process_unchanged_file(cb, ucfp, cconf);
		}

		// Got a changed file.
		//logp("got changed file: %s\n", p1b->path);

		// If either old or new is encrypted, or librsync is off,
		// we need to get a new file.
		if(!cconf->librsync
		  || cb->path.cmd==CMD_ENC_FILE
		  || p1b->path.cmd==CMD_ENC_FILE
		  || cb->path.cmd==CMD_ENC_METADATA
		  || p1b->path.cmd==CMD_ENC_METADATA
		  || cb->path.cmd==CMD_EFS_FILE
		  || p1b->path.cmd==CMD_EFS_FILE
		// TODO: make unencrypted metadata use the librsync
		  || cb->path.cmd==CMD_METADATA
		  || p1b->path.cmd==CMD_METADATA
		  || cb->path.cmd==CMD_VSS
		  || p1b->path.cmd==CMD_VSS
		  || cb->path.cmd==CMD_ENC_VSS
		  || p1b->path.cmd==CMD_ENC_VSS
		  || cb->path.cmd==CMD_VSS_T
		  || p1b->path.cmd==CMD_VSS_T
		  || cb->path.cmd==CMD_ENC_VSS_T
		  || p1b->path.cmd==CMD_ENC_VSS_T)
			return process_new_file(sdirs, cconf, cb, p1b, ucfp,
				dpthl);

		// Get new files if they have switched between compression on
		// or off.
		if(cb->burp1->datapth.buf
		  && dpthl_is_compressed(cb->compression, cb->burp1->datapth.buf))
			oldcompressed=1;
		if( ( oldcompressed && !cconf->compression)
		 || (!oldcompressed &&  cconf->compression))
			return process_new_file(sdirs, cconf, cb, p1b, ucfp,
				dpthl);

		// Otherwise, do the delta stuff (if possible).
		if(filedata(p1b->path.cmd))
		{
			if(process_changed_file(asfd, sdirs, cconf, cb, p1b,
				sdirs->currentdata)) return -1;
		}
		else
		{
			if(changed_non_file(p1b, ucfp, p1b->path.cmd, cconf))
				return -1;
		}
		sbuf_free_content(cb);
		return 1;
	}
	else if(pcmp>0)
	{
		//logp("ahead: %s\n", p1b->path);
		// ahead - need to get the whole file
		if(process_new(sdirs, cconf, p1b, ucfp, dpthl)) return -1;
		// do not free
		return 1;
	}
	else
	{
		//logp("behind: %s\n", p1b->path);
		// behind - need to read more from the old
		// manifest
		// Count a deleted file - it was in the old manifest but not
		// the new.
		cntr_add_deleted(cconf->cntr, cb->path.cmd);
	}
	return 0;
}