Introduction
本次需要代写的作业要求实现文件系统的高级操作,如查看文件的大小、创建时间、修改时间、用户、用户组等等meta信息。
由于属于操作系统层面的作业,因此如果不熟悉Linux C下的编程以及操作系统的知识的话,是没法做的。
Requirement
The program you will write will accept from 0 to an arbitrary number of
command line file names and produce an output structure for each identified
file (or all files in the working directory if no command line file names are
listed) as shown below:
FILENAME
FILE_TYPE
PERMISSIONS
OWNER_NAME
GROUP_NAME
DATE_OF_LAST_MODIFICATION
LINK_COUNT
SIZE_IN_BYTES OR DEV INFO
INODE_NUMBER
Example:
FILENAME:
alpha
FILE_TYPE:
ordinary
PERMISSIONS:
rw- r– r–
OWNER_NAME:
jedwards
GROUP_NAM:
grad
DATE_OF_LAST_MODIFICATION:
Mar 30 08:11 2003
LINK_COUNT:
SIZE_IN_BYTES:
1345 (or 12, 6 dev info)
INODE_NUMBER:
< a blank line between entries >
System calls needed on a UNIX system include:
#include <dirent.h>
getdirentries(int fd, char *buf, int nbytes, long *basep)
—|—
which reads up to nbytes of data into buf in the form:
unsigned long d_ino;
unsigned short d_reclen;
unsigned short d_namlen;
char d_name[MAXNAMLEN + 1];
—|—
see the man pages for more detail. This routine is difficult to use, so you
may find the library routines opendir() and readdir() easier to use as shown
in class.
#include <time.h>
char *ctime(long *clock)
—|—
ctime() converts a long integer, pointed to by clock, to a 26-character string
of the form:
Sun May 17 01:03:52 2015\n\0
see the man pages for more detail.
#include <sys/types.h>
#include <sys/stat.h>
lstat(char *path, struct stat buf)
—|—
which fills in a data structure of the general form:
struct stat {
dev_t st_dev; / device inode resides on /
ino_t st_ino; / this inode’s number /
mode_t st_mode; / protection /
nlink_t st_nlink; / number or hard links to the file /
uid_t st_uid; / user-id of owner /
gid_t st_gid; / group-id of owner /
dev_t st_rdev; / dev type, for inode that is dev /
off_t st_size; / total size of file /
time_t st_atime; / file last access time /
time_t st_mtime; / file last modify time /
time_t st_ctime; / file last status change time /
uint_t st_blksize; / opt blocksize for file sys i/o ops /
int st_blocks; / actual number of blocks allocated */
……….. etc………..
};
—|—
see the man pages for more detail. This data structure may vary somewhat from
platform to platform (see the stat.h header and its #includes), but the entry
names shown above are common to all Unix/Linux type platforms
The getdirentries() call requires that you use the open() system call to open
a directory, and you can then use getdirentries() to extract filenames from
the directory. (You may want to check out the library routines opendir() and
readdir(), which will do this for you in a more friendly way.) Your program
will have to work in two basic modes:
- if called with no arguments (as with ls) it must find the names of all the files in the current directory (including dotted files) and print information in the format shown above for each file object.
- if called with a series of file names (from the command line as with ls abc xyz etc) it must print information in the format shown above for each named object in the argv [ ] vector (wildcard characters are not allowed).
File types include ordinary (-), directory (d), symbolic link (l), character
device (c), and block device (b). You must show sample output with each of
these types. (You do not have to worry about pipe (p) and UNIX domain socket
(s) types, nor do you have to print resolution names for symbolic link (l)
types.) There are several additional library routines and header file macros
and defined constants that can help you get this done.
Several of these are discussed in class and they include:
library routines such as:
getpwuid()
getgrgid()
macros from header files such as:
S_ISDIR()
S_ISREG()
major()
minor()
etc.
defines from headers such as:
S_IRUSR
S_IWUSR
S_IXUSR
etc.
headers for this project (for our Linux systems such as mercury):
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h> // file type/prot macros
#include <sys/sysmacros.h> // major/minor macros
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
If you compile your code on mercury, you will need to define a compile time
symbol to work properly with NFS mounted file objects:
Bash-$ gcc -D_FILE_OFFSET_BITS=64 -g -o stat stat.c
You won’t need this if you compile on cs but it will not hurt to include it
either way (cs is a 64 bit Linux, while mercury is a 32 bit Linux)