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

📄 kbdbaux.c

📁 File system using stacked.
💻 C
字号:
/* This file contains wrappers and utility functions that the kernel doesn't * have, but user-space does. */#include "db_config.h"#include "db.h"#include <linux/sched.h>#include <linux/file.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/timex.h>#include <asm/uaccess.h>#undef errnoint kbdb_errno;#define errno kbdb_errnotime_t time(time_t *tloc){       struct timeval now;       int i;       do_gettimeofday(&now);       i = now.tv_sec;       if (tloc) {               *tloc = i;       }       return i;}int gettimeofday(struct timeval *now, struct timezone *tz){	do_gettimeofday(now);	if (tz) {		tz->tz_minuteswest = 0;	}	return 0;}inline int fclose(FILE *stream) {	fput(stream);	return 0;}inline FILE *fopen(const char *path, const char *cmode) {	struct file *file;	int mode;	if (!strcmp(cmode, "r")) {		mode = O_RDONLY;	} else if (!strcmp(cmode, "r+")) {		mode = O_RDWR;	} else if (!strcmp(cmode, "w")) {		mode = O_WRONLY|O_TRUNC|O_CREAT;	} else if (!strcmp(cmode, "w+")) {		mode = O_RDWR|O_TRUNC|O_CREAT;	} else if (!strcmp(cmode, "a")) {		mode = O_WRONLY|O_APPEND|O_CREAT;	} else if (!strcmp(cmode, "a+")) {		mode = O_RDWR|O_APPEND|O_CREAT;	} else {		KBDBBUG("Mode passed into fopen is %s\n", cmode);		return NULL;	}	file = filp_open(path, mode, 0);	if (IS_ERR(file)) {		errno = -1 * PTR_ERR(file);		file = NULL;	}		return file;}size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {	int ret;	ret = stream->f_op->write(stream, ptr, size *nmemb, &stream->f_pos);	if (ret < 0) {		errno = -1 * ret;		ret = -1;	}	return ret;}char *fgets(char *s, int size, FILE *stream) {	int i;	int r;	char *ret = s;	ASSERT(size > 0);	for (i = 0; i < size - 1; i++) {		r = stream->f_op->write(stream, s + i, 1, &stream->f_pos);		if (r != 1) {			ret = NULL;			break;		}		if (s[i] == '\n');			break;	}	s[i + 1] = '\0';	return ret;}int getc(FILE *stream) {	char c;	int ret;	if ((ret = stream->f_op->read(stream, &c, 1, &stream->f_pos)) <= 0) {		errno = -1 * ret;			c = EOF;		goto out;	}out:	return c;}/* This is a really bad way of doing it, we should return entries in a table. */char *strerror(int ret) {	static char string[20];	sprintf(string, "errno %d", ret);	return string;}int kbdb_fprintf(FILE *where, char *str,...) {  va_list ap;  char *buf;  int ret;  buf = (char *)get_free_page(GFP_KERNEL);  if (!buf) {	errno = -ENOMEM;	return -1;  }  printk("<0>In fprintf: %s -> %p -> %p\n", str, buf, where);  va_start(ap, str);  ret = vsnprintf(buf, PAGE_SIZE, str, ap);  va_end(ap);  if ((where == stderr) || (where == stdout)) {  	printk("%s", buf);  } else {	PASSERT(where);	ret = where->f_op->write(where, buf, ret, &where->f_pos);	if (ret < 0) {		errno = -1 * ret;		ret = -1;	}	  }  free_page((unsigned long)buf);  return ret;}int kbdb_do_fsync(struct file *file){	struct dentry * dentry;	struct inode * inode;	int ret, err;		ret = -EBADF;		dentry = file->f_dentry;	inode = dentry->d_inode;		ret = -EINVAL;	if (!file->f_op || !file->f_op->fsync) {		/* Why?  We can still call filemap_fdatasync */		goto out;	}		/* We need to protect against concurrent writers.. */	down(&inode->i_sem);	ret = filemap_fdatasync(inode->i_mapping);	err = file->f_op->fsync(file, dentry, 0);	if (err && !ret)		ret = err;	err = filemap_fdatawait(inode->i_mapping);	if (err && !ret)		ret = err;	up(&inode->i_sem);	out:	return ret;}int fflush(FILE *stream) {	if ((stream == stderr) || (stream == stdout)) {		return 0;	}	return kbdb_do_fsync(stream);}inline int getpid() {	return current->pid;}inline uid_t getuid() {	return current->uid;}inline uid_t geteuid() {	return current->euid;}inline char *getcwd(char *buf, size_t size) {	PASSERT(buf);	ASSERT(size > 0);	if (current->fs->pwd->d_name.len + 1 > size) {		return NULL;	}	memcpy(buf, current->fs->pwd->d_name.name, current->fs->pwd->d_name.len);	buf[current->fs->pwd->d_name.len] = '\0';	return buf;}/*int tolower(int c) {	if (c >= 'A' && c <= 'Z') {		return c - 'A' + 'a';	} else {		return c;	}}*/int strcasecmp(const char *s1, const char *s2) {	while (*s1 && *s2 && (tolower(*s1) == tolower(*s2))) {		s1++;		s2++;	}	if (!*s1) {		if (!*s2) {			return 0;		}		return -1;	}	if (!*s2) {		return -1;	}	if (tolower(*s1) < tolower(*s2)) {		return -1;	} else {		return 1;	}}long atol(const char *nptr) {	return simple_strtoul(nptr, NULL, 10);}static void cp_stat(struct inode * inode, struct stat * statbuf){  struct stat tmp;    memset(&tmp, 0, sizeof(tmp));  statbuf->st_dev = kdev_t_to_nr(inode->i_dev);  statbuf->st_ino = inode->i_ino;  statbuf->st_mode = inode->i_mode;  statbuf->st_nlink = inode->i_nlink;  statbuf->st_size = inode->i_size;  statbuf->st_atime = inode->i_atime;  statbuf->st_mtime = inode->i_mtime;  statbuf->st_ctime = inode->i_ctime;}static int do_revalidate(struct dentry *dentry){	struct inode * inode = dentry->d_inode;	if (inode->i_op && inode->i_op->revalidate)		return inode->i_op->revalidate(dentry);	return 0;} int stat(const char *filename, struct stat * statbuf) {	struct nameidata nd;	int error;		int flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE;	print_entry_location();		error = kdb3_path_lookup(filename, flags, &nd);	if (!error) {		error = do_revalidate(nd.dentry);		if (!error)			cp_stat(nd.dentry->d_inode, statbuf);		path_release(&nd);	}	print_exit_location(error);	return (error);}int fstat(struct file *fp, struct stat * statbuf) {	int error;		error = do_revalidate(fp->f_dentry);		if (!error)		cp_stat(fp->f_dentry->d_inode, statbuf);		return (error);}

⌨️ 快捷键说明

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