a3nm's blog

Installing CyanogenMod on a Galaxy Nexus (GSM)

— updated

I just installed CyanogenMod on my Galaxy Nexus phone. There is an official guide; here is my summary of what you need to do.

Backup your data
The process will reset your phone, so you need to back up all your data. An useful open-source program to take care of (part of) this is Slight backup.
Retrieve fastboot
Follow these instructions to install fastboot. I'm not sure that this was entirely straightforward, but I did this long ago so I'm not really sure. fastboot is now packaged for Debian so it is much simpler to install: apt-get install android-tools-fastboot.
Unlock the bootloader
Power down the device, and press the power, volume up and volume down buttons simultaneously for a few seconds. You will thus reach the bootloader. Connect the USB cable and run fastboot oem unlock, and confirm. This will reset the device and unlock the bootloader.
Retrieve and run ClockworkMod
I trust the official guide to have an up-to-date ClockworkMod download link. This being said, you don't need to keep ClockworkMod on your device: you can just boot it as needed. To do so, get to the bootloader like in the previous section, and run fastboot boot CLOCKWORK where CLOCKWORK is the ClockworkMod image file.
Perform a backup
Use ClockworkMod to back up the device before installing anything else, and use adb to retrieve the backup to your computer.
Format all partitions
This is an important step missing from the official guide: you should format /cache, /system and /data before installing CyanogenMod. Otherwise, in my case, CyanogenMod was stuck at the boot animation and adb logcat seemed to suggest that it had to do with a NullPointerException while reading the existing settings.
Retrieve CyanogenMod
Download a CyanogenMod image from this page. I first thought I'd go with a stable version, but I picked the latest nightly as of this writing (cm-10-20120923-NIGHTLY-maguro.zip) and had no problems with it yet.
Install CyanogenMod
Use adb to push the downloaded image to the sdcard folder on the device, and install the image using ClockworkMod.

irctk -- an IRC toolkit

— updated

There are a lot of language-specific libraries to interact with IRC and write bots, but if you want to do this from the shell, the only option I know of is to use ii. Sadly, ii is based on the idea of setting a connection up and interacting with filesystem objects, which is inconvenient if you just want to hack something together in one line like you do with netcat for TCP connections. This post presents irctk, a C program I wrote using libircclient. irctk connects to a server specified on the CLI and uses its standard input and output to read what it should say and write what it just heard. With irctk, you can do stuff like:

# output your server log events on irc
ssh server tail -f logfile.log | irctk example.com '#dashboard'
# timestamp and log irc messages to a file
irctk example.com '#chan' | awk '{ print strftime("%s"), $0; fflush() }' >file

You can also write programs which interact with their standard input and output and then just lift them to IRC with irctk. An example of this is wikifirc, a tool to filter irc.wikimedia.org on specific pages and users. The general scheme is just:

mkfifo fifo
cat fifo | irctk example.com '#chan' | program > fifo

Or you can just write simple programs directly in bash. As a convenience, irctk has options to filter incoming messages and only keep those which are specifically addressed to him, and it can reply automatically to the person who addressed him on the channel where it was addressed. For example, if you address the following bot like "fingerbot: foobar" or "/msg fingerbot foobar", it will reply with information about user foobar found with the finger command:

cat fifo | irctk -Fr fingerbot@example.com '#chat' |
  while read; do
    finger -s -- "$REPLY" 2>&1 | tail -1
  done >fifo

Here is a funnier (bash-specific) example: a bot to roll dice like "dmbot: 3d42" (thanks, p4bl0!): Fixed the code to avoid modulo bias

cat fifo | irctk -Fr dmbot@example.com '#chat' |
while read line; do
  if grep -E '^[0-9]{1,2}d[1-9][0-9]{0,2}$' <<<"$line" &>/dev/null; then
    D=(${line/d/ })
    for ((i = 0; i < ${D[0]}; i++)); do
      shuf -i1-${D[1]} -n1 | tr '\n' ' '
    done
    echo
  else
    echo "format error: must be NdM with N<100 and M<1000"
  fi
