Story for lecture 3: Directories, Users, File attributes ------------------------------------------------------------- 1. Intro: using ls to learn about directories and file attributes Last time we looked at the who command and learned about how to read and write files. We studied the system calls open, read, write, creat, close, lseek. These allow a programmer to operate on the CONTENTS of a file. Here's a picture. BUT, files are not islands. They are organized on the disk into directories, and they have attributes. Today we shall study directories and file attributes. To do so, we shall write a version of the unix utility called ls. 2. Using ls If we are going to write a version of ls, we should become familiar with what it does. $ ls $ ls /tmp $ ls / ls1.c samples.03 $ ls -l $ ls -l /tmp $ ls -l ls1.c $ ls -a $ ls -a / $ ls -lu Note: args can be nothing: defaults to current directory files: shows info about files directories: shows contents of directory 3. A brief review of the Unix directory tree Any hard disk is organized into a tree of directories, each of which contains files and/or directories. Such a structure is called a file system cd, pwd, ls allow you to explore the file system 4. Ok, so to write ls, we just need to display the files contained in a directory... Maybe it will be like who: open, read_struct, display_info, close 5. What is a Directory? Fuzzy Ans: a collection of files and/or dirs Real Ans: a special kind of file that contains a list of f a/o d Note: it also contains two special items, . and .. Again: it is just a file that contains a list of filenames 6. Can we use open, read, close to get the contents of a directory? Ans1: on old versions of Unix, that was the only way Ans2: on some versions of Unix, you still can, but not for all dirs demo: cat / ; cat /tmp ; cat /home od -c / ; od -c . Ans3: Some versions of Unix (e.g. Linux, never) Why?: Unix allows various disk formats to appear as part of a single tree. It supports Mac HFS, FAT, FAT32, lots of Unix flavors. So there IS NO SINGLE FORMAT, no utmp struct to read. 7. OK, OK, so how DO I read the contents of a directory? $ man -k direct $ man -k direct | grep read Answer: opendir, readdir, closedir Application: ls1, just like who, sort of 8. How close are we to a complete version of ls? Problems: a) no columns b) no sorting c) no -a d) no -l Solutions: a) boring, but doable b) use qsort, build a table c) not too hard d) hmmm 9. How do we add the -l option? Real Question: what does -l display? Answer: size, times, owner, group type, permissions Question: Where do we get file info? Search: $ man -k file $ man -k file | grep information | more Answer: stat() 10. How does stat() work? Diagram: the disk stores the contents of the file AND the info about a file. read() gets the contents stat() gets the info It copies the info from the disk into a buffer in user space example: statdemo.c 11. What info do we need? type, access: mode owner: owner group: group mtime: mtime size: size links: links Soln: we write stat1.c 12. How close are we? We have the info, but it is still in code. Our first version of who was like this. All we needed to do was to figure out how to decode the fields: size: ok links: ok mtime: ok (use ctime) owner/group: need to translate number to name type: ? perm: ? 13. Where is type and permissions encoded? Ans: in the mode value How: the 16-bit number stores type, rwxrwxrwx and three extras How does that work? Answer: A brief into to binary math The bitwise AND operator A brief intro to octal math The idea of a mask: grading multiple-choice tests Result: decoding the permission bits decoding the type - 14. How do we convert uid to name? Intro to /etc/passwd $ man -k uid $ man getpwuid The library function getpwuid() converts a uid to a struct with a name. 15. How do we convert gid to group name? Same idea. Meet /etc/group 16. stat2.c - the detail line from ls -l 17. How can we combine stat2.c with ls1.c ? soln: stat2 `ls1 | sort` But what about ls2 -l filename ?