// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleRescue

A Story About a Bug

A directory creation bug caused by permissions on a network file system, and a lesson about invalid assumptions and execution environments.

Art+Logic Apr 29, 20132 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

17-year_cicada.jpg

I’m going to tell you a little story about a bug. Like most bugs, it started with an invalid assumption. You see, I was ambushed by permissions on a network file system. Here’s what my code looked like, at least in spirit:

import os
import os.path
## Build a path
filepath = 'relative/path/to/file.txt'
dirpath = os.path.dirname(filepath)
## Create the directories in the path
os.makedirs(dirpath)
print("Success")

Go head and try that. Let me know how it works for you. It works fine for me. Well, it usually works fine for me. It could fail if I don’t have permission to create a directory in the current directory. Let’s add a test:

import os
import os.path
## Build a path
filepath = 'relative/path/to/file.txt'
dirpath = os.path.dirname(filepath)
## Create the directories in the path
if os.access('.', os.W_OK):
    os.makedirs(dirpath)
    print("Success")
else:
    print("No write permission for current directory")

It seems to work:

troy$ chmod 555 .
troy$ python demonstration2.py
No write permission for current directory
troy$ ls -l
total 96
-rw-r--r--  1 troy  staff    194 Apr 27 11:20 demonstration1.py
-rw-r--r--  1 troy  staff    290 Apr 27 11:21 demonstration2.py
troy$ chmod 755 .
troy$ python demonstration2.py
Success
troy$ ls -l
total 96
-rw-r--r--  1 troy  staff    194 Apr 27 11:20 demonstration1.py
-rw-r--r--  1 troy  staff    290 Apr 27 11:21 demonstration2.py
drwxr-xr-x  3 troy  staff    102 Apr 27 11:22 relative

Stepping back from the task at hand, let me mention that software developers rarely have great control over the execution environment. Let’s look at what happened to me when a user ran my directory creation code:

troy$ python path/to/demonstration2.py
Traceback (most recent call last):
  File "path/to/demonstration2.py", line 10, in <module>
    os.makedirs(dirpath)
  File "path/to/lib/python2.7/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "path/to/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: 'relative/path'

That’s not what I had expected. I had the user run some commands:

user$ mkdir directory
user$ touch file
user$ mkdir directory/child
mkdir: directory/child: Permission denied
user$ ls -l
total 32
drwxr-xr-x  2 user  staff  16384 Apr 27 11:30 directory
-rw-r--r--@ 1 user  staff      0 Apr 27 11:30 file

The user was able to create directories and files, but not subdirectories. This seems peculiar. While I knew that the user intended to use my code on a network file system, I hadn’t considered that directory creation would fail, especially not after creating the first directory successfully.

Software screen capture

The source of this particular issue was in the configuration of the file server. The OS X file server can be configured allow creation of directories but not child files or directories. It can even be configured to create directories and files with specific permissions, ignoring the mode passed to calls like mkdir().

I was reminded of a few things from this:

keep going

More to explore in Rescue.

All Rescue resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…