Skip to content

Commit

Permalink
0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
creaktive committed Jan 6, 2010
0 parents commit 9fa1dcc
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.bz2
*.deb
myTunes
10 changes: 10 additions & 0 deletions make.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

TOOLCHAIN="/var/toolchain/sys30"
BINARY="myTunes/usr/libexec/myTunes"

gcc -I"$TOOLCHAIN/usr/include" -L"$TOOLCHAIN/usr/lib" -lsqlite3 -O3 -o $BINARY myTunes.c
strip $BINARY
ldid -S $BINARY

dpkg-deb -b myTunes
207 changes: 207 additions & 0 deletions myTunes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
gcc -I/var/toolchain/sys30/usr/include -L/var/toolchain/sys30/usr/lib -lsqlite3 -O3 -o myTunes myTunes.c; strip myTunes; ldid -S myTunes
*/

#include <err.h>
#include <dirent.h>
#include <fcntl.h>
#include <signal.h>
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include <sys/event.h>
#include <sys/stat.h>

#define MEDIA "/private/var/mobile/Media"
#define LIBFILE MEDIA "/iTunes_Control/iTunes/iTunes Library.itlp/Library.itdb"
#define LOCFILE MEDIA "/iTunes_Control/iTunes/iTunes Library.itlp/Locations.itdb"
#define ITFILES MEDIA "/iTunes_Control/Music"
#define MYTUNES MEDIA "/myTunes"

int kq, pid;

int wait(char *file) {
int fd;

do {
usleep(100000);
fd = open(file, O_RDONLY);
} while (fd == -1);

return fd;
}

int mon(int fd) {
struct kevent ke;
int i = fd;

if (i != -1)
close(i);

fd = wait(LIBFILE);

/*
if (i != -1)
kill(pid, SIGUSR1);
*/
kill(pid, SIGUSR1);

EV_SET(&ke, fd, EVFILT_VNODE, EV_ADD, NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, NULL);
if (kevent(kq, &ke, 1, NULL, 0, NULL) == -1)
err(1, "kevent2");

return fd;
}

void postpone(int sig) {
alarm(5);
}

static int callback(void *empty, int argc, char **argv, char **col) {
char *p;
char title[256];
unsigned int i;
struct stat fs;

if (stat(argv[0], &fs) == -1)
return 0;

memset(title, '\0', sizeof(title));
for (i = 0, p = argv[1]; (i < sizeof(title) - 1) && (*p != '\0'); i++, p++)
switch (*p) {
case ':':
case '*':
case '?':
title[i] = '_';
break;
default:
title[i] = *p;
}

//printf("%s\t%s\n", argv[0], title);
link(argv[0], title);
return 0;
}

int query(sqlite3 *db, char *sql, void *cb) {
int i;
char *err;

for (i = 0; i < 5; i++)
if (sqlite3_exec(db, sql, cb, 0, &err) == SQLITE_OK)
return 1;
else {
warn("sqlite3_exec %s", err);
sqlite3_free(err);
usleep(100000);
}

return 0;
}

void clean() {
DIR *pd;
struct dirent *pde;

pd = opendir(MYTUNES);
if (pd == NULL)
return;

for (pde = readdir(pd); pde != NULL; pde = readdir(pd))
if (pde->d_type == DT_REG)
unlink(pde->d_name);

closedir(pd);
}

void fix(int sig) {
int ok;
sqlite3 *db;

alarm(0);

signal(SIGUSR1, SIG_IGN);
signal(SIGALRM, SIG_IGN);

close(wait(LOCFILE));
close(wait(LIBFILE));

mkdir(MYTUNES, S_IRWXU | S_IRWXG | S_IRWXO);
chdir(MYTUNES);
clean();

if (sqlite3_open(LIBFILE, &db))
warn("sqlite3_open %s", sqlite3_errmsg(db));
else if (
query(db, "ATTACH '" LOCFILE "' AS loc", NULL) &&
query(db, "SELECT '" ITFILES "/' || location, title || SUBSTR(location, 9) FROM item LEFT JOIN loc.location ON (pid = item_pid) ORDER BY location ASC", callback) &&
query(db, "DETACH DATABASE loc", NULL)
)
ok = 1;

sqlite3_close(db);

signal(SIGUSR1, postpone);
signal(SIGALRM, fix);
}

int main() {
int fd, null;
struct kevent ke;

if ((null = open("/dev/null", O_RDWR)) == -1)
err(1, "null");

switch (fork()) {
case -1:
err(1, "fork1");
case 0:
setsid();
switch (fork()) {
case -1:
err(1, "fork2");
case 0:
// chdir("/");
umask(0);

// close standart IO
close(0);
close(1);
close(2);

// open'em as /dev/null
dup2(null, 0);
dup2(null, 1);
dup2(null, 2);

break;
default:
return 0;
}
break;
default:
return 0;
}

pid = fork();
if (pid == -1)
err(1, "fork3");
else if (pid == 0) {
signal(SIGUSR1, postpone);
signal(SIGALRM, fix);

while (1)
pause();
} else {
kq = kqueue();
if (kq == -1)
err(1, "kqueue");

for (fd = mon(-1); 1; fd = mon(fd)) {
memset(&ke, '\0', sizeof(ke));

if (kevent(kq, NULL, 0, &ke, 1, NULL) == -1)
err(1, "kevent1");
}
}
}
23 changes: 23 additions & 0 deletions myTunes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh
MEDIA="/private/var/mobile/Media"
LIBFILE="$MEDIA/iTunes_Control/iTunes/iTunes Library.itlp/Library.itdb"
LOCFILE="$MEDIA/iTunes_Control/iTunes/iTunes Library.itlp/Locations.itdb"
ITFILES="$MEDIA/iTunes_Control/Music"
MYTUNES="$MEDIA/myTunes"

rm -Rf "$MYTUNES"
mkdir -p "$MYTUNES"
cd "$MYTUNES"

(
sqlite3 -list "$LIBFILE" << SQLITE_END | tr ':?*' '___'
ATTACH '$LOCFILE' AS loc;
SELECT '$ITFILES/' || location, title || SUBSTR(location, 9) FROM item LEFT JOIN loc.location ON (pid = item_pid) ORDER BY location ASC;
DETACH DATABASE loc;
SQLITE_END
) | while read line
do
file=$(echo $line | cut -d '|' -f1)
name=$(echo $line | cut -d '|' -f2)
$(ln "$file" "$name")
done
12 changes: 12 additions & 0 deletions myTunes/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Package: net.iWatcher.myTunes
Name: myTunes
Version: 0.1
Architecture: iphoneos-arm
Homepage: http://iwatcher.net/
Author: Stanislaw Pusep <creaktive@gmail.com>
Maintainer: Stanislaw Pusep <creaktive@gmail.com>
Section: System
Description: Gives you back the access to the media files stored on your device through the 'Media/myTunes' folder accessible via SCP, iPhoneBrowser or iFile internal server.
After installation, browse '/private/var/mobile/Media/myTunes' for you tracks.
Please note that the purpose of this application is backup; changes made through iTunes will be reflected, however uploading a track into 'myTunes' folder won't
make it accessible through iPod app (neither deleting will remove it from the playlist)!
11 changes: 11 additions & 0 deletions myTunes/DEBIAN/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

declare -a cydia
cydia=($CYDIA)

if [[ $1 == remove ]]; then
rm -Rf "/private/var/mobile/Media/myTunes"
else
echo "postrm - skip restore files"
fi
#end script
Binary file not shown.

0 comments on commit 9fa1dcc

Please sign in to comment.