コード例 #1
0
ファイル: sfx.cpp プロジェクト: landswellsong/FAR
void attach_sfx_module(const wstring& file_path, const SfxOptions& sfx_options) {
  AttachSfxModuleProgress progress(file_path);

  {
    OpenOptions options;
    options.arc_path = file_path;
    options.detect = false;
    options.arc_types.push_back(c_7z);
    unique_ptr<Archives> archives(Archive::open(options));
    if (archives->empty())
      FAIL_MSG(Far::get_msg(MSG_ERROR_SFX_CONVERT));
    if (!archives->front()->is_pure_7z())
      FAIL_MSG(Far::get_msg(MSG_ERROR_SFX_CONVERT));
  }

  FindData file_data = File::get_find_data(file_path);
  progress.set_total(file_data.size());
  wstring dst_path = file_path + c_sfx_ext;
  try {
    create_sfx_module(dst_path, sfx_options);

    File dst_file(dst_path, FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ, OPEN_EXISTING, 0);
    dst_file.set_pos(0, FILE_END);

    File src_file(file_path, FILE_READ_DATA, FILE_SHARE_READ, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN);
    Buffer<char> buf(1024 * 1024);
    while (true) {
      unsigned size_read = src_file.read(buf.data(), static_cast<unsigned>(buf.size()));
      if (size_read == 0)
        break;
      CHECK(dst_file.write(buf.data(), size_read) == size_read);
      progress.update_completed(size_read);
    }

    File::set_attr(file_path, file_data.dwFileAttributes);
    dst_file.set_time(file_data.ftCreationTime, file_data.ftLastAccessTime, file_data.ftLastWriteTime);
  }
  catch (...) {
    File::delete_file_nt(dst_path);
    throw;
  }
  File::delete_file_nt(file_path);
}
コード例 #2
0
ファイル: CommandCopy.cpp プロジェクト: unoyx/vfs
int CommandCopy::copyFile(MyString src, MyString dst, VirtualDiskNode* vfs)
{
    HANDLE src_file = CreateFile(src.c_str(),
                                 GENERIC_READ,
                                 FILE_SHARE_READ,
                                 nullptr,
                                 OPEN_EXISTING,
                                 FILE_ATTRIBUTE_NORMAL,
                                 nullptr);
    if (src_file == INVALID_HANDLE_VALUE) 
    {
        throw CommandException(_T("系统找不到指定的文件\n"));
    }
    DWORD file_size = GetFileSize(src_file, nullptr);
    //CHAR *src_buf = new CHAR[file_size];
    DelegateMem<char> src_buf(new char[file_size]);
    DWORD read_bytes = 0;
    if (!ReadFile(src_file, 
                  src_buf,
                  file_size,
                  &read_bytes,
                  nullptr)
        || file_size != read_bytes)
    {
        //delete[] src_buf;
        CloseHandle(src_file);
        throw CommandException(src + _T("文件读取失败\n"));
    }

    FileHandler dst_file(nullptr);
    if (vfs->isFile(dst))
    {
        _tprintf(_T("覆盖文件%s,是否确认<Y/N>?"), basename(dst).c_str());
        TCHAR confirm = 0;
        _tscanf_s(_T("%c%*c"), &confirm, sizeof(confirm));
        if (confirm != 'y' && confirm != 'Y')
        {
            return 0;
        }
        dst_file = vfs->openFile(dst);
        if (!dst_file.isValid())
        {
            //delete[] src_buf;
            CloseHandle(src_file);
            throw CommandException(_T("无法打开/创建目标文件\n"));
        }
    }
    else
    {
        dst_file = vfs->createFile(dst);
        if (!dst_file.isValid())
        {
            CloseHandle(src_file);
            throw CommandException(_T("无法打开/创建目标文件\n"));
        }
    }
    // TODO : 文件写入
    dst_file.write(src_buf, file_size);
    //delete[] src_file;
    CloseHandle(src_file);
    return 0;
}