Archive for April, 2009

Are macs more insecure than Windows / GNU/Linux?

While I am not a big Mac OS fan, I think unbiased analysis of its advantages and downsides is fundamental, to build a personal opinion. This article is a little biased against Mac OS, but I think it got the main point right:
I have developed this analogy of someone who wants to buy a nuclear reactor. There are two organizations interested in providing you with their nuclear reactor.
  • Provider # 1 gives you the reactor plus all the design information, all blue prints, everything but the kitchen sink!
  • Provider # 2 gives you the reactor and doesn’t give you a clue as to how it is built inside. It’s a black box (or a massive gray one). All you have is the control panels and the documentation that this provider is kind enough to provide with (you know…. they can’t give you everything for security reasons).
Given those two choices… which would you consider to be more secure/stable/reliable? Which one would you choose? I’d personally go for Provider 1. At least I know what I’m getting. And the guys are so comfortable with their design that they even give it away to buyers. Perhaps the guys at Chernobyl chose provider # 2.

Great analogy, but the same is true for Windows :D

[via Technology FLOSS]
  • Share/Bookmark

Automatically throttling rtorrent

I’ve been wondering how to get rtorrent to stop downloading while I’m working and go full throttle when I’m not. Even more so because I share my internet connection with 30 other people, and I can tell you, they get mad pretty fast :)

It would be nice to have a cronjob(Cron) that slows down the network load at, let’s say, 8 o’Clock in the morning and put it back full speed after 1 AM. Luckily since I already have rtorrent running in a screen instance to detach it from the console I can use the -X parameter which passes keystrokes on to the running program.

If I started rtorrent with the following command $ screen -dmS rtorrent rtorrent which creates a new, detached, screen with the name rtorrent and executes rtorrent, then I’d add the following to my crontab:

0 1 * * * screen -r rtorrent -X stuff CCCCC
0 1 * * * screen -r rtorrent -X stuff ccccc
0 7 * * * screen -r rtorrent -X stuff S
0 7 * * * screen -r rtorrent -X stuff s

What this does is, at 1 AM, remove up to 250KB/s limit (never put in more or the limit won’t be lifted) and at 7 AM throttle all uploads and downloads to 5KB/s.

Alternatively I could kill the rtorrent instance and restart it, but by doing it this way I can still use the web interface and I don’t have to preform hash checks as often (your disk will thank you).

Any better idea? Feel free to comment:D

  • Share/Bookmark

Easily finding duplicate files

Some days ago I was asked by my mother if there was an easy way to find duplicate photos on her computer. I thought about it and I came up with the idea that the easiest way to do this is to just compare if some hash matches between the files (which works fine as long the images are not modified). Then came the implementation and I thought since I know PHP best for this job, why not use it. Now I know that PHP hasn’t much of a reputation as a command line scripting language, but bear with me.
The first step is to enumerate all the files we want to compare, for this we’ll need two parameters:
  • The path from which to recursively get all files
  • The pattern (in our case a Perl regular expression) that filters over all files
So here goes the first part of the script:
if(count($argv) < 2)
	die("Usage:\n  ".$argv[0]." path [regexp]\n");
$path = $argv[1];
$pattern = count($argv) < 3 ? "/.*/" : $argv[2];
The next step is to actually enumerate all files that are to be compared:
$files = preg_ls($path, true, $pattern);
 
function preg_ls ($path=".", $rec=false, $pat="/.*/") {
    $pat=preg_replace ("|(/.*/[^S]*)|s", "\1S", $pat);
    while (substr ($path,-1,1) =="/") $path=substr ($path,0,-1);
    if (!is_dir ($path) ) $path=dirname ($path);
    if ($rec!==true) $rec=false;
    $d=dir ($path);
    $ret=Array ();
    while (false!== ($e=$d->read () ) ) {
        if ( ($e==".") || ($e=="..") ) continue;
        if ($rec && is_dir ($path."/".$e) ) {
            $ret=array_merge ($ret,preg_ls($path."/".$e,$rec,$pat));
            continue;
        }
        if (!preg_match ($pat,$e) ) continue;
        $ret[]=$path."/".$e;
    }
    return (empty ($ret) && preg_match ($pat,basename($path))) ? Array ($path."/") : $ret;
}
With the preg_ls function borrowed from php.net. Next we calculate and collect the hashes, and at the same time check for collisions:
$hashes = array();
$duplicates = array();
 
foreach($files as $file){
	$hash = sha1_file($file);
	if(array_key_exists($hash, $hashes))
		$duplicates[] = array($hashes[$hash], $file);
	else
		$hashes[$hash] = $file;
}
What this does is simply calculate the SHA1-hash for each file and checks wether we encountered it some time before. If we do know the hash it is a duplicate and should be memorized for later, if not it’s a new file so add it to our memory.
All that is missing now is the reporting functionality that reports duplicates we found back to the user.
print("Duplicates:\n");
foreach($duplicates as $duplicate){
	print("    ".$duplicate[1]." is a duplicate of ".$duplicate[0]."\n");
}
So there you go, a simple script that performs reasonably well, and has found most duplicate pictures in my mothers case :-)
If you just want the script then get it here, change the access rights:
$ mv ./duplicates.phps ./duplicates.php
$ chmod +x ./duplicates.phps 
And you’re ready to go:
$ ./duplicates.php ~
  • Share/Bookmark