Beispiel #1
0
/* Scan /proc/devices for a blockdevice (BLOCKDEV is 1) or a character
 * device (BLOCKDEV is 0) with a major number matching the major number of DEV.
 * When there is a match, store entry data in ENTRY and return 0. Return
 * non-zero otherwise. */
int
proc_dev_get_entry(dev_t device, int blockdev, struct proc_dev_entry* entry)
{
	struct file_buffer file;
	int rc;
	int scan_blockdev = 0;

	rc = get_file_buffer(&file, proc_dev_filename);
	if (rc)
		return rc;
	rc = -1;
	while (!eof(&file)) {
		if (scan_string(&file, "Block") == 0) {
			skip_line(&file);
			scan_blockdev = 1;
			continue;
		} else if (scan_dev_entry(&file, entry, scan_blockdev) == 0) {
			if ((major(entry->device) == major(device)) &&
			    blockdev == scan_blockdev) {
				rc = 0;
				break;
			}
			proc_dev_free_entry(entry);
		} else
			skip_line(&file);
	}
	free_file_buffer(&file);
	return rc;
}
Beispiel #2
0
/* Scan /proc/partitions for an entry matching DEVICE. When there is a match,
 * store entry data in ENTRY and return 0. Return non-zero otherwise. */
int
proc_part_get_entry(dev_t device, struct proc_part_entry* entry)
{
	struct file_buffer file;
	int rc;

	rc = get_file_buffer(&file, proc_part_filename);
	if (rc)
		return rc;
	rc = -1;
	while (!eof(&file)) {
		if (scan_part_entry(&file, entry) == 0) {
			if (entry->device == device) {
				rc = 0;
				break;
			}
			proc_part_free_entry(entry);
		} else
			skip_line(&file);
	}
	free_file_buffer(&file);
	return rc;
}
Beispiel #3
0
static int create_files(char *dir_name, int inode)
{
	DIR *dir;
	struct dirent *pdir;
	struct file_buffer *bdir;
	struct file_buffer *bfile;
	char *name;
	char *tmp;
	int len = 0;
	int file;
	int ret = 0;
	int finode = 0;

	dir = opendir(dir_name);
	if (dir == NULL) {
		printf("Can not open dir %s\n", dir_name);
		return -EACCES;
	}

	bdir = allocate_file_buffer();
	if (!bdir) {
		ret = -ENOMEM;
		goto close_dir;
	}
	init_file_buffer(bdir, DT_DIR);
	bdir->inode = inode;

	bfile = allocate_file_buffer();
	if (!bdir) {
		ret = -ENOMEM;
		goto release_bdir;
	}

	name = malloc(1024);
	if (!name) {
		ret = -ENOMEM;
		goto release_bfile;
	}
	memset(name, 0, 1024);

	init_inode_entry(bdir, inode, DT_DIR);

	while (pdir = readdir(dir)) {
		if (!strcmp(pdir->d_name, "."))
			continue;
		if(!strcmp(pdir->d_name, ".."))
			continue;

		finode = pop_inode();
		if (!finode)
			goto release_bfile;
		
		strcpy(name, dir_name);
		len = strlen(name);
		if (name[len - 1] != '/')
			strcat(name, "/");
		strcat(name, pdir->d_name);
		printf("Add file [ %s ] to disk\n", name);
		
		if (pdir->d_type == DT_DIR) {
			write_entry_to_dir(bdir, pdir->d_name, finode, DT_DIR);
			create_files(name, finode);
		} else {
			write_entry_to_dir(bdir, pdir->d_name, finode, DT_BLK);
			init_file_buffer(bfile, TYPE_FILE);
			bfile->inode = finode;
			init_inode_entry(bfile, finode, DT_BLK);
			file = open(name, O_RDONLY);
			if(file < 0) {
				printf("can not open file %s\n", name);
				continue;
			}
			write_file(file, bfile);
		}
	}

	write_inode(bdir);

release_file_name_buffer:
	free(name);
release_bdir:
	free_file_buffer(bfile);
release_bfile:
	free_file_buffer(bdir);
close_dir:
	closedir(dir);

	return ret;
}