class: title # Files and Directories ## CSC 213 – September 25, 2026 --- # Agenda for Today 1. Department Events 2. Upcoming Work 3. Files and Directories 5. Wrap Up --- # Department Events ## CS Picnic (RSVP Required) .indent[ Today at 5pm, Merrill Park West Shelter ] ## Flu and COVID Shot Clinic .indent[ October 1st and 2nd from 9am–4pm in the Harris Center Register with Hy-Vee at
] ## CS Table: Free and Open Source Software .indent[ Tuesday, September 29th at noon in JRC 224C ] *To receive a token, send an email to let me know you attended no more than 48 hours after the event.* --- class: section, blue # Upcoming Work --- # Lab: Malloc **Due:** Wednesday, October 7th by 11:59pm **Late Deadline:** Monday, October 12th by 11:59pm ## Questions _No new questions since last class_ --- class: section, blue # Files and Directories --- # Discussion: Filesystem Basics **Questions for think, pair, share:** 1. What is a file? 2. What is a directory? 3. What could we find on a computer's filesystem other than files and directories? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- # Discussion: Filesystem Permissions **Questions for think, pair, share:** 1. A file has permissions `rwxrw-r--`. What can different users do with this file? 2. A directory has permissions `rwxr-xr--`. What can different users do with this directory? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- class: section, green # Puzzles --- # Puzzle Rules ## Format Each puzzle shows a program fragment. The program finishes executing and exits without any errors. ## Setup The first puzzle program runs in an empty directory. Each following puzzle runs in the same directory, which has all the outputs from the previous puzzles. ## Responses Your task is to determine (without running the code) what files will be in the directory after each puzzle program runs, and what those files will contain. --- # Puzzle 1 ```c int fd = open("message.txt", O_RDWR | O_CREAT, 0640); char* message = "Hello"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 2 ```c int fd = open("message.txt", O_RDWR | O_CREAT | O_APPEND, 0640); char* message = " world!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 3 ```c int fd = open("message.txt", O_RDWR); char* message = "J"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 4 ```c link("message.txt", "greeting.txt"); int fd = open("greeting.txt", O_RDWR | O_APPEND); char* message = "Greetings.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 5 ```c symlink("message.txt", "symlink.txt"); int fd = open("symlink.txt", O_RDWR | O_APPEND); char* message = "ABC\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 6 ```c int fd = open("message.txt", O_RDWR | O_TRUNC); unlink("message.txt"); char* message = "DEF\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 7 ```c int fd = open("symlink.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "I love the malloc lab\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 8 ```c mkdir("somedir", 0755); int fd = open("somedir/message.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Hello world!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 9 ```c chdir("somedir"); int fd = open("greeting.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Greetings.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 10 ```c int fd = open("message.txt", O_RDWR, 0640); char* data = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); data[2] = 'f'; data[3] = 'e'; data[4] = 'a'; data[5] = 'r'; munmap(data, 4096); close(fd); ``` --- # Puzzle 11 ```c int fd = open("fork.txt", O_RDWR | O_CREAT, 0640); pid_t child_id = fork(); if (child_id == 0) { char* message = "fork\n"; write(fd, message, strlen(message)); } else { wait(NULL); char* message = "spoon\n"; write(fd, message, strlen(message)); } close(fd); ``` --- # Puzzle 12 ```c int fd = open("dup.txt", O_RDWR | O_CREAT, 0640); dup2(fd, STDOUT_FILENO); char* message = "Hello from write().\n"; write(fd, message, strlen(message)); printf("Hello from printf().\n"); message = "Interesting...\n"; write(fd, message, strlen(message)); close(fd); ``` --- class: section, blue # Wrap Up --- # Reminders ## Upcoming Deadlines - **Malloc Lab:** due two Wednesdays from now ## Need help? Use your resources! - Book an appointment during office hours - Attend mentor office hours - Review slides, readings, and other course resources - Use tools like `gdb` and AddressSanitizer to catch and diagnose issues ## Reading - **File System Implementation** (OSTEP, Chapter 40)