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:
FILENAMEFILE_TYPEPERMISSIONSOWNER_NAMEGROUP_NAMEDATE_OF_LAST_MODIFICATIONLINK_COUNTSIZE_IN_BYTES OR DEV INFOINODE_NUMBER
Example:
FILENAME:alphaFILE_TYPE:ordinaryPERMISSIONS:rw- r-- r--OWNER_NAME:jedwardsGROUP_NAM:gradDATE_OF_LAST_MODIFICATION:Mar 30 08:11 2003LINK_COUNT:2SIZE_IN_BYTES:1345 (or 12, 6 dev info)INODE_NUMBER:347*******< a blank line between entries >*******
System calls needed on a UNIX system include:
1 |
|
which reads up to nbytes of data into buf in the form:
1 | unsigned long d_ino; |
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.
1 |
|
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.
1 |
|
which fills in a data structure of the general form:
1 | struct stat { |
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_IRUSRS_IWUSRS_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 wont 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)