Cannot delete File / unmount disk because it is in use …

On OS X there are these moments when Finder tells you that the trash cannot be emptied or that a disk can not be unmounted because some files in/on them are still being used. When emptying the trash, Finder even tells you about the files in question but not about the app that is accessing them.

There are two ways to find out:

1. opensnoop

With opensnoop you can display what files are currently being accessed (as in live) including the process id and the name of the application. Either you can display all the files or just the one you are interested in.

For example I have an image on my desktop. I can attach to that file and when I open it via double click in Finder I get the following output:

sudo opensnoop -f /Users/hukl/Desktop/IMG_0434.JPG 
Password:
  UID    PID COMM          FD PATH                 
  501  10244 Finder         9 /Users/hukl/Desktop/IMG_0434.JPG 
  501     32 mds           15 /Users/hukl/Desktop/IMG_0434.JPG 
  501  10278 Preview        6 /Users/hukl/Desktop/IMG_0434.JPG 
  501  10278 Preview        6 /Users/hukl/Desktop/IMG_0434.JPG 
  501  10278 Preview        7 /Users/hukl/Desktop/IMG_0434.JPG 
  501  10278 Preview        8 /Users/hukl/Desktop/IMG_0434.JPG 
  501     32 mds           15 /Users/hukl/Desktop/IMG_0434.JPG 
  501  10278 Preview        6 /Users/hukl/Desktop/IMG_0434.JPG

This only helps though if the file is being actively accessed. More often though an application only holds a reference to the file, preventing Finder to delete it. In this case opensnoop is no good but luckily there is another way:

2. lsof

lsof basically lists information about all files opened by applications. Therefore if I want to know why I can’t delete this image I just opened I can run:

lsof | grep /Users/hukl/Desktop/IMG_0434.JPG 
Preview   10278 hukl    8r     REG               14,5    1584476 483868 /Users/hukl/Desktop/IMG_0434.JPG

Now that I know that Preview.app is still accessing the File I can kill the process and delete the file.

Many times its Finder itself still holding references to the files even if all the applications are closed and there is no apparent reason for not deleting the file. In this case option-click on the Finder icon in the dock and relaunch Finder (you can also kill it in Terminal of course). The files should be deletable and the disks should be unmountable.

Using the Intel 510 Series SSD in a 2011 MacBook Pro at full speed and with TRIM

I just got a new MacBook Pro from my current employer and since I got it without an SSD I bought the Intel 510 250GB and installed it. Everything worked smoothly after the first boot. However, as @denis2342 pointed out, there are a few extra steps to make it run at full speed and performance.

First of all, although this MacBook Pro has a SATA-III interface with up to 6 Gigabit, the System Profiler only showed a »Negotiated Link Speed« of 3 Gigabit. In order to make it negotiate to 6 Gigabit a SMC reset has to be performed. Basically you have to press the (left side) Shift-Control-Option keys and the power button at the same time and after that you have to boot normally.

After that System Profiler showed a »Negotiated Link Speed« of 6 Gigabit.

Then, although OS X enables TRIM support for Apples own SSD drives on the latest MacBook Pros, it doesn’t enable it for 3rd party SSDs. There were workarounds which involved patching a CoreFramework it was kind of messy and not something you’d recommend to any beginner. Luckily there is now a tool called »TRIM Enabler« which allows to backup and restore the Core Framework library and also to patch it with the click of a button. This also worked as expected and after another reboot the System Profiler showed that TRIM was enabled for my 3rd party SSD.

After I ran an Update the TRIM support was disabled again and I had to run TRIM Enabler once more.

I really hope that Apple is enabling TRIM for all SSDs with Lion to make this step unnecessary.

That is about it. This SSD is really blazing fast. If you’re interested, there is a nice in-depth review at anandtech.com

While the SSDs from other vendors are still faster, the Intel SSDs offer a higher reliability.

Mac OS X Keyboard Shortcut for locking the screen

For years I’ve been searching for a keyboard shortcut that would instantly lock my screen / desktop. I’ve seen this on Linux window managers and something like this probably exists on every major operating system.

My current workaround was to open Keychain.app, enable the menu bar icon in the preferences and click each time on the menu bar item -> lock screen.

I tried AppleScript, I tried Automator, I even convinced a friend that it would be necessary to write a small app for this. Recently however, somebody said that there is such a shortcut and that it exists since Mac OS 8.

This holy shortcut is:

⌃ + ⇧ + ⏏ (Control + Shift + Eject)

On Macs without an Eject key you can use the power button in the shortcut instead.

You have to enable »Require Password immediately after sleep or screen saver begins« in the System Preferences -> Security to make this truly lock your screen.

There are probably many other shortcuts in Mac OS I don’t know. If you have any hints where to get a complete list, please let me know!

High Sierra Update

With macOS 10.13 there is now a dedicated screen lock feature which can be invoked with this new shortcut:

⌃ + ⌘ + Q (Control + Command + Q)

Other Updates

  • In Mac OS X terminology this shortcut puts the display immediately to sleep.
  • A website listing this and other shortcuts can be found here

Files with mixed and invalid Encodings in Ruby

Recently I encountered a file which mostly contained UTF-8 characters. I could read the file and even throw it at Nokogiri and there was no problem.

When I wanted to preprocess the content of the file with gsub ruby raised this Exception:

invalid byte sequence in UTF-8 (ArgumentError)

Now what to do?

There aren’t many attractive options. I googled around for quite some time but nobody seemed to have a reasonable solution so I tried wrapping a begin…rescue around each element I extracted with nokogiri and attempted to do the gsub clean up work there and if that would fail I would either try to guess the right encoding (ISO Latin or ASCII) or skip that element entirely. However even if that would have silenced the exceptions it would most likely be incorrect in the end.

My favorite option would be to simply ignore the bad sequences all together but I couldn’t find a proper way to do it in Ruby. After some time of googling I found this blog post describing how this can be achieved with Iconv

In case the blog post disappears I will write down the executive summary here:

ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
valid_string = ic.iconv(untrusted_string)

I guess this is useful outside the ruby universe as well. I hate encoding issues – I really do. Somehow though you end up with them in _every_ project.

How to setup SparkleShare beta2 for mac with your own server

Today the first SparkleShare beta for mac came out. SparkleShare is somehow very similar to dropbox but instead of being bound to one company, SparkleShare is based on git and therefor allows hosting your repositories on your own servers. I’ve been waiting for this release for because one of my colleagues is already using it on linux for quite a while.

Unfortunately the documentation is a little thin about self hosted repositories so here are the necessary steps to get it working:

  1. Download and install SparkleShare for Mac
  2. Create a bare git repository on your server and make sure you can connect to it using public key authentication
  3. Start up SparkleShare and add a new folder. In the address field add the address of your server to which you can log in to via public key authentication. In the folder field add the absolute path to the repository on the server.

Initially I tried to set it up via the built in setup assistant but for some reason an error occurred. I was able to log in and clone via the command line though. It turned out that I misinterpreted the address field because I was thinking that the usual ssh://user@server url would go in there.

You can also add a remote folder via the command line although that doesn’t seem to be recommended as mentioned in the comments.

  1. Open up a Terminal and cd to ~/SparkleShare
  2. Then clone your remote repository into that folder: git clone user@server/path/to/repo.git
  3. The folder should now appear in the SparkleShare menu and work as expected

Also make sure to have a working /usr/bin/git executable or symlink an existing git binary to that location otherwise SparkleShare will crash right away.