Lecture 25 Slides - File Systems and Paths

1 of 18

Lecture 25

Files and Folders (OS Level Imperatives)

2 of 18

File system operations

  • We’ll be using some handy-dandy functions in the file-operations package

(require "file-operations.rkt")

3 of 18

Path namesand tree-structured file systems

4 of 18

  • Modern operating systems allow files to be placed in directories (folders)
    • And directories to be placed in other directories
    • And so on and so on
  • Creating a branching structure – in other words… a tree!
  • The start of the tree is called the root directory

Tree-structured File Systems

root

a

b

d

e

f

g

c

5 of 18

File paths

root

a

b

d

e

f

g

c

  • Naming a file involves telling the system how to get to it
    • Starting with the root
    • List all the directories you need to take
    • To get to the file
  • This is called a path for the file

root -> b -> f

6 of 18

Specifying files using pathnames

  • A pathname is a string describing exactly where a file or directory is in a computer’s file system
  • It lists all the directories in the path, separated by /’s
  • On Windows
    • Elements are separated with \’s
    • And also has a drive letter

/Users/connor/Desktop

The pathname of my Desktop on my Mac

C:\Users\connor\Desktop

The pathname the same folder on my PC

C:\Windows\sys32\virus.exe

A file called “virus.exe” inside the sys32 folder inside the Windows folder.

D:\foo\bar\baz.txt

A text file on the D drive of my PC

7 of 18

Files, directories, and their paths

/

/a

/ b

/a/d

/a/e

/b/f

/b/g

/c

root

a

b

d

e

f

g

c

8 of 18

Delimiters in pathnames

  • The first hierarchical file system was MULTICS, which used the > character to separate directory names, as in:

>foo>bar>baz.txt

  • AT&T did a simplified version of MULTICS called Unix. It used slashes instead of >:

/foo/bar/baz.txt

  • DOS used Unix-style pathnames (with drive letters for backward compatibility with an earlier operating system called CP/M):

C:/foo/bar/baz.txt

9 of 18

Windows pathname delimiters

  • Then AT&T sued the (then) small company Microsoft for infringing their patent on using / as a directory separator
  • Microsoft changed the / to a \
    • C:\foo\bar\baz.txt
  • Unfortunately, \ means something special in almost every programming language, in a string and so you have to type it as two \’s:
    • “C:\\foo\\bar\\baz.txt”
  • This is a pain, so windows also lets you use /

10 of 18

Mac pathname delimiters

  • When Apple introduced hierarchical file systems, they used colons because they didn’t want to get sued like Microsoft had
    • Mac HD:My stuff:Diary:November
  • But when they converted to OSX (what macOS used to be called), they converted to slashes, since ATT’s patent had run out
    • /Applications/Racket/DrRacket

11 of 18

Linux pathname delimiters

  • Linux always was Unix, so it always used slashes
    • /usr/local/bin/csh

12 of 18

Path objects in Racket

  • When typing pathnames in code, you type them as strings
  • But Racket represents them internally as Path objects
  • Paths print as #<path:whatever>

/Users/connor/Desktop

#<path:/Users/ian/Desktop>

C:\Users\connor\Desktop

#<path:C:\Users\connor\Desktop>

C:\Windows\sys32\virus.exe

#<path:C:\Windows\sys32\virus.exe>

D:\foo\bar\baz.txt

#<path:D:\foo\bar\baz>

13 of 18

Creating a Path in DrRacket

; build-path : string ... -> Path

; builds a path from a series of strings

> (build-path "test" "test_2" "bar.txt")

/test/test_2/bar.txt

> (build-path "test" "test_3")

/test/test_3/

  • To build a path, we can use the build-path function!

14 of 18

Getting JUST the filename from a Path

(path-filename path)

Returns just the filename (no directories etc.) from a path

15 of 18

Converting Paths to/from Strings

(define p (build-path "test" "test_3"))

> (path->string p)

"test/test_3"

> (string->path "test/test_2/bar.txt")

#<path:test/test_2/bar.txt>

  • Sometimes it’s useful to treat a Path as a String
    • (or convert a string to a Path).
  • path->string takes in a Path and returns the corresponding string
  • string->path takes in a string and returns the corresponding path

16 of 18

Walking the file system

17 of 18

Finding all the files in a subtree

  • directory-files gives us all the files inside a given Path to a directory
    • It will return a (listof Path)
    • (directory-files (build-path “.”))
  • directory-subdirectories gives us all the subdirectories inside a given Path to a directory
    • It will also return a (listof Path)
    • (directory-subdirectories (build-path “.”))
  • How do we find all the files in the directory and its subdirectories?
  • Use recursion!

18 of 18

Finding all files in a subtree

(define (all-files directory)

(append (directory-files directory)

(apply append

(map all-files

(directory-subdirectories

directory)))))