<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>CodeBrainz.ca</title>
	<atom:link href="http://codebrainz.ca/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://codebrainz.ca</link>
	<description>10110101 10101011 10101010 11110101 2</description>
	<pubDate>Sun, 27 Jun 2010 21:34:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>Ubuntu Lucid Lynx (10.04) Fixup Script</title>
		<link>http://codebrainz.ca/index.php/2010/06/27/ubuntu-lucid-lynx-1004-fixup-script/</link>
		<comments>http://codebrainz.ca/index.php/2010/06/27/ubuntu-lucid-lynx-1004-fixup-script/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 21:34:25 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[GNU/Linux]]></category>

		<category><![CDATA[Shell Scripts]]></category>

		<category><![CDATA[10.04]]></category>

		<category><![CDATA[font size]]></category>

		<category><![CDATA[indicator-applet]]></category>

		<category><![CDATA[location bar]]></category>

		<category><![CDATA[lucid lynx]]></category>

		<category><![CDATA[nautilus]]></category>

		<category><![CDATA[titlebar buttons]]></category>

		<category><![CDATA[ubuntu]]></category>

		<category><![CDATA[ubuntuone]]></category>

		<category><![CDATA[volume icon]]></category>

		<category><![CDATA[wrong side]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=149</guid>
		<description><![CDATA[I wrote a super simple script to fix some issues I have with some defaults in the latest Ubuntu release.  Nothing majour here, just annoyances really.  Below is the script, read it and comment out any lines you do not want to take effect on your system.  It requires root access for a few [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a super simple script to fix some issues I have with some defaults in the latest Ubuntu release.  Nothing majour here, just annoyances really.  Below is the script, read it and comment out any lines you do not want to take effect on your system.  It requires root access for a few of the commands, such as managing packages and removing system-wide files.  Run it as a regular user and the sudo command will be used to prompt you for a password, run like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x ubuntu-fix.sh
$ .<span style="color: #000000; font-weight: bold;">/</span>ubuntu-fix.sh</pre></div></div>

<p>And the following is the source code or <a href='http://codebrainz.ca/wp-content/uploads/2010/06/ubuntu-fix.sh'>download the script here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Simple script to remove some of what I consider to be annoyances with</span>
<span style="color: #666666; font-style: italic;"># Ubuntu 10.04 Lucid Lynx (and probably future versions).</span>
<span style="color: #666666; font-style: italic;">#</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-e</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># keep location bar always visible instead of breadcrumbs</span>
gconftool-<span style="color: #000000;">2</span> <span style="color: #660033;">--set</span> <span style="color: #ff0000;">'/apps/nautilus/preferences/always_use_location_entry'</span> \
    <span style="color: #660033;">--type</span> bool <span style="color: #ff0000;">'true'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># move buttons to the correct side and add menu on left icon</span>
gconftool-<span style="color: #000000;">2</span> <span style="color: #660033;">--set</span> <span style="color: #ff0000;">'/apps/metacity/general/button_layout'</span> <span style="color: #660033;">--type</span> string \
    <span style="color: #ff0000;">'menu:minimize,maximize,close'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># make fonts a reasonable size</span>
gconftool-<span style="color: #000000;">2</span> <span style="color: #660033;">--set</span> <span style="color: #ff0000;">'/desktop/gnome/interface/font_name'</span> <span style="color: #660033;">--type</span> string <span style="color: #ff0000;">'Sans 9'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># remove the poorly named and useless (to me) &quot;ubuntuone&quot; client package</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dpkg</span> <span style="color: #660033;">-s</span> ubuntuone-client <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'not-installed'</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> remove <span style="color: #660033;">--yes</span> <span style="color: #660033;">--purge</span> ubuntuone-client
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>xdg<span style="color: #000000; font-weight: bold;">/</span>autostart<span style="color: #000000; font-weight: bold;">/</span>ubuntuone-launch.desktop 
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># remove the panel thing with email and other icons with wrong bg color</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dpkg</span> <span style="color: #660033;">-s</span> indicator-applet <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'not-installed'</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> remove <span style="color: #660033;">--yes</span> <span style="color: #660033;">--purge</span> indicator-applet
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>xdg<span style="color: #000000; font-weight: bold;">/</span>autostart<span style="color: #000000; font-weight: bold;">/</span>indicator-applet.desktop 
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># add the regular volume icon back at startup</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>xdg<span style="color: #000000; font-weight: bold;">/</span>autostart<span style="color: #000000; font-weight: bold;">/</span>gnome-volume-control-applet.desktop <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>xdg<span style="color: #000000; font-weight: bold;">/</span>autostart<span style="color: #000000; font-weight: bold;">/</span>gnome-volume-control-applet.desktop<span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
[Desktop Entry]
Name=Volume Control Applet
Comment=Show desktop volume control
Icon=multimedia-volume-control
Exec=gnome-volume-control-applet
Terminal=false
Type=Application
Categories=
NoDisplay=true
OnlyShowIn=XFCE;
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=gnome-media
X-GNOME-Bugzilla-Component=gnome-volume-control
# See http://bugzilla.gnome.org/show_bug.cgi?id=568320
#X-GNOME-Autostart-Phase=Panel
X-GNOME-Autostart-Notify=true
X-GNOME-Autostart-Delay=2
X-Ubuntu-Gettext-Domain=gnome-media-2.0
EOF</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># offer to logout</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Done fixing Ubuntu, you need to log out and log back in for some of <span style="color: #000099; font-weight: bold;">\
</span>the changes to take effect.  
&nbsp;
Log out now? [y/N] &quot;</span>
<span style="color: #c20cb9; font-weight: bold;">read</span> DOLOGOUT
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-z</span> <span style="color: #007800;">$DOLOGOUT</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$DOLOGOUT</span> == <span style="color: #ff0000;">&quot;yes&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$DOLOGOUT</span> == <span style="color: #ff0000;">&quot;y&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> \
        <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$DOLOGOUT</span> == <span style="color: #ff0000;">&quot;YES&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$DOLOGOUT</span> == <span style="color: #ff0000;">&quot;Y&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>gnome-session-save <span style="color: #660033;">--kill</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2010%2F06%2F27%2Fubuntu-lucid-lynx-1004-fixup-script%2F&amp;linkname=Ubuntu%20Lucid%20Lynx%20%2810.04%29%20Fixup%20Script"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2010/06/27/ubuntu-lucid-lynx-1004-fixup-script/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Low Level Disk IO in Managed .NET</title>
		<link>http://codebrainz.ca/index.php/2010/05/23/low-level-disk-io-in-managed-net/</link>
		<comments>http://codebrainz.ca/index.php/2010/05/23/low-level-disk-io-in-managed-net/#comments</comments>
		<pubDate>Sun, 23 May 2010 21:25:58 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[.NET and Mono]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[disk]]></category>

		<category><![CDATA[hard drive access]]></category>

		<category><![CDATA[low level]]></category>

		<category><![CDATA[managed]]></category>

		<category><![CDATA[stream]]></category>

		<category><![CDATA[win32 api]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=143</guid>
		<description><![CDATA[I&#8217;ve been messing around with disk I/O in C# lately, attempting to read/modify the boot sectors and such.  I&#8217;ve used the lower-level functions for accessing disks like files, from the Win32 API.  Most of the signatures were adapted from the pinvoke.net website as well as some examples from MSDN.
What I&#8217;ve done is made one file/class [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been messing around with disk I/O in C# lately, attempting to read/modify the boot sectors and such.  I&#8217;ve used the lower-level functions for accessing disks like files, from the Win32 API.  Most of the signatures were adapted from the <a href="http://www.pinvoke.net/" target="_blank">pinvoke.net</a> website as well as some examples from MSDN.</p>
<p>What I&#8217;ve done is made one file/class to hold all the unsafe/unmanaged code and another to wrap those functions up nicely in a .NET-style class named DiskStream.  The DiskStream class inherits from the Stream object and so exposes an interface similar to a FileStream.  You can open up a DiskStream and read, write, and seek it just like a regular file.</p>
<p>There are a few gotcha&#8217;s associated with performing disk access like this:</p>
<ul>
<li>You need to access the disk in multiples of the sector size (ie. 512 bytes).</li>
<li>I don&#8217;t believe this will work at all in any Windows below Win2K.</li>
<li>Currently has odd behaviour on disks with mounted volumes, if there&#8217;s a mounted volume, it appears only the MBR can be written.  I&#8217;m going to fix this in a future version so that the volumes get locked and/or dismounted before hand.</li>
</ul>
<p>So here&#8217;s the code for accessing the Win32 API from .NET.  I think this is pretty useful, since I had a hard time tracking down this stuff and making it work.  Here&#8217;s the first class, which I call DeviceIO:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.Win32.SafeHandles</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">namespace</span> DiskLib
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// </span>
    <span style="color: #008080; font-style: italic;">/// P/Invoke wrappers around Win32 functions and constants.</span>
    <span style="color: #008080; font-style: italic;">/// </span>
    <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">class</span> DeviceIO
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080;">#region Constants used in unmanaged functions</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> FILE_SHARE_READ <span style="color: #008000;">=</span> 0x00000001<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> FILE_SHARE_WRITE <span style="color: #008000;">=</span> 0x00000002<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> FILE_SHARE_DELETE <span style="color: #008000;">=</span> 0x00000004<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> OPEN_EXISTING <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> GENERIC_READ <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>0x80000000<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> GENERIC_WRITE <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>0x40000000<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> FILE_FLAG_NO_BUFFERING <span style="color: #008000;">=</span> 0x20000000<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> FILE_FLAG_WRITE_THROUGH <span style="color: #008000;">=</span> 0x80000000<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> FILE_READ_ATTRIBUTES <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>0x0080<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> FILE_WRITE_ATTRIBUTES <span style="color: #008000;">=</span> 0x0100<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">uint</span> ERROR_INSUFFICIENT_BUFFER <span style="color: #008000;">=</span> <span style="color: #FF0000;">122</span><span style="color: #008000;">;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Unamanged function declarations</span>
        <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">unsafe</span> <span style="color: #0600FF;">extern</span> SafeFileHandle CreateFile<span style="color: #000000;">&#40;</span>
            <span style="color: #FF0000;">string</span> FileName,
            <span style="color: #FF0000;">uint</span> DesiredAccess,
            <span style="color: #FF0000;">uint</span> ShareMode,
            IntPtr SecurityAttributes,
            <span style="color: #FF0000;">uint</span> CreationDisposition,
            <span style="color: #FF0000;">uint</span> FlagsAndAttributes,
            IntPtr hTemplateFile<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> CloseHandle<span style="color: #000000;">&#40;</span>SafeFileHandle hHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> DeviceIoControl<span style="color: #000000;">&#40;</span>
            SafeFileHandle hDevice,
            <span style="color: #FF0000;">uint</span> dwIoControlCode,
            IntPtr lpInBuffer,
            <span style="color: #FF0000;">uint</span> nInBufferSize,
            <span style="color: #000000;">&#91;</span><span style="color: #0600FF;">Out</span><span style="color: #000000;">&#93;</span> IntPtr lpOutBuffer,
            <span style="color: #FF0000;">uint</span> nOutBufferSize,
            <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">uint</span> lpBytesReturned,
            IntPtr lpOverlapped<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #0600FF;">unsafe</span> <span style="color: #FF0000;">bool</span> WriteFile<span style="color: #000000;">&#40;</span>
            SafeFileHandle hFile,
            <span style="color: #0600FF;">void</span><span style="color: #008000;">*</span> pBuffer,
            <span style="color: #FF0000;">int</span> NumberOfBytesToWrite,
            <span style="color: #FF0000;">int</span><span style="color: #008000;">*</span> pNumberOfBytesWritten,
            <span style="color: #FF0000;">int</span> Overlapped<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #0600FF;">unsafe</span> <span style="color: #FF0000;">bool</span> ReadFile<span style="color: #000000;">&#40;</span>
            SafeFileHandle hFile,
            <span style="color: #0600FF;">void</span><span style="color: #008000;">*</span> pBuffer,
            <span style="color: #FF0000;">int</span> NumberOfBytesToRead,
            <span style="color: #FF0000;">int</span><span style="color: #008000;">*</span> pNumberOfBytesRead,
            <span style="color: #FF0000;">int</span> Overlapped<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> SetFilePointerEx<span style="color: #000000;">&#40;</span>
            SafeFileHandle hFile,
            <span style="color: #FF0000;">long</span> liDistanceToMove,
            <span style="color: #0600FF;">out</span> <span style="color: #FF0000;">long</span> lpNewFilePointer,
            <span style="color: #FF0000;">uint</span> dwMoveMethod<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> FlushFileBuffers<span style="color: #000000;">&#40;</span>
            SafeFileHandle hFile<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>With that out of the way, I wrote a Stream class to make accessing the disk more &#8220;,NET-ish&#8221;.  Here&#8217;s the code for the stream class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.Win32.SafeHandles</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> DiskLib
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DiskStream <span style="color: #008000;">:</span> Stream
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> DEFAULT_SECTOR_SIZE <span style="color: #008000;">=</span> <span style="color: #FF0000;">512</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> BUFFER_SIZE <span style="color: #008000;">=</span> <span style="color: #FF0000;">4096</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> diskID<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> DiskInfo diskInfo<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> FileAccess desiredAccess<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> SafeFileHandle fileHandle<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> DiskInfo DiskInfo
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">diskInfo</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> SectorSize
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">diskInfo</span>.<span style="color: #0000FF;">BytesPerSector</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> DiskStream<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> diskID, FileAccess desiredAccess<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">diskID</span> <span style="color: #008000;">=</span> diskID<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">diskInfo</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DiskInfo<span style="color: #000000;">&#40;</span>diskID<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">desiredAccess</span> <span style="color: #008000;">=</span> desiredAccess<span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// if desiredAccess is Write or Read/Write</span>
            <span style="color: #008080; font-style: italic;">//   find volumes on this disk</span>
            <span style="color: #008080; font-style: italic;">//   lock the volumes using FSCTL_LOCK_VOLUME</span>
            <span style="color: #008080; font-style: italic;">//     unlock the volumes on Close() or in destructor</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileHandle</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">openFile</span><span style="color: #000000;">&#40;</span>diskID, desiredAccess<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> SafeFileHandle openFile<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> id, FileAccess desiredAccess<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">uint</span> access<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>desiredAccess<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">case</span> FileAccess.<span style="color: #0000FF;">Read</span><span style="color: #008000;">:</span>
                    access <span style="color: #008000;">=</span> DeviceIO.<span style="color: #0000FF;">GENERIC_READ</span><span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> FileAccess.<span style="color: #0000FF;">Write</span><span style="color: #008000;">:</span>
                    access <span style="color: #008000;">=</span> DeviceIO.<span style="color: #0000FF;">GENERIC_WRITE</span><span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> FileAccess.<span style="color: #0000FF;">ReadWrite</span><span style="color: #008000;">:</span>
                    access <span style="color: #008000;">=</span> DeviceIO.<span style="color: #0000FF;">GENERIC_READ</span> <span style="color: #008000;">|</span> DeviceIO.<span style="color: #0000FF;">GENERIC_WRITE</span><span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">default</span><span style="color: #008000;">:</span>
                    access <span style="color: #008000;">=</span> DeviceIO.<span style="color: #0000FF;">GENERIC_READ</span><span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            SafeFileHandle ptr <span style="color: #008000;">=</span> DeviceIO.<span style="color: #0000FF;">CreateFile</span><span style="color: #000000;">&#40;</span>
                id,
                access,
                DeviceIO.<span style="color: #0000FF;">FILE_SHARE_READ</span>,
                IntPtr.<span style="color: #0000FF;">Zero</span>,
                DeviceIO.<span style="color: #0000FF;">OPEN_EXISTING</span>,
                DeviceIO.<span style="color: #0000FF;">FILE_FLAG_NO_BUFFERING</span> <span style="color: #008000;">|</span> DeviceIO.<span style="color: #0000FF;">FILE_FLAG_WRITE_THROUGH</span>,
                IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ptr.<span style="color: #0000FF;">IsInvalid</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Marshal.<span style="color: #0000FF;">ThrowExceptionForHR</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">GetHRForLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> ptr<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> CanRead
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">desiredAccess</span> <span style="color: #008000;">==</span> FileAccess.<span style="color: #0000FF;">Read</span> <span style="color: #008000;">||</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">desiredAccess</span> <span style="color: #008000;">==</span> FileAccess.<span style="color: #0000FF;">ReadWrite</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #0600FF;">true</span> <span style="color: #008000;">:</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> CanWrite
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">desiredAccess</span> <span style="color: #008000;">==</span> FileAccess.<span style="color: #0000FF;">Write</span> <span style="color: #008000;">||</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">desiredAccess</span> <span style="color: #008000;">==</span> FileAccess.<span style="color: #0000FF;">ReadWrite</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #0600FF;">true</span> <span style="color: #008000;">:</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> CanSeek
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">long</span> Length
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">diskInfo</span>.<span style="color: #0000FF;">Size</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">long</span> Position
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">long</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>DeviceIO.<span style="color: #0000FF;">SetFilePointerEx</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileHandle</span>, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">out</span> n, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">uint</span><span style="color: #000000;">&#41;</span>SeekOrigin.<span style="color: #0000FF;">Current</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    Marshal.<span style="color: #0000FF;">ThrowExceptionForHR</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">GetHRForLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> n<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> EndOfStreamException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Cannot set position beyond the end of the disk.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">long</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>DeviceIO.<span style="color: #0000FF;">SetFilePointerEx</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileHandle</span>, value, <span style="color: #0600FF;">out</span> n, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">uint</span><span style="color: #000000;">&#41;</span>SeekOrigin.<span style="color: #0000FF;">Begin</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    Marshal.<span style="color: #0000FF;">ThrowExceptionForHR</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">GetHRForLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Flush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// not required, since FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING are used</span>
            <span style="color: #008080; font-style: italic;">//if (!Unmanaged.FlushFileBuffers(this.fileHandle))</span>
            <span style="color: #008080; font-style: italic;">//    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            DeviceIO.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileHandle</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> SetLength<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> value<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Setting the length is not supported with DiskStream objects.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">unsafe</span> <span style="color: #FF0000;">int</span> Read<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer, <span style="color: #FF0000;">int</span> offset, <span style="color: #FF0000;">int</span> count<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">fixed</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #008000;">*</span> p <span style="color: #008000;">=</span> buffer<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>DeviceIO.<span style="color: #0000FF;">ReadFile</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileHandle</span>, p <span style="color: #008000;">+</span> offset, count, <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>n, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    Marshal.<span style="color: #0000FF;">ThrowExceptionForHR</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">GetHRForLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> n<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">unsafe</span> <span style="color: #0600FF;">void</span> Write<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer, <span style="color: #FF0000;">int</span> offset, <span style="color: #FF0000;">int</span> count<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">fixed</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #008000;">*</span> p <span style="color: #008000;">=</span> buffer<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>DeviceIO.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileHandle</span>, p <span style="color: #008000;">+</span> offset, count, <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>n, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    Marshal.<span style="color: #0000FF;">ThrowExceptionForHR</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">GetHRForLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">long</span> Seek<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> offset, SeekOrigin origin<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">long</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>DeviceIO.<span style="color: #0000FF;">SetFilePointerEx</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileHandle</span>, offset, <span style="color: #0600FF;">out</span> n, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">uint</span><span style="color: #000000;">&#41;</span>origin<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                Marshal.<span style="color: #0000FF;">ThrowExceptionForHR</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">GetHRForLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> n<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> ReadSector<span style="color: #000000;">&#40;</span>DiskSector sector<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>sector.<span style="color: #0000FF;">Data</span>, <span style="color: #FF0000;">0</span>, sector.<span style="color: #0000FF;">SectorSize</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> WriteSector<span style="color: #000000;">&#40;</span>DiskSector sector<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sector.<span style="color: #0000FF;">Data</span>, <span style="color: #FF0000;">0</span>, sector.<span style="color: #0000FF;">SectorSize</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SeekSector<span style="color: #000000;">&#40;</span>DiskSector sector<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Seek</span><span style="color: #000000;">&#40;</span>sector.<span style="color: #0000FF;">Offset</span>, SeekOrigin.<span style="color: #0000FF;">Begin</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> DiskSector
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> DEFAULT_SECTOR_SIZE <span style="color: #008000;">=</span> <span style="color: #FF0000;">512</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">long</span> offset<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> data<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> sectorSize<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> SectorSize
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">sectorSize</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>value <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sector size must be a multiple of 2.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">sectorSize</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">long</span> Offset
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">offset</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>value <span style="color: #008000;">%</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SectorSize</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sector offset must be a multiple of SectorSize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">offset</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> Data
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">data</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SectorSize</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Data length must be the same as SectorSize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">data</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> DiskSector<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> sectorData, <span style="color: #FF0000;">long</span> sectorOffset, <span style="color: #FF0000;">int</span> sectorSize<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>sectorSize <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sector size must be a multiple of 2.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">sectorSize</span> <span style="color: #008000;">=</span> sectorSize<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>sectorData.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> sectorSize<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Data length must be the same as SectorSize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">data</span> <span style="color: #008000;">=</span> sectorData<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>sectorOffset <span style="color: #008000;">%</span> sectorSize<span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sector offset must be a multiple of SectorSize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">offset</span> <span style="color: #008000;">=</span> sectorOffset<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> DiskSector<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> sectorData, <span style="color: #FF0000;">long</span> sectorOffset<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> sectorSize <span style="color: #008000;">=</span> DEFAULT_SECTOR_SIZE<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">sectorSize</span> <span style="color: #008000;">=</span> sectorSize<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>sectorData.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> sectorSize<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Data length must be the same as SectorSize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">data</span> <span style="color: #008000;">=</span> sectorData<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>sectorOffset <span style="color: #008000;">%</span> sectorSize<span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sector offset must be a multiple of SectorSize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">offset</span> <span style="color: #008000;">=</span> sectorOffset<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> DiskSector<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> sectorOffset<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> sectorSize <span style="color: #008000;">=</span> DEFAULT_SECTOR_SIZE<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">sectorSize</span> <span style="color: #008000;">=</span> sectorSize<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">data</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>sectorSize<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>sectorOffset <span style="color: #008000;">%</span> sectorSize<span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sector offset must be a multiple of SectorSize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">offset</span> <span style="color: #008000;">=</span> sectorOffset<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You might&#8217;ve noticed the DiskInfo class, I&#8217;m not going to post the code here, but I&#8217;ll provide a download link.  It&#8217;s currently just a wrapper around WMI/Win32_DiskDrive, although I&#8217;m planning on removing the WMI code and replacing it with more platform invoke calls.  I&#8217;ve also added a DiskSector struct to (attempt to) make accessing the disk using sectors easier.  The DiskSector struct can be used with the ReadDiskSector(), WriteDiskSector() and SeekDiskSector() methods of the DiskStream class.</p>
<p>And finally a WARNING to anyone intending to use this code:  messing around with disks like this is EXTREMELY dangerous and you can hose your whole system very easily.  I HIGHLY recommend you play with this code within a Virtual Machine (VirtualBox, Qemu, etc) or on a separate system that you don&#8217;t mind losing.</p>
<p>It&#8217;s also important to note that this is very early code, and it has not at all been thoroughly tested in any way.  I&#8217;ve used it to toggle some bits in the MBR, read the partition table, and to wipe a disk clean, and that&#8217;s the extent of my testing.  Proceed with caution and at your own risk.</p>
<p>I&#8217;ll update this post as I improve the code here.</p>
<p><a href="http://codebrainz.ca/wp-content/uploads/2010/05/disklib.zip">Here&#8217;s a bunch of source files which could be useful in Zip format.</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2010%2F05%2F23%2Flow-level-disk-io-in-managed-net%2F&amp;linkname=Low%20Level%20Disk%20IO%20in%20Managed%20.NET"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2010/05/23/low-level-disk-io-in-managed-net/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Python Whois</title>
		<link>http://codebrainz.ca/index.php/2010/01/09/python-whois/</link>
		<comments>http://codebrainz.ca/index.php/2010/01/09/python-whois/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 08:50:26 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[Shell Scripts]]></category>

		<category><![CDATA[domain names]]></category>

		<category><![CDATA[select]]></category>

		<category><![CDATA[socket]]></category>

		<category><![CDATA[whois]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=137</guid>
		<description><![CDATA[A little Whois reader I wrote while messing around with sockets in Python.  It doesn&#8217;t do any processing on the reply, it just returns the whole thing as a string.  It&#8217;s also missing any mechanism to determine which Whois servers to use for which TLDs.  But anyway, it&#8217;s a good start!

#!/usr/bin/env python
import socket
import select
&#160;
PORT = [...]]]></description>
			<content:encoded><![CDATA[<p>A little Whois reader I wrote while messing around with sockets in Python.  It doesn&#8217;t do any processing on the reply, it just returns the whole thing as a string.  It&#8217;s also missing any mechanism to determine which Whois servers to use for which TLDs.  But anyway, it&#8217;s a good start!</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">select</span>
&nbsp;
PORT = <span style="color: #ff4500;">43</span>
BUFSIZE = <span style="color: #ff4500;">1024</span>
LINEEND = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> whois<span style="color: black;">&#40;</span>domain, server, port=PORT<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Perform a WHOIS search for domain on server/port.
    '</span><span style="color: #483d8b;">''</span>
    s = <span style="color: #dc143c;">socket</span>.<span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">AF_INET</span>, <span style="color: #dc143c;">socket</span>.<span style="color: black;">SOCK_STREAM</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>server, port<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    msg = <span style="color: #483d8b;">''</span>
    lookup = domain + LINEEND
    readable, writable, error = <span style="color: #dc143c;">select</span>.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>s<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,<span style="color: #ff4500;">60</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> s <span style="color: #ff7700;font-weight:bold;">in</span> writable: s.<span style="color: black;">send</span><span style="color: black;">&#40;</span>lookup<span style="color: black;">&#41;</span>
&nbsp;
    readable, writable, error = <span style="color: #dc143c;">select</span>.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>s<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,<span style="color: #ff4500;">60</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> s <span style="color: #ff7700;font-weight:bold;">in</span> readable:
&nbsp;
        data = s.<span style="color: black;">recv</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1024</span><span style="color: black;">&#41;</span>
        msg = msg + data
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> data:
            s.<span style="color: black;">shutdown</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">SHUT_RDWR</span><span style="color: black;">&#41;</span>
            s.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">break</span>
        readable, writable, error = <span style="color: #dc143c;">select</span>.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>s<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,<span style="color: #ff4500;">60</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        s.<span style="color: black;">shutdown</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">SHUT_RDRW</span><span style="color: black;">&#41;</span>
        s.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> msg.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> whois<span style="color: black;">&#40;</span><span style="color: #483d8b;">'google.com'</span>, <span style="color: #483d8b;">'whois.markmonitor.com'</span><span style="color: black;">&#41;</span></pre></div></div>

<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2010%2F01%2F09%2Fpython-whois%2F&amp;linkname=Python%20Whois"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2010/01/09/python-whois/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Testing an LCD Monitor for Bad Pixels</title>
		<link>http://codebrainz.ca/index.php/2009/10/11/testing-an-lcd-monitor-for-bad-pixels/</link>
		<comments>http://codebrainz.ca/index.php/2009/10/11/testing-an-lcd-monitor-for-bad-pixels/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 21:34:30 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[color changer]]></category>

		<category><![CDATA[dead pixel]]></category>

		<category><![CDATA[detector]]></category>

		<category><![CDATA[fullscreen]]></category>

		<category><![CDATA[lcd]]></category>

		<category><![CDATA[pygame]]></category>

		<category><![CDATA[stuck pixel]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=134</guid>
		<description><![CDATA[I wrote a little cross-platform Python script to run a monitor through a sequence of colors to assist in detecting stuck pixels.  The project is hosted here.
]]></description>
			<content:encoded><![CDATA[<p>I wrote a little cross-platform Python script to run a monitor through a sequence of colors to assist in detecting stuck pixels.  The project is <a href="http://code.google.com/p/lcdpixeltest/">hosted here</a>.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2009%2F10%2F11%2Ftesting-an-lcd-monitor-for-bad-pixels%2F&amp;linkname=Testing%20an%20LCD%20Monitor%20for%20Bad%20Pixels"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2009/10/11/testing-an-lcd-monitor-for-bad-pixels/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Python Module for Geo-Coding/Geo-Location</title>
		<link>http://codebrainz.ca/index.php/2009/10/05/python-module-for-geo-codinggeo-location/</link>
		<comments>http://codebrainz.ca/index.php/2009/10/05/python-module-for-geo-codinggeo-location/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 01:00:05 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[Shell Scripts]]></category>

		<category><![CDATA[canada]]></category>

		<category><![CDATA[geocoding]]></category>

		<category><![CDATA[geolocation]]></category>

		<category><![CDATA[local]]></category>

		<category><![CDATA[lookup]]></category>

		<category><![CDATA[offline]]></category>

		<category><![CDATA[postal code]]></category>

		<category><![CDATA[reverse]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=132</guid>
		<description><![CDATA[I&#8217;ve written a very simple python module/script to help find postal code, city, province, latitude/longitude matching the supplied postal code, city, province, or coordinate.
Check out the Google Code Project page for more information.
You can checkout the source code by issuing the following command:

svn checkout http://python-geolocate.googlecode.com/svn/trunk/ python-geolocate-read-only

]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a very simple python module/script to help find postal code, city, province, latitude/longitude matching the supplied postal code, city, province, or coordinate.</p>
<p><a href="http://code.google.com/p/python-geolocate/" target="_blank">Check out the Google Code Project page for more information.</a></p>
<p>You can checkout the source code by issuing the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> checkout http:<span style="color: #000000; font-weight: bold;">//</span>python-geolocate.googlecode.com<span style="color: #000000; font-weight: bold;">/</span>svn<span style="color: #000000; font-weight: bold;">/</span>trunk<span style="color: #000000; font-weight: bold;">/</span> python-geolocate-read-only</pre></div></div>

<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2009%2F10%2F05%2Fpython-module-for-geo-codinggeo-location%2F&amp;linkname=Python%20Module%20for%20Geo-Coding%2FGeo-Location"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2009/10/05/python-module-for-geo-codinggeo-location/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Osmosis Won&#8217;t Read &#8216;planet-latest.osm.bz2&#8242;</title>
		<link>http://codebrainz.ca/index.php/2009/09/29/osmosis-wont-read-planet-latestosmbz2/</link>
		<comments>http://codebrainz.ca/index.php/2009/09/29/osmosis-wont-read-planet-latestosmbz2/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 03:02:02 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[bz2]]></category>

		<category><![CDATA[bzip2]]></category>

		<category><![CDATA[corrupt]]></category>

		<category><![CDATA[openstreetmap]]></category>

		<category><![CDATA[osm]]></category>

		<category><![CDATA[osmosis]]></category>

		<category><![CDATA[planet file]]></category>

		<category><![CDATA[planet-latest.bz2]]></category>

		<category><![CDATA[saxparseexception]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=129</guid>
		<description><![CDATA[I couldn&#8217;t get osmosis to read my OSM planet file, I even tried downloading another one, which of course took a long time.  Then I found a post on the mailing list talking about the problem.
The solution is rather simple, just extract the file before passing it to osmosis to read, for example:

bzcat planet-latest.osm.bz2 [...]]]></description>
			<content:encoded><![CDATA[<p>I couldn&#8217;t get <a href="http://wiki.openstreetmap.org/index.php/Osmosis" target="_blank">osmosis</a> to read my OSM planet file, I even tried downloading another one, which of course took a long time.  Then I found a post on the <a href="http://www.nabble.com/osmosis---planet-import-pgsql-simple-schema-td23422590.html" target="_blank">mailing list</a> talking about the problem.</p>
<p>The solution is rather simple, just extract the file before passing it to osmosis to read, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">bzcat</span> planet-latest.osm.bz2 <span style="color: #000000; font-weight: bold;">|</span> .<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>osmosis <span style="color: #660033;">--read-xml</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">file</span></span>=- <span style="color: #660033;">--bounding-polygon</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">file</span></span>=<span style="color: #ff0000;">&quot;country2pts.txt&quot;</span> <span style="color: #660033;">--write-xml</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">file</span></span>=<span style="color: #ff0000;">&quot;country.osm.EXT&quot;</span></pre></div></div>

<p>The error message I received was this:</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">kManager waitForCompletion
SEVERE: Thread for task 1-read-xml failed
org.openstreetmap.osmosis.core.OsmosisRuntimeException: Unable to parse xml file planet-latest.osm.bz2.  publicId=(null), systemId=(null), lineNumber=4313, columnNumber=13.
	at org.openstreetmap.osmosis.core.xml.v0_6.XmlReader.run(XmlReader.java:113)
	at java.lang.Thread.run(Thread.java:636)
Caused by: org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
	at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
	at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
	at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:391)
	at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1390)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.endEntity(XMLDocumentFragmentScannerImpl.java:878)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.endEntity(XMLDocumentScannerImpl.java:581)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.endEntity(XMLEntityManager.java:1369)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1740)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(XMLEntityScanner.java:1469)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(XMLDocumentFragmentScannerImpl.java:1351)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1286)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2723)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:624)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:486)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:810)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:740)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:110)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:525)
	at javax.xml.parsers.SAXParser.parse(SAXParser.java:392)
	at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
	at org.openstreetmap.osmosis.core.xml.v0_6.XmlReader.run(XmlReader.java:108)
	... 1 more
Sep 28, 2009 7:30:16 PM org.openstreetmap.osmosis.core.Osmosis main
SEVERE: Execution aborted.
org.openstreetmap.osmosis.core.OsmosisRuntimeException: One or more tasks failed.
	at org.openstreetmap.osmosis.core.pipeline.common.Pipeline.waitForCompletion(Pipeline.java:146)
	at org.openstreetmap.osmosis.core.Osmosis.run(Osmosis.java:85)
	at org.openstreetmap.osmosis.core.Osmosis.main(Osmosis.java:30)</pre></div></div>

<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2009%2F09%2F29%2Fosmosis-wont-read-planet-latestosmbz2%2F&amp;linkname=Osmosis%20Won%26%238217%3Bt%20Read%20%26%238216%3Bplanet-latest.osm.bz2%26%238242%3B"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2009/09/29/osmosis-wont-read-planet-latestosmbz2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Installing Mapnik and Friends on Ubuntu 9.04</title>
		<link>http://codebrainz.ca/index.php/2009/09/27/installing-mapnik-and-friends-on-ubuntu-904/</link>
		<comments>http://codebrainz.ca/index.php/2009/09/27/installing-mapnik-and-friends-on-ubuntu-904/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 22:18:08 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[GNU/Linux]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[extracts]]></category>

		<category><![CDATA[mapnik]]></category>

		<category><![CDATA[mapnik-utils]]></category>

		<category><![CDATA[openstreetmap]]></category>

		<category><![CDATA[osm]]></category>

		<category><![CDATA[osm2pgsql]]></category>

		<category><![CDATA[postgis]]></category>

		<category><![CDATA[postgresql]]></category>

		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=115</guid>
		<description><![CDATA[I&#8217;m posting this for anyone who&#8217;s had a hard time getting Mapnik installed
along with some handy utilities on Ubuntu 9.04.  Here we go.
Mapnik from Source
Update the package listing and then upgrade all packages:

apt-get update
apt-get upgrade

After that install the dependencies:

sudo apt-get install g++ cpp \
libboost1.35-dev libboost-filesystem1.35-dev \
libboost-iostreams1.35-dev libboost-program-options1.35-dev \
libboost-python1.35-dev libboost-regex1.35-dev \
libboost-thread1.35-dev \
libxml2 libxml2-dev \
libfreetype6 libfreetype6-dev [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m posting this for anyone who&#8217;s had a hard time getting Mapnik installed<br />
along with some handy utilities on Ubuntu 9.04.  Here we go.</p>
<h2>Mapnik from Source</h2>
<p>Update the package listing and then upgrade all packages:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> update
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> upgrade</pre></div></div>

<p>After that install the dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">g++</span> <span style="color: #c20cb9; font-weight: bold;">cpp</span> \
libboost1.35-dev libboost-filesystem1.35-dev \
libboost-iostreams1.35-dev libboost-program-options1.35-dev \
libboost-python1.35-dev libboost-regex1.35-dev \
libboost-thread1.35-dev \
libxml2 libxml2-dev \
libfreetype6 libfreetype6-dev \
libjpeg62 libjpeg62-dev \
libltdl7 libltdl7-dev \
libpng12-<span style="color: #000000;">0</span> libpng12-dev \
libgeotiff-dev libtiff4 libtiff4-dev \
libcairo2 libcairo2-dev python-cairo python-cairo-dev \
libcairomm-<span style="color: #000000;">1.0</span>-<span style="color: #000000;">1</span> libcairomm-<span style="color: #000000;">1.0</span>-dev \
ttf-dejavu ttf-dejavu-core ttf-dejavu-extra \
libgdal1-dev python-gdal \
postgresql-<span style="color: #000000;">8.3</span>-postgis postgresql-<span style="color: #000000;">8.3</span> \
postgresql-server-dev-<span style="color: #000000;">8.3</span> postgresql-contrib-<span style="color: #000000;">8.3</span> \
libsqlite3-dev  \
subversion build-essential</pre></div></div>

<p>Now we need to get the Mapnik source from the repository:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-v</span> ~<span style="color: #000000; font-weight: bold;">/</span>src
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>src
<span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.mapnik.org<span style="color: #000000; font-weight: bold;">/</span>trunk mapnik
<span style="color: #7a0874; font-weight: bold;">cd</span> mapnik</pre></div></div>

<p>After that we can configure and build Mapnik:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python scons<span style="color: #000000; font-weight: bold;">/</span>scons.py configure <span style="color: #007800;">INPUT_PLUGINS</span>=all \
<span style="color: #007800;">OPTIMIZATION</span>=<span style="color: #000000;">3</span> \
<span style="color: #007800;">SYSTEM_FONTS</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>truetype<span style="color: #000000; font-weight: bold;">/</span>ttf-dejavu<span style="color: #000000; font-weight: bold;">/</span>
python scons<span style="color: #000000; font-weight: bold;">/</span>scons.py
<span style="color: #c20cb9; font-weight: bold;">sudo</span> python scons<span style="color: #000000; font-weight: bold;">/</span>scons.py <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> ldconfig</pre></div></div>

<p>Now we&#8217;ll do a quick test to see if Mapnik installed correctly:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python <span style="color: #666666; font-style: italic;"># will start the python interpreter</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> import mapnik
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> <span style="color: #7a0874; font-weight: bold;">exit</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>If the <code>import mapnik</code> line didn&#8217;t spit out any errors, everything is probably ok.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-2559489894295068";
/* CodeBrainz 468x60, created 9/10/09 */
google_ad_slot = "1160474372";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h2>Setting Up the Database</h2>
<p>First we need to tweak some configuration files.  Open<br />
<code>/etc/postgresql/8.3/main/postgresql.conf</code> with your favourite<br />
text edit and modify the following lines:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">shared_buffers = 128MB
checkpoint_segments = <span style="color: #000000;">20</span>
maintenance_work_mem = 256MB
autovacuum = off</pre></div></div>

<p>Next edit <code>/etc/sysctl.conf</code> and add the following at the end:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">kernel.shmmax=<span style="color: #000000;">268435456</span></pre></div></div>

<p>Now to avoid a reboot, we&#8217;ll update the kernel like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'echo 268435456 &gt;/proc/sys/kernel/shmmax'</span></pre></div></div>

<p>Then restart the PostgreSQL server:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>postgresql-<span style="color: #000000;">8.3</span> restart</pre></div></div>

<p>The next thing we need to do is create the database. Substitute<br />
&#8220;<code><span style="color: red; ">your_user</span></code>&#8221; below with your user name.  Note: If your user name is &#8216;user&#8217; you might get an error from the commands below, I did when I tried.

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #660033;">-u</span> postgres <span style="color: #660033;">-i</span>
createuser <span style="color: #660033;">-s</span> your_user
createdb <span style="color: #660033;">-E</span> UTF8 <span style="color: #660033;">-O</span> your_user gis
createlang plpgsql gis
<span style="color: #7a0874; font-weight: bold;">exit</span></pre></div></div>

<p>Now setup PostGIS on our database:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">psql <span style="color: #660033;">-d</span> gis <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>postgresql-<span style="color: #000000;">8.3</span>-postgis<span style="color: #000000; font-weight: bold;">/</span>lwpostgis.sql</pre></div></div>

<p>And then change some permissions on the database:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ALTER TABLE geometry_columns OWNER TO your_user; ALTER TABLE spatial_ref_sys OWNER TO your_user;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> psql <span style="color: #660033;">-d</span> gis</pre></div></div>

<p>Now we&#8217;ll enable the <code>intarray</code> module:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">psql gis <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>postgresql<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8.3</span><span style="color: #000000; font-weight: bold;">/</span>contrib<span style="color: #000000; font-weight: bold;">/</span>_int.sql</pre></div></div>

<h2>Installing Some Extra Tools</h2>
<p>The tools we&#8217;ll be installing here are useful for working with OpenStreetMap<br />
data and also to facilitate generating maps with Mapnik.</p>
<h3>OSM2PGSQL</h3>
<p>This utility will import OpenStreetMap data files into the PostGIS database.  First we&#8217;ll install some dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> build-essential libxml2-dev libgeos-dev libpq-dev libbz2-dev proj</pre></div></div>

<p>And then checkout the latest version from the repository:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>src
<span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.openstreetmap.org<span style="color: #000000; font-weight: bold;">/</span>applications<span style="color: #000000; font-weight: bold;">/</span>utils<span style="color: #000000; font-weight: bold;">/</span>export<span style="color: #000000; font-weight: bold;">/</span>osm2pgsql<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> osm2pgsql
<span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<h3>Mapnik-Tools</h3>
<p>We&#8217;re going to put this right in our home directory:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~
<span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.openstreetmap.org<span style="color: #000000; font-weight: bold;">/</span>applications<span style="color: #000000; font-weight: bold;">/</span>rendering<span style="color: #000000; font-weight: bold;">/</span>mapnik<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<h2>Setting up the World</h2>
<p>Now we&#8217;re going to grab some boundaries data for the world.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>mapnik
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>tile.openstreetmap.org<span style="color: #000000; font-weight: bold;">/</span>world_boundaries-spherical.tgz
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>tile.openstreetmap.org<span style="color: #000000; font-weight: bold;">/</span>processed_p.tar.bz2
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>tile.openstreetmap.org<span style="color: #000000; font-weight: bold;">/</span>shoreline_300.tar.bz2</pre></div></div>

<p>We&#8217;ll extract them each into <code>~/mapnik/world_boundaries</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> xfv world_boundaries-spherical.tgz
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-v</span> world_boundaries-spherical.tgz
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #660033;">-v</span> processed_p.tar.bz2 shoreline_300.tar.bz2 world_boundaries<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> world_boundaries
<span style="color: #c20cb9; font-weight: bold;">tar</span> xfv processed_p.tar.bz2
<span style="color: #c20cb9; font-weight: bold;">tar</span> xfv shoreline_300.tar.bz2
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-v</span> processed_p.tar.bz2 shoreline_300.tar.bz2</pre></div></div>

<h2>Importing Some Data</h2>
<p>The last thing we&#8217;re going to do is put some OpenStreetMap data into our database.  You can either import the whole planet:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>ftp.heanet.ie<span style="color: #000000; font-weight: bold;">/</span>mirrors<span style="color: #000000; font-weight: bold;">/</span>openstreetmap.org<span style="color: #000000; font-weight: bold;">/</span>planet-latest.osm.bz2</pre></div></div>

<p>But it&#8217;s a HUGE file, so if you just want to have a smaller part of the world,<br />
you can use extracts.  To create your own extracts, <a href="http://www.weait.com/content/make-your-first-map" target="_blank">read this tutorial</a>.  Otherwise<br />
grab an extract for your area of interest from somewhere (ex.<a href="http://downloads.cloudmade.com/" target="_blank">CloudMade</a>).  You&#8217;ll want to download the file ending in <code>.osm.bz2</code>.  In my example I&#8217;ll be using <a href="http://downloads.cloudmade.com/north_america/canada/british_columbia/british_columbia.osm.bz2" target="_blank">british_columbia.osm.bz2</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>downloads.cloudmade.com<span style="color: #000000; font-weight: bold;">/</span>north_america<span style="color: #000000; font-weight: bold;">/</span>canada<span style="color: #000000; font-weight: bold;">/</span>british_columbia<span style="color: #000000; font-weight: bold;">/</span>british_columbia.osm.bz2</pre></div></div>

<p>We just need to import the data into the database now, for this we&#8217;ll use osm2pgsql which we installed earlier.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>osm2pgsql
.<span style="color: #000000; font-weight: bold;">/</span>osm2pgsql <span style="color: #660033;">--slim</span> <span style="color: #660033;">-d</span> gis ~<span style="color: #000000; font-weight: bold;">/</span>british_columbia.osm.bz2</pre></div></div>

<p>And there you have it.  If you want to test this out from the command line, you<br />
can use the <a href="http://www.weait.com/content/make-your-first-map" target="_blank">last portion of this tutorial</a>, or to use python, try the <a href="http://mapnik.org/documentation/" target="_blank">examples here</a>.</p>
<h2>Resources</h2>
<p>Most of this tutorial was ripped directly off from the following sites:</p>
<ul>
<li><a href="http://weait.com/content/build-your-own-openstreetmap-server" target="_blank">http://weait.com/content/build-your-own-openstreetmap-server</a></li>
<li><a href="http://www.weait.com/content/make-your-first-map" target="_blank">http://www.weait.com/content/make-your-first-map</a></li>
<li><a href="http://trac.mapnik.org/wiki/UbuntuInstallation" target="_blank">http://trac.mapnik.org/wiki/UbuntuInstallation</a></li>
<li><a href="http://wiki.openstreetmap.org/wiki/Mapnik#World_Boundaries" target="_blank">http://wiki.openstreetmap.org/wiki/Mapnik#World_Boundaries</a></li>
</ul>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2009%2F09%2F27%2Finstalling-mapnik-and-friends-on-ubuntu-904%2F&amp;linkname=Installing%20Mapnik%20and%20Friends%20on%20Ubuntu%209.04"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2009/09/27/installing-mapnik-and-friends-on-ubuntu-904/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Mapnik Package for Ubuntu</title>
		<link>http://codebrainz.ca/index.php/2009/09/26/ubuntu-package-for-mapnik/</link>
		<comments>http://codebrainz.ca/index.php/2009/09/26/ubuntu-package-for-mapnik/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 21:55:38 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[GNU/Linux]]></category>

		<category><![CDATA[gis]]></category>

		<category><![CDATA[jaunty]]></category>

		<category><![CDATA[mapnik]]></category>

		<category><![CDATA[maps]]></category>

		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=107</guid>
		<description><![CDATA[I made a Debian/Ubuntu package for Mapnik v0.6.1.  There&#8217;s a 32-bit and 64-bit package.  I&#8217;ve only tested it on Ubuntu 9.04, but it will probably work on Debian.
To install it download the package and type:
sudo dpkg -i mapnik_0.6.1-1_i386.deb
Then when it complains about dependencies, type:
sudo apt-get install -f
To install all the dependencies and finish [...]]]></description>
			<content:encoded><![CDATA[<p>I made a Debian/Ubuntu package for Mapnik v0.6.1.  There&#8217;s a 32-bit and 64-bit package.  I&#8217;ve only tested it on Ubuntu 9.04, but it will probably work on Debian.</p>
<p>To install it download the package and type:</p>
<p><code>sudo dpkg -i mapnik_0.6.1-1_i386.deb</code></p>
<p>Then when it complains about dependencies, type:</p>
<p><code>sudo apt-get install -f</code></p>
<p>To install all the dependencies and finish the install.  Sorry I don&#8217;t have a repository to put this package in.</p>
<p>I think if you use GDebi Installer it will do everything in one step.</p>
<p>Note: There are lots of dependencies (~500MB on a clean Ubuntu alternate install), I used those listed <a href="http://trac.mapnik.org/wiki/UbuntuInstallation" target="_blank">here</a>.  I tried removing all the -dev packages and anything else that looked like it was just for compiling, but it didn&#8217;t work.</p>
<p>UPDATE: I&#8217;ve also made an <code>osm2pgsql</code> package from the latest subversion copy.  <a href='http://codebrainz.ca/wp-content/uploads/2009/09/osm2pgsql_20090926-1_i386.deb'>Download it here</a>.</p>
<p>Enjoy</p>
<p><a href='http://codebrainz.ca/wp-content/uploads/2009/09/mapnik_061-1_i386.deb'>Download the Mapnik v0.6.1 x86 Package Here</a></p>
<p><a href='http://codebrainz.ca/wp-content/uploads/2009/09/mapnik_061-1_amd64.deb'>Download the Mapnik v0.6.1 amd64 Package Here</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2009%2F09%2F26%2Fubuntu-package-for-mapnik%2F&amp;linkname=Mapnik%20Package%20for%20Ubuntu"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2009/09/26/ubuntu-package-for-mapnik/feed/</wfw:commentRss>
		</item>
		<item>
		<title>GPS Device Detection in Python</title>
		<link>http://codebrainz.ca/index.php/2009/09/20/gps-device-detection-in-python/</link>
		<comments>http://codebrainz.ca/index.php/2009/09/20/gps-device-detection-in-python/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 00:02:56 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[GNU/Linux]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[Shell Scripts]]></category>

		<category><![CDATA[detection]]></category>

		<category><![CDATA[device]]></category>

		<category><![CDATA[gps]]></category>

		<category><![CDATA[pyserial]]></category>

		<category><![CDATA[receiver]]></category>

		<category><![CDATA[serial]]></category>

		<category><![CDATA[usb]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=101</guid>
		<description><![CDATA[As part of one of my current projects, maptop, I needed a simple module to detect whether or not a GPS receiver is connected to the computer, and if so, which port is it connected to.
For the task, I used PySerial, along with some of their example code, to test if each serial port can [...]]]></description>
			<content:encoded><![CDATA[<p>As part of one of my current projects, <a title="Maptop - Turn your laptop/netbook into a GPS device." href="http://maptop.googlecode.com" target="_blank">maptop</a>, I needed a simple module to detect whether or not a GPS receiver is connected to the computer, and if so, which port is it connected to.</p>
<p>For the task, I used <a title="PySerial" href="http://pyserial.sourceforge.net/" target="_blank">PySerial</a>, along with <a title="Example Code from PySerial by Maptop" href="http://pyserial.sourceforge.net/examples.html#finding-serial-ports" target="_blank">some of their example code</a>, to test if each serial port can be opened, and if so, if there are NMEA GPS sentences coming in on it.  It uses a fixed baud rate of 4800, which is what my receiver is using and is also <a title="NMEA 0183 - Serial Configuration" href="http://en.wikipedia.org/wiki/NMEA_0183#Serial_configuration_.28data_link_layer.29" target="_blank">specified here</a>.  The timeout of 2 seconds seems to work (at least with my device).  Both the baud rate and the timeout can be changed by modifying the <code>BAUD_RATE</code> and <code>TIMEOUT</code> &#8220;constants&#8221; at the top of the script.</p>
<p>I&#8217;m not sure if this works on Windows or not, I haven&#8217;t tested it yet, but it *should work*.</p>
<p>Below is the code for the module:</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-2559489894295068";
/* CodeBrainz 468x60, created 9/10/09 */
google_ad_slot = "1160474372";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># Copyright 2009 Matthew Brush &lt; mbrush AT leftclick DOT ca &gt;</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># This program is free software; you can redistribute it and/or modify</span>
<span style="color: #808080; font-style: italic;"># it under the terms of the GNU General Public License as published by</span>
<span style="color: #808080; font-style: italic;"># the Free Software Foundation; either version 2 of the License, or</span>
<span style="color: #808080; font-style: italic;"># (at your option) any later version.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># This program is distributed in the hope that it will be useful,</span>
<span style="color: #808080; font-style: italic;"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span style="color: #808080; font-style: italic;"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span style="color: #808080; font-style: italic;"># GNU General Public License for more details.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># You should have received a copy of the GNU General Public License</span>
<span style="color: #808080; font-style: italic;"># along with this program; if not, write to the Free Software</span>
<span style="color: #808080; font-style: italic;"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,</span>
<span style="color: #808080; font-style: italic;"># MA 02110-1301, USA.</span>
&nbsp;
__version__ = <span style="color: #483d8b;">&quot;0.1&quot;</span>
__all__ = <span style="color: black;">&#91;</span> <span style="color: #483d8b;">'get_gps_port'</span>, <span style="color: #483d8b;">'test_port'</span> <span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> serial
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">glob</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">platform</span>
&nbsp;
BAUD_RATE = <span style="color: #ff4500;">4800</span>    <span style="color: #808080; font-style: italic;"># the standard nmea baud rate</span>
TIMEOUT = <span style="color: #ff4500;">2</span>         <span style="color: #808080; font-style: italic;"># time to wait for nmea sentences</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> NoGpsReceiverError<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">value</span> = value
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">value</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> scan<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Scan for available ports. return a list of device names. &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">platform</span>.<span style="color: black;">system</span> == <span style="color: #483d8b;">'Windows'</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> scanwin32
        ports = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> order, port, desc, hwid <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>scanwin32.<span style="color: black;">comports</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            ports.<span style="color: black;">append</span><span style="color: black;">&#40;</span>port<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> ports
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> scanlinux
        <span style="color: #ff7700;font-weight:bold;">return</span> scanlinux.<span style="color: black;">scan</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> test_port<span style="color: black;">&#40;</span>port<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Detects whether NMEA GPS sentences can be read from the port. &quot;&quot;&quot;</span>
    nmea_gps_pattern = <span style="color: #483d8b;">'^<span style="color: #000099; font-weight: bold;">\$</span>GP[A-Z]{3},'</span>
    gps_detected = <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        s = serial.<span style="color: black;">Serial</span><span style="color: black;">&#40;</span>port, BAUD_RATE, timeout=TIMEOUT<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:   <span style="color: #808080; font-style: italic;"># try 10 lines in a row</span>
            line = s.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            m = <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span>nmea_gps_pattern, line<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> m:
                gps_detected = <span style="color: #008000;">True</span>
                <span style="color: #ff7700;font-weight:bold;">break</span>
        s.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> gps_detected
    <span style="color: #ff7700;font-weight:bold;">except</span> serial.<span style="color: black;">SerialException</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_gps_port<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Looks for GPS data coming in on any serial port and returns the port name. &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> port <span style="color: #ff7700;font-weight:bold;">in</span> scan<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> test_port<span style="color: black;">&#40;</span>port<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> port
    <span style="color: #ff7700;font-weight:bold;">raise</span> NoGpsReceiverError<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Unable to locate a suitable GPS receiver.'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Testing ports to find GPS receiver...&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">platform</span>.<span style="color: black;">system</span> == <span style="color: #483d8b;">'Windows'</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;  Warning: gpsdetect not tested in Windows yet.&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> port <span style="color: #ff7700;font-weight:bold;">in</span> scan<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> test_port<span style="color: black;">&#40;</span>port<span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Detected GPS receiver on '&quot;</span> + port + <span style="color: #483d8b;">&quot;'.&quot;</span>
                found_gps = <span style="color: #008000;">True</span>
                <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> NoGpsReceiverError:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;No GPS receivers were detected on the system.&quot;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>: main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The complete source can be <a href="http://codebrainz.ca/wp-content/uploads/2009/09/gpsdetecttar.bz2">downloaded here</a>.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2009%2F09%2F20%2Fgps-device-detection-in-python%2F&amp;linkname=GPS%20Device%20Detection%20in%20Python"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2009/09/20/gps-device-detection-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Keeping USB Drive from Sleeping</title>
		<link>http://codebrainz.ca/index.php/2009/09/15/keeping-usb-drive-from-sleeping/</link>
		<comments>http://codebrainz.ca/index.php/2009/09/15/keeping-usb-drive-from-sleeping/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 02:07:59 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[GNU/Linux]]></category>

		<category><![CDATA[awake]]></category>

		<category><![CDATA[cifs]]></category>

		<category><![CDATA[crashing]]></category>

		<category><![CDATA[cron]]></category>

		<category><![CDATA[drive]]></category>

		<category><![CDATA[file server]]></category>

		<category><![CDATA[nas]]></category>

		<category><![CDATA[samba]]></category>

		<category><![CDATA[sleep]]></category>

		<category><![CDATA[sleeping]]></category>

		<category><![CDATA[ubuntu]]></category>

		<category><![CDATA[usb]]></category>

		<guid isPermaLink="false">http://codebrainz.ca/?p=97</guid>
		<description><![CDATA[I finally got so fed up of my Samba shares not being accessible after a while that I did some digging around.  One of my 3 external USB drives attached to my file server keeps going to sleep, which sounds like a good thing.  What happens, however, is when I attempt to access [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got so fed up of my Samba shares not being accessible after a while that I did some digging around.  One of my 3 external USB drives attached to my file server keeps going to sleep, which sounds like a good thing.  What happens, however, is when I attempt to access the shares from my Windows machine, I get a &#8220;Not Accessible&#8221; error and I can no longer use any of my Samba shares.</p>
<p>My file server is running Ubuntu 9.04 and all the drives are mounted under <code>/media</code>, then each important directory from each drive is symlinked to a sub directory of <code>/media/shared</code>, and <code>/media/shared</code> is the Samba share.  This is convenient because it allows my file server, which has 5 various sized disks and attachments, to appear as one huge drive on the network.  I&#8217;m not sure if this is why the one sleepy drive is bringing down the whole share or not.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-2559489894295068";
/* CodeBrainz 468x60, created 9/10/09 */
google_ad_slot = "1160474372";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>To resolve the issue, I keep having to SSH into my file server, run a command such as <code>fdisk -l</code> to access the drive(s) and then restart Samba.  There is a noticeable delay when <code>fdisk -l</code> hits that one sleepy drive while it spins up.  My super simple hack to prevent this drive from spinning down, even though I&#8217;d rather let it sleep and wake on its own, is this:</p>
<pre>
$ sudo crontab -e
</pre>
<p>Then I added the following line to the cron tab file:</p>
<pre>
5 * * * * fdisk -l >/dev/null 2>&#038;1
</pre>
<p>This will issue the <code>fdisk -l</code> command every 5 minutes, dumping the <code>fdisk</code> output to neverland.  The idea is to access the drive every once in a while to prevent it from going to sleep.  I expect to do some tweaking on the 5 minutes part, in an attempt to run the job less frequently, but still enough to keep the drive awake.</p>
<p>It&#8217;s an ugly hack and I&#8217;m sure there&#8217;s something I could do with <code>hdparm</code> if I spent some more time on it, but whatever.  It&#8217;s only me accessing the file server and the performance is not very critical.</p>
<p>If you do know of slicker way of doing this, drop a comment and let me know.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodebrainz.ca%2Findex.php%2F2009%2F09%2F15%2Fkeeping-usb-drive-from-sleeping%2F&amp;linkname=Keeping%20USB%20Drive%20from%20Sleeping"><img src="http://codebrainz.ca/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codebrainz.ca/index.php/2009/09/15/keeping-usb-drive-from-sleeping/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
