📄 creatvdiskdlg.cpp
字号:
// CreatVDiskDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ULExplorer.h"
#include "CreatVDiskDlg.h"
#include "UL_FileSys.h"
// CCreatVDiskDlg 对话框
IMPLEMENT_DYNAMIC(CCreatVDiskDlg, CDialog)
CCreatVDiskDlg::CCreatVDiskDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCreatVDiskDlg::IDD, pParent)
{
}
CCreatVDiskDlg::~CCreatVDiskDlg()
{
}
void CCreatVDiskDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_PATH, m_ctlEditPath);
DDX_Control(pDX, IDC_EDIT_SIZE, m_ctlEditSize);
DDX_Control(pDX, IDC_SPIN_SIZE, m_ctlSpin);
DDX_Control(pDX, ID_FILE_BROWSE, m_BtnBrowse);
DDX_Control(pDX, IDOK, m_BtnOK);
DDX_Control(pDX, IDCANCEL, m_BtnCancel);
DDX_Control(pDX, IDC_PROGRESS, m_ctlProgress);
}
BEGIN_MESSAGE_MAP(CCreatVDiskDlg, CDialog)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_BN_CLICKED(ID_FILE_BROWSE, OnBnClickedFileBrowse)
END_MESSAGE_MAP()
// CCreatVDiskDlg 消息处理程序
BOOL CCreatVDiskDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_ctlEditSize.SetWindowText("16");
m_ctlSpin.SetBuddy(&m_ctlEditSize);
m_ctlSpin.SetRange(1, 32);
m_ctlProgress.SetRange32(0, 50);
/*
m_BtnBrowse
m_BtnOK
m_BtnCancel
*/
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
#define SUPER_MAGIC 0x231 // 文件系统魔数。
// 磁盘上超级块结构
struct d_super_block
{
unsigned short s_ninodes; // 节点数
unsigned short s_nzones; // 逻辑块数
unsigned short s_imap_blocks; // i 节点位图所占用的数据块数
unsigned short s_zmap_blocks; // 逻辑块位图所占用的数据块数
unsigned short s_firstdatazone; // 第一个数据逻辑块
unsigned short s_log_zone_size; // log(数据块数/逻辑块)(以2 为底)
unsigned long s_max_size; // 文件最大长度
unsigned short s_magic; // 文件系统魔数
};
struct d_inode
{
unsigned short i_mode; // 文件类型和属性(rwx 位)
unsigned short i_uid; // 用户id(文件拥有者标识符)
unsigned long i_size; // 文件大小(字节数)
unsigned long i_time; // 修改时间(自1970.1.1:0 算起,秒)
unsigned char i_gid; // 组id(文件拥有者所在的组)
unsigned char i_nlinks; // 链接数(多少个文件目录项指向该i 节点)
unsigned short i_zone[9]; // 直接(0-6),间接(7)或双重间接(8)逻辑块号
};
struct dir_entry
{
unsigned short inode; // i 节点
char name[NAME_LEN]; // 文件名
};
extern int set_bit(unsigned short bitnr, char *addr);
void CCreatVDiskDlg::OnBnClickedOk()
{
int i;
CString tmp;
m_ctlEditSize.GetWindowText(tmp);
unsigned long size = atol(tmp);
if (size<1||size>32)
return;
size *= 1024;
m_ctlEditPath.GetWindowText(tmp);
CFile File;
if (!File.Open(tmp, CFile::modeCreate|CFile::modeWrite))
{
AfxMessageBox("虚拟磁盘文件创建失败");
m_ctlProgress.SetPos(0);
return;
}
m_ctlProgress.SetPos(9);
char buf[1024];
ZeroMemory(buf, 1024);
/*
for (i=0; i<size; i++)
{
File.Write(buf, 1024);
}
*/
File.Seek(1024*(size-1), CFile::begin);
File.Write(buf, 1024);
m_ctlProgress.SetPos(10);
unsigned long Pos = 0;
// 第一个块为引导块(清空)
File.Seek(Pos, CFile::begin);
File.Write(buf, 1024);
m_ctlProgress.SetStep(12);
// 超级块
struct d_super_block superb;
superb.s_ninodes = 1024; // 节点总数
superb.s_nzones = size; // 逻辑块总数16M
superb.s_imap_blocks = 1; // i 节点位图所占用的数据块数
unsigned short zb = size/(1024*8);
superb.s_zmap_blocks = (size-1024*8*zb>0)?zb+1:zb; // 逻辑块位图所占用的数据块数
superb.s_firstdatazone = 2+superb.s_imap_blocks+superb.s_zmap_blocks+32; // 第一个数据逻辑块
superb.s_log_zone_size = 0; // log(数据块数/逻辑块)(以2 为底)
superb.s_max_size = 1024*1024; // 文件最大长度
superb.s_magic = SUPER_MAGIC; // 文件系统魔数
Pos += 1024;
//memset(buf, 'S', 1024);
//File.Seek(Pos, CFile::begin);
//File.Write(buf, 1024);
File.Seek(Pos, CFile::begin);
File.Write(&superb, sizeof(struct d_super_block));
m_ctlProgress.SetPos(13);
// i节点位图 块位图
char* bitmap = (char*)malloc(superb.s_imap_blocks*1024);
memset(bitmap, 0, superb.s_imap_blocks*1024);
set_bit(0, bitmap);
set_bit(1, bitmap);
ZeroMemory(buf, 1024);
Pos += 1024;
File.Seek(Pos, CFile::begin);
File.Write(bitmap, 1024);
m_ctlProgress.SetPos(14);
free(bitmap);
//
bitmap = (char*)malloc(superb.s_zmap_blocks*1024);
memset(bitmap, 0, superb.s_zmap_blocks*1024);
//for (i=0; i<2+superb.s_imap_blocks+superb.s_zmap_blocks+32+1; i++)
set_bit(0, bitmap);
set_bit(1, bitmap);
Pos += 1024;
File.Seek(Pos, CFile::begin);
File.Write(bitmap, 1024);
m_ctlProgress.SetPos(15);
Pos += 1024;
File.Seek(Pos, CFile::begin);
File.Write(buf, 1024);
m_ctlProgress.SetPos(16);
free(bitmap);
// inode表
for(i=0; i<32; i++)
{
Pos += 1024;
File.Seek(Pos, CFile::begin);
File.Write(buf, 1024);
m_ctlProgress.SetPos(17+i);
}
// 建立root inode
d_inode inode;
memset(&inode, 0, sizeof(d_inode));
inode.i_gid = 0;
inode.i_mode = S_IFDIR;
inode.i_nlinks = 1;
inode.i_size = sizeof(struct dir_entry)*2;
inode.i_time = time(0);
inode.i_uid = 0;
inode.i_zone[0] = 2+superb.s_imap_blocks+superb.s_zmap_blocks+32;
Pos = (2+superb.s_imap_blocks+superb.s_zmap_blocks)*1024;
File.Seek(Pos, CFile::begin);
File.Write(&inode, sizeof(d_inode));
m_ctlProgress.SetPos(49);
//
struct dir_entry *de = (struct dir_entry *)buf;
de->inode = ROOT_INO;
strcpy (de->name, ".");
de++;
de->inode = ROOT_INO;
strcpy (de->name, "..");
Pos = ((2+superb.s_imap_blocks+superb.s_zmap_blocks)+32)*1024;
File.Seek(Pos, CFile::begin);
File.Write(buf, sizeof(struct dir_entry)*2);
m_ctlProgress.SetPos(50);
File.Close();
OnOK();
}
void CCreatVDiskDlg::OnBnClickedFileBrowse()
{
CFileDialog FileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "虚拟盘文件 (*.vhd)|*.vhd");
if (FileDlg.DoModal()==IDOK)
{
CString path = FileDlg.GetPathName();
if ( (path.Right(4)).MakeLower()!=".vhd")
path += ".vhd";
m_ctlEditPath.SetWindowText(path);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -