⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mkfs.c

📁 课程设计
💻 C
字号:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mount.h>

#include "xbfs.h"

int main(int argc, char *argv[])
{
	char *path;
	int fd;
	struct stat st;
	static struct xbfs_sb_disk sd __attribute__ ((aligned(BLK_SIZE)));
	static struct xbfs_inode_disk id __attribute__ ((aligned(BLK_SIZE)));
	unsigned long long size;
	unsigned long tot_count;
	static char buf[BLK_SIZE];
	int i;

	if (argc != 2) {
		printf(COPYRIGHT "usage: %s [blkdev]\n", path);
		return 0;
	}
	
	path = argv[1];
	fd = open(path, O_WRONLY, 0);
	if (fd < 0) {
		printf("open %s error\n", path);
		return 0;
	}

	if (fstat(fd, &st)) {
		printf("stat %s error\n", path);
		goto out_close;
	}

	if (S_ISBLK(st.st_mode)) {
		if (ioctl(fd, BLKGETSIZE64, &size)) {
			printf("get the size of %s error\n", path);
			goto out_close;
		}
	} else if (S_ISREG(st.st_mode)) {
		size = st.st_size;
	} else {
		printf("%s is not a BLK device or a regular file. cannot mkfs on it.\n", path);
		goto out_close;
	}
	
	tot_count = size >> BLK_SIZE_BITS;
	sd.data_count = tot_count - 1;
	sd.bmp_count = (sd.data_count + BLK_PER_BMP - 1) >> BLK_PER_BMP_BITS;
	sd.data_count -= sd.bmp_count;
	sd.bmp_start = 2;
	sd.data_start = sd.bmp_start + sd.bmp_count;
	sd.root_inode = sd.data_start;
	printf(	"size: %lld\n"
			"data blocks: %ld\n"
			"bitmap blocks: %ld\n"
			"total blocks: %ld\n"
			, size, sd.data_count, sd.bmp_count, tot_count);

	sd.magic = XBFS_MAGIC;

	// write super block
	lseek(fd, BLK_SIZE, SEEK_SET);
	write(fd, &sd, BLK_SIZE);
	
	// write bitmap
	lseek(fd, sd.bmp_start << BLK_SIZE_BITS, SEEK_SET);
	// root inode reserved
	buf[0] = 1;		
	write(fd, buf, BLK_SIZE);
	buf[0] = 0;
	for (i = 1; i < sd.bmp_count; i++)
		write(fd, buf, BLK_SIZE);

	// write dummy root inode
	id.mode = S_IFDIR;
	id.size = 0;
	lseek(fd, sd.data_start << BLK_SIZE_BITS, SEEK_SET);
	write(fd, &id, BLK_SIZE);

out_close:
	close(fd);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -