CS 2500 Lab 7: List Recursion for Files and Directories

Remember:

A Simple Filesystem

As computer users we can create files and directories (sometimes called folders) on our computers to store our data and our programs. It is useful to have operations that allow manipulating these files and directories. Consider the following data definition of a flat FileSystem (FS), i.e., no directories, just files.

;; A File is a String

;; An FS (FileSystem) is one of
;; -- empty
;; -- (cons File FS)

Exercise 1:

Design a function called total that consumes a FileSystem fs and produces the total number of files in fs.

Exercise 2:

Design a function that consumes a FileSystem and a File and returns true if the file is present in the FileSystem and false otherwise.

Adding Directories to our Filesystem

Flat FileSystems quickly become chaotic. We would instead like to have a hierarchical way to structure our files on our machines, so we extend our previous data definition to allow directories as well.

;; A File is a String

;; A DirEntry is one of:
;; -- File 
;; -- Dir 

;; A Dir is one of
;; -- empty
;; -- (cons DirEntry Dir)
;;
;; Interpretation: A directory is constructed by adding directory entries, which
;; may be files or directories, to the empty directory.
;;  - (cons f d) is the directory containing the file f and all the 
;;    entries of directory d
;;  - (cons d1 d2) creates the directory containing the subdirectory d1 and all
;;    the entries of directory d2

Exercise 3:

Construct a Dir that represents the following directory tree. Note that our simple data definitions don't allow names for directories.
- hw3.ss
- hw1.ss
+ - lists-and-recursion.ss
  - structs.ss
  - tetris.ss
- syllabus.pdf
+ - olin.png
  - david.png
  - r6rs.pdf
  - kitteh.png
- hw2.ss

Exercise 4:

Design a function that consumes a Dir d and produces the total number of files in the whole directory tree of d.

Exercise 5:

Design a function that consumes a Dir d and a File f and returns true if f exists in d or any of its subdirectories and false otherwise.

Exercise 6:

Design a function that consumes a Dir d and two Files src and target, and produces a Dir which has the same files as d but with all files named src renamed to target.

Adding Attributes to Files and Directories

Directories gave us more structure but the information that we have for each file and directory is still minimal. Let's add attributes to files and directories so we can track who owns a file and what its size is on disk:

(define-struct file (name size owner))
;; A File is (make-file String Number String)
;; interp. (make-file s n o) creates a File with name s, size n kilobytes,
;; and owned by o.

Let's also add names to directories and split their contents into two separate pieces, a list of subdirectories and a list of files:

;; An LoF is one of
;; -- empty
;; -- (cons File LoF)

(define-struct dir (name dirs files))
;; A Dir is a (make-dir String LoD LoF)
;; interp. (make-dir s ds fs) creates a Dir with name s containing
;; ds as its list of subdirectories and fs as its list of files.

;; An LoD is one of
;; -- empty
;; -- (cons Dir LoD)

Exercise 7:

Design a function that consumes a Dir d and produces the total number of files (just files, not directories) in the directory tree of d.

Exercise 8:

Design a function that consumes a Dir d and a string s representing an owner and returns a list of files owned by s in the directory tree of d.

Exercise 9:

Design a function that consumes a Dir d and a Number n representing file size and returns a list of files in the directory tree of d with size at least n.

Exercise 10:

Design a function that consumes a Dir d and computes the total size of all files in the whole directory tree. You may assume that directories take up no space.

If you finish early, try designing a function that draws the contents of a directory tree as an image…

Enjoy!