Understanding how to use the filesystem will allow you to add common and expected features to your apps.
Listen to the full episode for more insight into filesystems. And make sure to subscribe to the podcast from within your podcast app to get new episodes automatically. You can also read the full transcript below.
How many times have you needed to save your work? Maybe you’re writing a document and need to save it so that you can continue working on it later. I don’t know anybody that can write more than a few pages perfectly the first time and then have no need of that document after printing it. Even emailing your work requires it to be saved somewhere.
And documents aren’t the only thing that needs to be saved. Maybe you’re playing a game and want to save your progress so you can continue playing later.
Now you could just leave everything as is in memory. Just pause the game or leave the document open. Then come back to it whenever you want.
That works until you lose power. Or until some bug causes the application to crash.
And then there’s the question of size. Modern games are much too big to fit everything into memory at once. Maybe a word processor can fit everything into memory including a full novel that you’re writing. But that’s because even the longest novel is insignificant in size compared to all the data needed by an immersive game world.
What about pictures and video? It wouldn’t be very good if you couldn’t do anything with a picture except look at it right after it was taken. We need to save things to a permanent storage location that we can rely on to exist after turning off a device or a computer. This has been possible for so many years that it’s just expected. Nobody really considers it optional anymore. It would be like buying a car with an optional air conditioner. A long time ago, cars didn’t have air conditioners. Or power steering. Or power windows that can be opened and closed with the press of a button. To this day, people still say to roll down the window.
There was a time when computers came with optional disk drives. Some early computers didn’t even have this as an option. You would turn on the computer, do whatever you wanted, and lose everything when you turned it off.
Some computers were designed to use a tape recorder to save your work. The same kind of tape recorder that people used to record and play voices and music. When I was a kid, I would wait patiently next to the stereo for a song to play that I liked and sit as quiet as possible while holding the tape recorder next to the speaker to record the song. The recording quality was terrible even when I could avoid dogs barking or birds chirping.
The computers would emit a series of tones or beeping sounds whenever you wanted to save your work. The only good thing was that the recording was done through a cable instead of telling everybody to be quiet so you could save your work. It was still a long process with quite a few steps. And it was slow.
I’m skipping over even earlier computers that used paper cards or rolls of paper tape to store information. They could punch holes in the paper and then read information back by looking for the holes. I just don’t have any direct experience with these computers.
Early main frame computers did have the ability to store information on magnetic disks or magnetic tape. Some disks were removable and some were fixed. The tape was stored in large cartridges that could be loaded into even larger tape players. Some removable disks were larger than birthday cakes and were the type of thing that you might ask for help carrying.
The removable disks became smaller and flexible and could fit inside a binder with regular papers. They were about 8 inches in diameter and kept inside a protective sleeve so that people could hold them without getting fingerprints on the actual magnetic surface.
These types of disks became smaller again until they were just over 5 inches in diameter and many personal computers started including disk drives that could read the floppy disks.
I remember early IBM computers that had room for one or two floppy disk drives. Having two drives made copying disks very easy. And early Apple computers could connect with external floppy disk drives. The drives were bulky and took up a lot of room either inside or outside of the computer.
There was one time that I learned how to repair Apple floppy disk drives. The disks would spin and a magnetic coil would move over the surface of the spinning disk to detect changes in the magnetic field in the disk. The coil could also be used to change the magnetic information on the disk when writing. Anyway, the speed that the disk turned was really important and needed to be adjusted. If the disk turned at the wrong speed, then it wouldn’t be able to find the spots on the disk where the magnetic fields contained actual information. To provide a starting point, the magnetic disks had a small hole near the center. The hole would let the floppy drive figure out where the information began on the disk. And the speed would let it figure out where information could be found farther away from the hole.
To adjust the speed, Apple came up with an interesting solution. There was a series of black and white stripes inside the disk drive that were spaced so that when the cover was off and the motor was turning and normal incandescent lighting was used to light the room, then the spinning stripes would appear to stand still. This was a strobe effect that used the slight flickering of the lights at 60 Hz or 60 times per second to adjust the speed. If the disk drive motor was spinning too fast or too slow, then the stripes would appear to move because they wouldn’t match the frequency of electricity in the United States.
As an interesting side effect, it was possible to purposefully change the speed of your floppy disks so that nobody else could read your files. Sort of a simple security system for paranoid people.
Modern disks still use spinning surfaces covered on magnetic material. Except for the solid state drives which are really just like a big USB drive inside a shell the same size as the mechanical spinning drives.
They still use a coil that hovers just above the magnetic surface and reads and writes information through small changes in north-south magnetic polarity. Hard drives have their disks sealed inside a hard shell away from dust that could scratch the magnetic surface. And hard drives have multiple spinning platters that each give a top and bottom surface to read and write from. Floppy drives don’t use hard platters and instead have a single flexible disk.
As computers became smaller, a new form of floppy disk became popular. This was not really flexible anymore and was enclosed in a hard plastic cartridge. It was smaller at just 3 1/2 inches in diameter. And it had a sliding cover that would open when the disk was inserted in the drive and close again when it was removed to help protect the magnetic surface. Many people call these floppy disks nowadays even though you can’t really bend them.
The purpose of all the disks, whether hard or floppy, mounted permanently inside the computer or removable is the same. They give you the ability to store large amounts of information that remains in place even when the computer loses power or the application crashes.
Optical disks such as CDs and DVDs use a different mechanism to store information but also serve the same purpose.
And modern computers also use solid state drives such as USB thumb drives which again use a different mechanism but serve the same purpose.
Anytime you can avoid motors and spinning disks with either magnetic or optical material, you’ll have a more reliable and faster system to store information. Some computers today avoid mechanical storage completely and use only solid state drives.
All of this that I’ve described so far has been just about the drive itself. But how do you organize the information? You can’t just write a bunch of north and south poles and expect to make sense of it later unless you organize everything into a system.
That’s what a filesystem is. It’s the organization of permanent storage so you can actually work with it.
Languages such as C++ have some basic support for filesystems. And C++ is about to get better with this. But most of the support comes from the operating system itself.
Other languages might have richer support for a filesystem but C++ can be used in places such as micro controllers where there’s not enough room for all the code needed to support a filesystem. There’s not really an operating system either in these cases. I’m going to focus on places where you do have a filesystem.
Whether you’re running Windows, Linux, or a Mac operating system, you’ll have support for a full filesystem. Something that can let you forget about the speed of rotating platters or how many read and write heads exist. Something that can manage removable storage media.
You’ll get other benefits too such as security so that a computer system with multiple users can protect files from one user to the next. Networking. The ability to extend the filesystem in interesting ways. And of course, the ability to save information to files so that your customers will have the benefits they expect.
This begins a series of episodes about filesystems. Each episode will focus on a specific aspect of filesystems so that you can understand how to use this in your code.