Enhanced CTorrent and CTCS

From NAS-Central Buffalo - The Linkstation Wiki
Revision as of 19:31, 27 January 2007 by (Talk) (Automatic downloading of torrents)

Jump to: navigation, search

Enhanced CTorrent is a BitTorrent client implemented in C++ to be lightweight and quick and is the continuation of its parent CTorrent.

Enhanced CTorrent has an average CPU load of 4.2% at a download rate of 100KB/s and an upload rate of 100KB/s on an ARM equiped Linkstation Pro.

LSPro (arm9) - OpenLink

Building from scratch

$ gunzip ctorrent-1.3.4-dnh2.2.tar.gz
$ tar xvf ctorrent-1.3.4-dnh2.2.tar.gz
  • If you are compiling in Scratchbox make sure to have libstdc++.so installed in for example /usr/lib. Either copy it from your Linkstation directly to the Scratchbox environment by using scp or install it from anywhere else.
  • Enhanced CTorrent has very little dependencies therefore you should be able to do a simple:
$ ./configure
$ make

Pre-built binary

A pre built ARM binary ctorrent is available from LSPro Temporary Upload Folder

CTorrent Control Server (CTCS)

To control and manage your torrents from an external computer you can make use of the Web interface provided by the CTorrent Control Server (CTCS). This is a Perl script which needs no further configuration.

Typical Setup

  • Make sure the binary ctorrent is available via your PATH environment varaiable.
  • Start the CTorrent Control Server and publish the server at port 2780:
$ ctcs -p 2780 > /dev/null &
  • Start ctorrent as a backgorund process downloading torrent.torrent subscribing at the locally running CTCS server at port 2780 and ignoring any console output since we will be controlling it via a web-interface.
$ ctorrent -S localhost:2780 torrent.torrent > /dev/null &
  • On the remote system open up a webbrowser and surf to:

For further information on how to start, stop and manage ctorrent please read the CTorrent user's guide.

Automatic downloading of torrents

It is very desireable to start a torrent more userfriendly at a remote machine than to manually:

  1. Download the torrent on the remote machine to a Linkstation share.
  2. Do a remote login to the Linkstation.
  3. Start ctorrent with the correct command line parameters.

To semi-automate these tasks, the Bash script below will check a specified directory on your Linkstation for any newly added .torrent files and will spawn a new ctorrent process for it when the torrent was not already activated previously. This way you can start downloading a torrent simply by copying a .torrent file to a certain shared directory on your Linkstation.


#                       Activate torrents Bash shell script                   #
# Author: Sander van Woensel
# Description: Starts ctorrent for any newly added .torrent file found in the
#              path specified on the command line.

#                                  Constants                                  #


LOG_FILE="/var/log/$(basename $0 .sh).log"

ERROR_NO_CTORRENT="Cannot execute ctorrent. Make sure ctorrent is available via one of the\n       paths in your PATH environment variable."
ERROR_INVALID_TORRENT_DIR="Invalid .torrent file directory specified."

#                                  Functions                                  #

# Prints script usage.
function usage
printf "Usage: $0 <path>

Spawns a ctorrent process for any new .torrent file in <path> not already
actiavted previously.

     path: Directory to check for newly added .torrent files.


function log
	echo "[$(date -R)] $@" >> "$LOG_FILE"

function error
	printf "Error: $*\n\n"

# Waits until file ($1) does not change in size (and thus is fully copied).
function wait_till_file_uploaded
	while CURRENT_SIZE=$(ls -ld "$1" | awk '{print $5}') && [ $CURRENT_SIZE != $SIZE ]; do
		sleep 2

# Spawn ctorrent for every new torrent file found.
function activate
	# TODO: retrieve field $11 till $NF (last field). How to do this in awk? 
	PROCESSING=$(ps -df | awk '$8 ~ /ctorrent/ {print $11}')
	# Change directory to the .torrent directory. This removes the need
	# to concatenate any paths when executing ls.
	cd "$1" 
	# Get all torrent files.
	# Take into account file names with spaces (read command reads one full line at the time).
	ls -d1 *.torrent *.tor 2>/dev/null | while read FILE; do
		if [ -f "$FILE" ] && [ -r "$FILE" ] ; then
			# If file is NOT found in the currently processing file list.
			if [ -z $(echo "$PROCESSING" | grep "$FILE") ]; then
				wait_till_file_uploaded "$FILE"
				log "ctorrent -S localhost:$CTORRENT_PORT \"$FILE\" &"
				ctorrent -S localhost:$CTORRENT_PORT "$FILE" > /dev/null 2>> "$LOG_FILE" &

#                                    Main                                     #

# Did the user requested the help text?
if [ $# != 1 ] || [ "$1" == -h ] || [ "$1" == --help ]; then
	exit $EXIT_OK

# Check if ctorrent can be executed.
if [ -z $(which ctorrent) ]; then

# Check if the specified directory is available and can be read.
if [ $# != 1 ] || [ ! -d "$1" ] || [ ! -r "$1" ]; then

# All tests succeeded; spawn ctorrent.
activate "$1"


  • Login your Linkstation as root.
  • Copy the above text in a new file on your linkstation and save it as /etc/cron/cron.d/activate_torrents.sh
  • Make sure the script is executable for the user root:
# chmod u+x /etc/cron/cron.d/activate_torrents.sh
  • Add the script to the cron table so it gets executed at a specified interval. First open up the cron table in the default editor (mostly vi).
# crontab -e
  • Add the following line to the end of the crontab file:
*/05 * * /etc/cron/cron.d/activate_torrents.sh /mnt/disk1/<share>/torrents
  • This will check for new .torrent files in the directory /mnt/disk1/<share>/torrents every 5 minutes and will start a new ctorrent session when necessary.
  • On your computer copy a .torrent file to the share: /mnt/disk1/<share>/torrents, wait 5 minutes and check if ctorrent started downloading by executing a :
# ps -df | grep ctorrent
  • Or you could check CTCS by browsing to: http://<linkstation-ip>:2780.
  • Log out of your linkstation. You sould now be able to start any torrent by simply copying it to a share on your Linkstation.

Related articles