done >fifo

irctk has a few other features, like support for commands like "/join", "/nick", "/part", etc., to be able to script actions. Installing irctk should be as easy as installing libircclient (by hand, the version packaged in e.g. Debian is not recent enough as of this writing), and then typing:

git clone 'https://a3nm.net/git/irctk'
cd irctk
make

You can check the README for more information. Comments, suggestions, bug reports and feature requests are welcome at <a3nmNOSPAM@a3nm.net>.

XOR of integers from 1 to n

— updated

A funny (but probably useless) formula. The left-hand side is the XOR of integers from 1 to n, the operators are the usual C bitwise operators.

1 ^ ... ^ n = (n >> 1) & 1 ^ (n&1 ? 1 : n)

It's totally trivial to prove (just write the sequence of XOR values and look at the pattern), but, before you think about it, it's not that obvious that a closed formula exists.

Of course, this formula directly yields a closed-form expression of the XOR of integers from m to n, for any m and n: just compute the XOR of integers from 1 to m-1 and from 1 to n and XOR them together. Thanks to Nathan D. Ryan for suggesting this.

wikifirc -- filter irc.wikimedia.org on specific pages and users

— updated

I just wrote wikifirc, a small IRC bot to follow recent changes for specific Wikipedia pages or users in realtime. You can use it as an IRC watchlist, as a way to follow changes by your friends, and maybe more. A very tiny tool, but just in case someone needs this... It's intended to read and send IRC messages from stdin and stdout, so you probably need irctk, which still isn't stable yet but will be properly released Real Soon Now. More information about wikifirc is available in the README.

On identity

— updated

This post is of a more general and "philosophical" nature, which means that it might sound totally obvious if you agree with me and have already thought the same things. I am just writing this in the hope that there are people who haven't realized that they agree with this, or disagree with this, or had never even thought about this. (On a related note...)

How do you define your identity? Several things come to mind. Your name, nationality, gender, body, date of birth, nationality, the schools you attended, the places you traveled to, your friendships, your family, important experiences you had, stuff you own, important things you did, your hobbies, your tastes, your job, your beliefs, your values, your opinions...

But which ones really define your identity? Or, to be more precise, which ones would you say define yourself as a person? It would be things without which you think you wouldn't be the "same" person anymore. This idea is not mine (it has been suggested by Douglas Hofstadter, at least), it is not clear-cut at all and might not really be meaningful because it's pretty hard to tell what being the "same" person really means. Still, it gives a criterion to rate the answers to the previous question. Would you be able to imagine being you, but with a different name? Probably. Could you imagine being the same person with a different nationality? I think I could, but this is less clear. Are your hobbies really important, in that they are something that you cannot imagine not doing? Or are they just things which you happened to take some interest in, but whose nature isn't that important. (When you say that you enjoy singing or reading, do you mean this because it's important to you or just because it's just a pleasant way to spend your time?)

The question there is not about whether things would have turned out differently if you had made different choices, but about things would have been superficially different but fundamentally the same. Don't be mistaken either by the fact that citizens of a same country, or students of a same university, or people of the same gender, are usually expected to display some solidarity as compared to the outside world. If you make choice x, you tend to socialize and sympathize with x-ers, but had you made choice y, you would have sympathized with y-ers. So, solidarity with x-ers does not mean that x is an obvious part of your identity if you could imagine yourself choosing y and showing solidarity to y-ers instead.

