Friday, 28 December 2012

sox, rec, and play stopped working with ALSA?

I upgraded my openSUSE installation from 12.1 to 12.2 recently and to my chagrin, my audio recording script stopped working. I use references to ALSA devices such as hw:1. The error message was cryptic:

rec FAIL formats: can't open output file `hw:1': can not open audio device: Invalid argument

After a lot of searching, I found the answer. The base cause is that in libsox2, which openSUSE 12.2 uses instead of libsox1, has separated out the Linux specific ALSA support into a separate shared object which is not loaded unless you set the environment variable AUDIODRIVER=alsa. So this is what I had to do in my script:

export AUDIODRIVER=alsa
export AUDIODEV=hw:1
rec ... program.wav

You are supposed to be able to use -t alsa in the options of rec but I had no luck with that.

Similarly for the play command which is just a link to sox like rec.

If you are just using the default sound device, you may not hit this bug as it may fall back to pulseaudio support. It's only when you must target a specific ALSA sound card that this bug manifests itself.

Here is the bug report that was the crucial clue.

Thursday, 6 December 2012

Blacklist a command in bash

I have an alias called nf. Sometimes due to fat fingers I end up typing mf and this starts up Metafont, which is installed because I use TeX, then I would have to exit it. I got tired of this and added this alias to $HOME/.alias:

alias mf='echo "Use \\mf if you really want metafont"'

If I really want to run mf, which is rarely, I can type \mf at the command line as the \ stops alias expansion. Invocations from shell scripts and Makefiles are not affected as $HOME/.alias is only read in by interactive shells.

BTW, please do not use this technique to block shell users from executing certain commands by aliasing them to something else. It's trivial to bypass in just the way I've shown.

Wednesday, 28 November 2012

cd to directory containing a file

Many times I have run a program on a file and then immediately, wanted to change to the directory containing it, to do more operations. For example:
vim path/to/some/dir/file
then I next want to do:
cd path/to/some/dir
I used to do
cd !$
and then use the up arrow key to get the previous failed command to edit out the trailing filename and re-execute. Then I decided I should write a shell alias or function to do this. It has to be an alias or a function and not a script as cd needs to work in the current shell.

This is what I came up with:
cdf() { cd ${1%/[^/]*}; }
This uses the remove matching suffix operation of parameter substitution, see the bash manual page for details. So now I can do:
cdf !$
and I will end up in the directory containing the file I just worked on.

I picked cdf as an abbreviation for change to directory of file, but you may prefer some other name.

Friday, 23 November 2012

VirtualBox host/guest conflct over audio device

I started a VirtualBox instance on my workstation that contains a CentOS instance to develop software. A bit later on I found this error from a periodic cron job I run on the workstation to record a radio program using the sound card:

rec FAIL formats: can't open input `hw:0': snd_pcm_open error: Device or resource busy
That error message comes from the rec program, part of the sox package. The first time it happened I thought the audio hardware had locked up so I rebooted it and the error went away. The next morning it happened again. I realised that the reason it went away the first time was because the CentOS guest had been shutdown by the reboot.

I disabled the audio device in the guest OS and had no more failures recording from the sound card. In general if you do not need audio in the guest, disable the device so that the guest does not interfere with the host OS's use of it.

You can also disable by editing the XML config fie, but only when the guest OS is not running. This is the relevant line:
<AudioAdapter controller="AC97" driver="ALSA" enabled="false"/>

Thursday, 1 November 2012

Filezilla, Domain OS (Apollo), and Cygwin

What do these three things have in common? Well, there's a story behind it.

At a site where I work there are a couple of ancient Apollo workstations running Domain OS, a Unix-like OS. This OS had an early networked filesystem where the super-root is called // and hosts have their filesystems underneath this. E.g. if you have two workstations named ws1 and ws2, their roots will be at //ws1 and //ws2.

Users needed to connect to the workstation filesystem using FTP. I proposed Filezilla as a replacement for an older, less friendly client. A side note,  you need to use active mode FTP when connecting to this old FTP server. We could login fine and get a directory listing, but when we tried to enter a directory by clicking on the folder icon, we would get an error like: /ws1 not found.  Looking at the command stream it was obvious what was going wrong. Filezilla was issuing CWD /ws1 when it should be CWD //ws1.

How to get Filezilla to either 1. use relative paths for CWD, or 2. understand that the root is //, not /? There was no option to use relative paths, it seems that Filezilla always converts paths to absolute ones for CWDBy trial and error I discovered that Cygwin has by coincidence the same pathname convention, // is the super-root. So by setting the Server Type to Cygwin in the Site Manager entry for this Apollo workstation, Filezilla connections worked.

So that's the connection. Hope this tip helps you if you happen to have to connect to a Domain OS FTP server with Filezilla.

Monday, 17 September 2012

SFTP is not FTP

Today I encountered another confused person who thought that to provide SFTP service, he had to install an FTP server.

I'm writing this post so that I can point people to it next time I encounter this misconception.

Yes, SFTP stands for Secure File Transfer Program and FTP stands for File Transfer Protocol but there the similarity ends. SFTP is run over a ssh connection, which normally uses the single service port 22. FTP is a different protocol using two ports, normally 20 and 21 for data and command (I will not go into the complexity of active and passive modes here). They are not related. The Wikipedia entry for FTP explains it succinctly. SFTP servers are different from FTP servers. Although there are clients that are capable of connecting to both types of servers, for example, Filezilla.

If you can ssh to a server, you can probably sftp also. I qualified that claim with "probably" because the sftp functionality has to be enabled and allowed to users.

SFTP is much much preferred over FTP due to encryption of the stream.

To complicate things there is a variant of FTP called FTPS which uses TLS to encrypt the stream.

Thursday, 6 September 2012

What good is the --target option of cp?


If you look at the man page for cp(1) on operating systems where the GNU tools are used, such as Linux, you will see there is an third form that uses the -t option or alternatively the equivalent long form --target.
cp [OPTION]... -t DIRECTORY SOURCE...
So what good is this when you already can do:
cp [OPTION] SOURCE... DIRECTORY
Here's a reason. Suppose the SOURCE list is large and comes from a file or another command. So you have to use the xargs command to invoke cp as many times as necessary to consume the list, without running into command line argument limits. Assuming the source list is one per line, you could do something like this:
xargs -d '\n' cp -pr -t destdir < listofsources
The -t allows the destination directory to be put before the source arguments in the cp command. Without it,  you would have to resort to the interpolation feature of xargs, i.e. -I {}