/*
* linux/fs/stat.c
*
* (C) 1991 Linus Torvalds
*/

#include <errno.h>		// ͷļϵͳиֳš(Linus minix )
#include <sys/stat.h>		// ļ״̬ͷļļļϵͳ״̬ṹstat{}ͳ

#include <linux/fs.h>		// ļϵͳͷļļṹfile,buffer_head,m_inode ȣ
#include <linux/sched.h>	// ȳͷļṹtask_structʼ0 ݣ
// һЩйúͻȡǶʽຯ䡣
#include <linux/kernel.h>	// ںͷļһЩں˳úԭζ塣
#include <asm/segment.h>	// βͷļйضμĴǶʽຯ

//// ļ״̬Ϣ
// inode ļӦi ڵ㣬statbuf stat ļ״̬ṹָ룬ڴȡõ״̬Ϣ
static void
cp_stat (struct m_inode *inode, struct stat *statbuf)
{
  struct stat tmp;
  int i;

// ֤()ݵڴռ䡣
  verify_area (statbuf, sizeof (*statbuf));
// ȻʱӦڵϵϢ
  tmp.st_dev = inode->i_dev;	// ļڵ豸š
  tmp.st_ino = inode->i_num;	// ļi ڵš
  tmp.st_mode = inode->i_mode;	// ļԡ
  tmp.st_nlink = inode->i_nlinks;	// ļ
  tmp.st_uid = inode->i_uid;	// ļûid
  tmp.st_gid = inode->i_gid;	// ļid
  tmp.st_rdev = inode->i_zone[0];	// 豸(ļַļļ)
  tmp.st_size = inode->i_size;	// ļСֽļǳļ
  tmp.st_atime = inode->i_atime;	// ʱ䡣
  tmp.st_mtime = inode->i_mtime;	// ޸ʱ䡣
  tmp.st_ctime = inode->i_ctime;	// ڵ޸ʱ䡣
// Щ״̬ϢƵûС
  for (i = 0; i < sizeof (tmp); i++)
    put_fs_byte (((char *) &tmp)[i], &((char *) statbuf)[i]);
}

//// ļ״̬ϵͳú - ļȡļ״̬Ϣ
// filename ָļstatbuf Ǵ״̬ϢĻָ롣
// 0򷵻س롣
int
sys_stat (char *filename, struct stat *statbuf)
{
  struct m_inode *inode;

// ȸļҳӦi ڵ㣬򷵻ش롣
  if (!(inode = namei (filename)))
    return -ENOENT;
// i ڵϵļ״̬ϢƵûУͷŸi ڵ㡣
  cp_stat (inode, statbuf);
  iput (inode);
  return 0;
}

//// ļ״̬ϵͳ - ļȡļ״̬Ϣ
// fd ָļľ()statbuf Ǵ״̬ϢĻָ롣
// 0򷵻س롣
int
sys_fstat (unsigned int fd, struct stat *statbuf)
{
  struct file *f;
  struct m_inode *inode;

// ļֵһļNR_OPEN߸þļṹָΪգ
// Ӧļṹi ڵֶΪգس벢˳
  if (fd >= NR_OPEN || !(f = current->filp[fd]) || !(inode = f->f_inode))
    return -EBADF;
// i ڵϵļ״̬ϢƵûС
  cp_stat (inode, statbuf);
  return 0;
}