So, now that you have a list of things that are part of your real identity, the next question is, obviously, what's so special about those? Somehow, you're probably assuming that these traits not only make you a different person, but also a better person. (You might argue that what you perceive as your identity includes sad and painful experiences, but my point is that you're probably thinking that they made you wiser.) So, how do you justify that? Think about, say, the important decision of choosing to go to university x instead of university y. Had you chosen y instead, you would have missed all those important defining experiences X that you had and all your important friendships and interactions with other x students. However, you would have had other experiences Y, which you have no way of knowing. Your belief that set X is fundamentally different and somehow more defining that unknown set Y is totally intuitive and not falsifiable!

This should annoy you. Once again, if you're like me, you tend to dismiss as hopelessly stupid the people who, being born in country x and not really knowing anything else, are proud of their x citizenship, and are x nationalists, convinced of the universal superiority of x. Of course x might be actually superior, but by the Copernican principle, it's quite unlikely that you should happen to be born precisely there. It's better to avoid this kind of arbitrary belief in the superiority of something, yet the above reasoning puts us in the same uncomfortable situation. What can we do?

The tempting thing to do is something argued by Paul Graham in Keep Your Identity Small. Those things that you consider as part of your identity make you poorer. If you say you are fundamentally x and couldn't be anything else, then you will never be able to take deliberate steps to try y out. Conversely, if you say that you happen to be x but could have been y, it also means that you leave open the possibility of becoming y in the future. That's what Graham says, but the reasoning above gives us another reason to keep our identity small: because it's hard to justify why you put something there. At least, we could try to limit the damage and derive our important choices and stances from a few basic abstract principles. The hope, after all, is to find out who you are really, ie. the axioms of your identity, and not their observable consequences.

This leads to pretty weird questions. If you're into computers, is it part of your identity to try to understand your computer? Or understand computers? Or understand the general mechanisms of information processing and retrieval? Is this musical instrument part of your identity, or the general notion of music playing? or performance art? or just art? Is it a given piece of music, or a music genre, or rather some precise point of that piece of music? And then, of course, comes the risk of trying to hide behind the abstract (ie. "I don't love football, I love the concept of people competing against one another to move a certain object according to certain rules"), and the question of whether all this reasoning really has universal value anyway. You might think of bullfighting as a convoluted and not very kind way to slaughter bulls, but someone who love bullfighting might have abstract explanations of why it has universal symbolics, which must mean a lot to them, but would not convince anyone else. Is there really a connection between those things that define you that you could explain in a convincing way to other people?

The worse is that you may feel the existence of a connection, but not be able to explicit it. Your identity, reduced to basic principles, is the key to explain what your various interests, hobbies, experiences and achievements have in common, and you may have that tantalizing impression that this unexplainable fascination you have for the Korean writing system, or overtone singing, or Bach fugues, is somehow related to the rest, but you couldn't explain how. And this is but a feeling, and there is no way to make sure that this isn't just wishful thinking, or a posteriori justification of those various quirks that you irrationally argue are essential parts of your identity.

Then, of course, along with the risk of being unable to make sense of all this mess, there is the risk that you might be missing the most important things--that you may have beliefs which are so deep that you don't even notice them anymore. This is quite harmful too, but how could you know?

And then there are all the practical difficulties with keeping your identity small. Someone who considers x as part of their core identity will tend to sympathize with other die-hard x-ers more than people who think that they just happen to be x, so this is not the best way to be close to people. Furthermore, if having a lot of fundamental guiding traits reduces the area of what you can do, it can also orient you and help you, whereas if you try to exclude as little as possible, you have more options to consider and more choices to make. (I am interested in everything, I can do everything, where do I start?) Last, you might become less willing to engage in a large project x, lest the fact of having investing time in x makes you irreversibly a member of x, preventing you from switching sides to y if you feel like it.

This last point, actually, is another think to keep in mind: since your life will only last that long, those unimportant choices you made just become part of your identity with time as you get less and less time to prove to others and to yourself that you could have lived differently. Some things, like your name or date of birth, are a given and will not tend to define you more with time, but others do. You can say, "I lived 20 years in the USA, but I do not consider that part of my identity because I could have lived elsewhere and because I could perfectly imagine moving elsewhere if I wanted to; I just happened to live the last 20 years in the USA." But imagine that same sentence with 60 instead of 20. No matter what, in that case, the bulk of your life was spent in the USA; it's much harder to toss it away as something that just happened but wasn't important.