from watch import FileWatcher
from rsync import *
from config import load_config, save_config
+from util import secure_path
import urllib, urllib2
import json
import os
server_url = "http://localhost:8080/"
cagibi_folder = "."
-def changesHandler(modified, added, removed):
+
+def upload_local_changes(modified, added, removed):
+ pattern = "%s/files/%s/hashes" % server_url
+
for file in modified:
- fd = open(file, "rb")
- hashes = blockchecksums(fd)
- print hashes
+ url = pattern % file
+ fd = urllib2.urlopen(url)
+ hashes = json.load(fd)
+ patchedfile = open(secure_path(cagibi_folder, file), "rb")
+ deltas = rsyncdelta(patchedfile, hashes)
+ patchedfile.close()
+
+ # Send the deltas to the server.
+ post_data = {}
+ post_data["deltas"] = json.dumps(deltas)
+ post_string = urllib.urlencode(post_data)
+ fd = urllib2.urlopen(url, post_string)
def checkout_upstream_changes():
"""Checkout changes on the server"""
local_files[file] = {}
local_files[file]["rev"] = server_files[file]["rev"]
url = "%s/files/%s/data" % (server_url, file)
- urllib.urlretrieve(url, os.path.join(cagibi_folder, file))
+ urllib.urlretrieve(url, secure_path(cagibi_folder, file))
print "Retrieved %s" % file
modified = True
if server_files[file]["rev"] > local_files[file]["rev"]:
# Get it too, but using the rsync algo
- unpatched = open(os.path.join(cagibi_folder, file).encode('ascii', 'ignore'), "rb")
+ unpatched = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "rb")
hashes = blockchecksums(unpatched)
json_hashes = json.dumps(hashes)
post_data = {}
save_to = os.tmpfile()
patchstream(unpatched, save_to, json_response)
unpatched.close()
- os.unlink(os.path.join(cagibi_folder, file))
+ os.unlink(secure_path(cagibi_folder, file))
save_to.seek(0)
- file_copy = open(os.path.join(cagibi_folder, file).encode('ascii', 'ignore'), "w+b")
+
+ # FIXME: rename instead of copying ?
+ file_copy = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "w+b")
file_copy.write(save_to.read())
file_copy.close()
save_to.close()
cagibi_folder = client_config["folder"]
fw = FileWatcher()
- fw.addHandler(changesHandler)
+ fw.addHandler(upload_local_changes)
checkout_upstream_changes()
fw.watch()
for file in os.listdir(cagibi_folder):
dir[file] = {}
- dir[file]["mtime"] = os.path.getmtime(os.path.join(cagibi_folder, file))
+ dir[file]["mtime"] = os.path.getmtime(secure_path(cagibi_folder, file))
if file in files_info:
dir[file]["rev"] = files_info[file]["rev"]
# FIXME: possible race condition
if not os.path.exists(filename) and filename not in files_info:
- fd = open(os.path.join(cagibi_folder, filename), "wb")
+ fd = open(secure_path(cagibi_folder, filename), "wb")
fd.write(contents)
fd.close()
files_info[filename] = {"rev": 1}
# FIXME: possible race condition
if os.path.exists(filename) and filename in files_info:
- os.remove(os.path.join(cagibi_folder, filename))
+ os.remove(secure_path(cagibi_folder, filename))
del files_info[filename]
save_config(files_info, filename="files.json")
return "Ok."
@get('/files/<filename>/data')
def file_data(filename):
+ """Return the contents of the file"""
return static_file(filename, root=cagibi_folder)
+@get('/files/<filename>/hashes')
+def file_hashes(filename):
+ """Return the hashes of a file"""
+ unpatched = open(secure_path(cagibi_folder, filename), "rb")
+ hashes = blockchecksums(unpatched)
+ unpatched.close()
+ return json.dumps(hashes)
+
+@post('/files/<filename>/hashes')
+def update_file_hashes(filename):
+ """Updates a file using the deltas received by a client"""
+ deltas = json.loads(request.forms.get("hashes"))
+
+ unpatched = open(secure_path(cagibi_folder, filename), "rb")
+ save_to = os.tmpfile()
+ patchstream(unpatched, save_to, json_response)
+ unpatched.close()
+ os.unlink(secure_path(cagibi_folder, filename))
+ save_to.seek(0)
+
+ # FIXME: rename instead of copying ?
+ file_copy = open(secure_path(cagibi_folder, filename).encode('ascii', 'ignore'), "w+b")
+ file_copy.write(save_to.read())
+ file_copy.close()
+ save_to.close()
+
+ files_info[filename] = {"rev": 1}
+ save_config(files_info, filename="files.json")
+
+ pass
+
@post('/files/<filename>/deltas')
def return_deltas(filename):
"""return the deltas corresponding to the file. The client must send in its request the hashes of the file"""
hashes = json.loads(request.forms.get("hashes"))
- patchedfile = open(os.path.join(cagibi_folder, filename), "rb")
+ patchedfile = open(secure_path(cagibi_folder, filename), "rb")
deltas = rsyncdelta(patchedfile, hashes)
patchedfile.close()
--- /dev/null
+import os
+
+def secure_path(folder_path, file):
+ """Make sure that a file/path is located in the folder "folder" """
+ #FIXME: make it work for directories.
+ return folder_path + os.path.sep + os.path.basename(file)