Esempio n. 1
0
// renames file srcFilename to destFilename, returns true on success
bool Rename(const std::string &srcFilename, const std::string &destFilename)
{
	INFO_LOG(COMMON, "Rename: %s --> %s",
			srcFilename.c_str(), destFilename.c_str());
#ifdef _WIN32
	auto sf = UTF8ToTStr(srcFilename);
	auto df = UTF8ToTStr(destFilename);
	// The Internet seems torn about whether ReplaceFile is atomic or not.
	// Hopefully it's atomic enough...
	if (ReplaceFile(df.c_str(), sf.c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr))
		return true;
	// Might have failed because the destination doesn't exist.
	if (GetLastError() == ERROR_FILE_NOT_FOUND)
	{
		if (MoveFile(sf.c_str(), df.c_str()))
			return true;
	}
#else
	if (rename(srcFilename.c_str(), destFilename.c_str()) == 0)
		return true;
#endif
	ERROR_LOG(COMMON, "Rename: failed %s --> %s: %s",
			  srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg());
	return false;
}
Esempio n. 2
0
void winreplacefile(char *source, char *target)
{
	wchar_t wsource[PATH_MAX];
	wchar_t wtarget[PATH_MAX];

	int sz = MultiByteToWideChar(CP_UTF8, 0, source, -1, wsource, PATH_MAX);
	if (sz == 0)
	{
		winerror(&gapp, "cannot convert filename to Unicode");
		return;
	}

	sz = MultiByteToWideChar(CP_UTF8, 0, target, -1, wtarget, PATH_MAX);
	if (sz == 0)
	{
		winerror(&gapp, "cannot convert filename to Unicode");
		return;
	}

#if (_WIN32_WINNT >= 0x0500)
	ReplaceFile(wtarget, wsource, NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL);
#else
	DeleteFile(wtarget);
	MoveFile(wsource, wtarget);
#endif
}
Esempio n. 3
0
void indri::file::Path::rename( const std::string& oldName, const std::string& newName ) {
#ifndef WIN32
  int result = ::rename( oldName.c_str(), newName.c_str() );

  if( result != 0 ) {
    if( errno == EEXIST ) {
      LEMUR_THROW( LEMUR_IO_ERROR, "The destination file already exists: " + oldName );
    } else if( errno == EACCES || errno == EPERM ) {
      LEMUR_THROW( LEMUR_IO_ERROR, "Insufficient permissions to rename: '" + oldName + "' to '" + newName + "'." );
    } else {
      LEMUR_THROW( LEMUR_IO_ERROR, "Unable to rename: '" + oldName + "' to '" + newName + "'." );
    }
  }
#else
  BOOL result;

  if( Path::exists( newName ) ) {
    result = ReplaceFile( newName.c_str(), oldName.c_str(), NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL );
  } else {
    result = MoveFile( oldName.c_str(), newName.c_str() );
  }

  if( !result ) {
    LEMUR_THROW( LEMUR_IO_ERROR, "Unable to rename: '" + oldName + "' to '" + newName + "'." );
  }
#endif
}
Esempio n. 4
0
bool Encryption::Decrypt(TCHAR * file)
{
    pszSourceFile = file;
    pszDestinationFile = TEXT("temp");

    OpenFile();
    ReadFileType();

    if(bIsPlaintext)
    {
        if(hSourceFile)
            CloseHandle(hSourceFile);
        if(hDestinationFile)
            CloseHandle(hDestinationFile);
        DeleteFile(pszDestinationFile);
        return true;
    }

    DWORD dwCount;
    PBYTE pbBuffer = NULL;
    DWORD dwBlockLen;

    dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;

    if(!(pbBuffer = (PBYTE)malloc(dwBlockLen)))
        return false;

    bool fEOF = false;
    do
    {
        if(!ReadFile(hSourceFile, pbBuffer, dwBlockLen, &dwCount, NULL))
            return false;
        if(dwCount < dwBlockLen)
            fEOF = true;
        if(!CryptDecrypt(hKey, 0, fEOF, 0, pbBuffer, &dwCount))
            return false;
        if(!WriteFile(hDestinationFile, pbBuffer, dwCount, &dwCount, NULL))
            return false;
    } while(!fEOF);

    if(hSourceFile)
        CloseHandle(hSourceFile);
    if(hDestinationFile)
        CloseHandle(hDestinationFile);

    if(!ReplaceFile(pszSourceFile, pszDestinationFile, NULL, REPLACEFILE_WRITE_THROUGH, 0, 0))
        return false;

    return true;
}
Esempio n. 5
0
//----------------------------------------------------------------------------------------------
// Pause optimiser
//----------------------------------------------------------------------------------------------
BOOL OptimiseVGMPauses(char *filename) {
  gzFile *in,*out;
  struct TVGMHeader VGMHeader;
  char *outfilename,b0,b1,b2;
  long int OldLoopOffset;
  int i,PauseLength=0;

  if (!FileExists(filename)) return FALSE;

  // Open input file
  in=gzopen(filename,"rb");

  // Read its VGM header
  if(!ReadVGMHeader(in,&VGMHeader,FALSE)) {
    gzclose(in);
	return FALSE;
  }

  OldLoopOffset=VGMHeader.LoopOffset+LOOPDELTA;

  // Make the output filename...
  outfilename=MakeTempFilename(filename);
  
  // ...open it...
  out=gzopen(outfilename,"wb0");

  // ...skip to the data section...
  gzseek(in,VGM_DATA_OFFSET,SEEK_SET);
  gzseek(out,VGM_DATA_OFFSET,SEEK_SET);

  // ...and parse the input file
  // Go through the file; if it's a pause, add it to the total; if it's data, write the current total and zero it
  do {
    // Update loop point
    // Write any remaining pauyse being buffered first, though
    if (gztell(in)==OldLoopOffset) {
      WritePause(out,PauseLength);
      PauseLength=0;
      VGMHeader.LoopOffset=gztell(out)-LOOPDELTA;
    }

    b0=gzgetc(in);
    switch (b0) {
    case VGM_GGST:  // GG stereo
    case VGM_PSG:  // PSG write
      WritePause(out,PauseLength);
      PauseLength=0;
      b1=gzgetc(in);
      gzputc(out,b0);
      gzputc(out,b1);
      break;
    case VGM_YM2413:  // YM2413
    case VGM_YM2612_0:  // YM2612 port 0
    case VGM_YM2612_1:  // YM2612 port 1
    case VGM_YM2151:  // YM2151
    case 0x55:  // Reserved up to 0x5f
    case 0x56:  // All have 2 bytes of data
    case 0x57:
    case 0x58:
    case 0x59:
    case 0x5a:
    case 0x5b:
    case 0x5c:
    case 0x5d:
    case 0x5e:
    case 0x5f:
      WritePause(out,PauseLength);
      PauseLength=0;
      b1=gzgetc(in);
      b2=gzgetc(in);
      gzputc(out,b0);
      gzputc(out,b1);
      gzputc(out,b2);
      break;
    case VGM_PAUSE_WORD:  // Wait n samples
      b1=gzgetc(in);
      b2=gzgetc(in);
      PauseLength+=MAKEWORD(b1,b2);
      break;
    case VGM_PAUSE_60TH:  // Wait 1/60 s
      PauseLength+=LEN60TH;
      break;
    case VGM_PAUSE_50TH:  // Wait 1/50 s
      PauseLength+=LEN50TH;
      break;
//    case VGM_PAUSE_BYTE:  // Wait n samples
//      b1=gzgetc(in);
//      PauseLength+=b1;
//      break;
    case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
    case 0x78: case 0x79: case 0x7a: case 0x7b: case 0x7c: case 0x7d: case 0x7e: case 0x7f: // Wait 1-16 samples
      PauseLength+=(b0 & 0xf)+1;
      break;
    case VGM_END:  // End of sound data
      WritePause(out,PauseLength);
      PauseLength=0;
      b0=EOF;  // break out of loop
      break;
    default:
      break;
    }  // end switch
  } while (b0!=EOF);

  // At end:
  // 1. Write EOF mrker
  gzputc(out,VGM_END);
  // 2. Copy GD3 tag
  if (VGMHeader.GD3Offset) {
    struct TGD3Header GD3Header;
    int NewGD3Offset=gztell(out)-GD3DELTA;
    gzseek(in,VGMHeader.GD3Offset+GD3DELTA,SEEK_SET);
    gzread(in,&GD3Header,sizeof(GD3Header));
    gzwrite(out,&GD3Header,sizeof(GD3Header));
    for (i=0; i<GD3Header.Length; ++i) gzputc(out,gzgetc(in));
    VGMHeader.GD3Offset=NewGD3Offset;
  }
  // 3. Fill in VGM header
  VGMHeader.EoFOffset=gztell(out)-EOFDELTA;
  // LoopOffset updated while optimising

  gzclose(out);

  WriteVGMHeader(outfilename,VGMHeader);

  // Clean up
  gzclose(in);

  ReplaceFile(filename,outfilename);

  free(outfilename);

  return TRUE;
}
Esempio n. 6
0
int main(int argc, char *argv[])
{
  // if there is not one argument on the command line
  if (argc<1+1) {
    // print usage
    printf("Usage: Ecc <es_file_name>\n       -line\n");
    //quit
    return EXIT_FAILURE;
  }
  if(argc>2)
  {
    if(strcmp(argv[2],"-line")==0)
    {
      _bRemoveLineDirective=1;
    }
  }
  // open the input file
  _fInput = FOpen(argv[1], "r");
  //printf("%s\n", argv[1]);
  // open all the output files
  char *strImplementation    = ChangeFileNameExtension(argv[1], ".cpp_tmp");
  char *strImplementationOld = ChangeFileNameExtension(argv[1], ".cpp");
  char *strDeclaration       = ChangeFileNameExtension(argv[1], ".h_tmp");
  char *strDeclarationOld    = ChangeFileNameExtension(argv[1], ".h");
  char *strTables            = ChangeFileNameExtension(argv[1], "_tables.h_tmp");
  char *strTablesOld         = ChangeFileNameExtension(argv[1], "_tables.h");

  _fImplementation = FOpen(strImplementation, "w");
  _fDeclaration    = FOpen(strDeclaration   , "w");
  _fTables         = FOpen(strTables        , "w");
  // get the filename as preprocessor usable identifier
  _strFileNameBase = ChangeFileNameExtension(argv[1], "");
  _strFileNameBaseIdentifier = strdup(_strFileNameBase);
  {char *strNextSlash = _strFileNameBaseIdentifier;
  while((strNextSlash = strchr(strNextSlash, '/'))!=NULL) {
    *strNextSlash = '_';
  }}
  {char *strNextSlash = _strFileNameBaseIdentifier;
  while((strNextSlash = strchr(strNextSlash, '\\'))!=NULL) {
    *strNextSlash = '_';
  }}
  // print their headers
  PrintHeader(_fImplementation );
  PrintHeader(_fDeclaration    );
  PrintHeader(_fTables         );

  // remember input filename
  char strFullInputName[MAXPATHLEN];
  _fullpath(strFullInputName, argv[1], MAXPATHLEN);
  _strInputFileName = strFullInputName;
  TranslateBackSlashes(_strInputFileName);
  // make lex use the input file
  yyin = _fInput;

  // parse input file and generate the output files
  yyparse();

  // close all files
  fclose(_fImplementation);
  fclose(_fDeclaration);
  fclose(_fTables);

  // if there were no errors
  if (ctErrors==0) {
    // update the files that have changed
    ReplaceFile(strImplementationOld, strImplementation);
    ReplaceIfChanged(strDeclarationOld, strDeclaration);
    ReplaceIfChanged(strTablesOld, strTables);
 
    return EXIT_SUCCESS;
  // if there were errors
  } else {
    // delete all files (the old declaration file is left intact!)
    remove(strImplementation);
    remove(strDeclaration   );
    remove(strTables        );
    return EXIT_FAILURE;
  }
}
Esempio n. 7
0
bool Encryption::Encrypt(TCHAR * file)
{
    pszSourceFile = file;
    pszDestinationFile = TEXT("temp");

    OpenFile();
    ReadFileType();

    if(bIsCiphertext)
    {
        if(hSourceFile)
            CloseHandle(hSourceFile);
        if(hDestinationFile)
            CloseHandle(hDestinationFile);
        DeleteFile(pszDestinationFile);
        return true;
    }

    // move file pointer to the start of the file
    DWORD dwPtr = SetFilePointer(hSourceFile, 0, NULL, FILE_BEGIN);

    WriteFileType();

    PBYTE pbKeyBlob = NULL;
    PBYTE pbBuffer = NULL;
    DWORD dwBlockLen;
    DWORD dwBufferLen;
    DWORD dwCount;

    // In a do loop, encrypt the source file, and write to the source file.
    dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;

    // Determine the block size. If a block cipher is used, it must have room for an extra block.
    if(ENCRYPT_BLOCK_SIZE > 1)
        dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
    else
        dwBufferLen = dwBlockLen;

    if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
        return false;

    bool fEOF = FALSE;
    do
    {
        if(!ReadFile(hSourceFile, pbBuffer, dwBlockLen, &dwCount, NULL))
            return false;

        if(dwCount < dwBlockLen)
            fEOF = TRUE;

        if(!CryptEncrypt(hKey,NULL, fEOF,0, pbBuffer,  &dwCount, dwBufferLen))
            return false;

        if(!WriteFile(hDestinationFile, pbBuffer, dwCount, &dwCount, NULL))
            return false;

    } while(!fEOF);

    if(hSourceFile)
        CloseHandle(hSourceFile);
    if(hDestinationFile)
        CloseHandle(hDestinationFile);

    if(!ReplaceFile(pszSourceFile, pszDestinationFile, NULL, REPLACEFILE_WRITE_THROUGH, 0, 0))
        return false;

    return true;
}