combox

split and encrypted files between online file storage providers
git clone git://git.ricketyspace.net/combox.git
Log | Files | Refs

commit c04f52356574387c133635025fe8f65127d79427
parent 3458ba2623dcc065d6e75125668233bc4fada149
Author: Siddharth Ravikumar <sravik@bgsu.edu>
Date:   Thu, 15 Jan 2015 17:00:53 -0500

combox/file.py: wrote new utility functions.

- rm_nodedir
- move_nodedir
- rm_shards
- move_shards

Diffstat:
combox/file.py | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 99 insertions(+), 0 deletions(-)

diff --git a/combox/file.py b/combox/file.py @@ -58,6 +58,105 @@ def mk_nodedir(directory, config): print e, "Something wrong. report bug to sravik@bgsu.edu" +def rm_nodedir(directory, config): + """ + Removes directory `directory' inside the nodes. + + config: a dictionary containing configuration info about combox. + """ + + nodes = get_nodedirs(config) + + rel_path = relative_path(directory, config) + + for node in nodes: + dir_path = path.join(node, rel_path) + try: + os.rmdir(dir_path) + except OSError, e: + print e, "Something wrong. report bug to sravik@bgsu.edu" + + +def move_nodedir(src, dest, config): + """ + Moves directory `directory' inside the nodes from old to new location. + + src: old path to the directory + dest: new path to the directory + config: a dictionary containing configuration info about combox. + """ + + nodes = get_nodedirs(config) + + src_rel_path = relative_path(src, config) + dest_rel_path = relative_path(dest, config) + + for node in nodes: + src_dir_path = path.join(node, src_rel_path) + dest_dir_path = path.join(node, dest_rel_path) + try: + os.rename(src_dir_path, dest_dir_path) + except OSError, e: + print e, "Something wrong. report bug to sravik@bgsu.edu" + + +def rm_shards(fpath, config): + """ + Removes the file shards of `fpath' in the node directories. + + fpath: is the path to a file in the combox directory. + + config: a dictionary containing configuration info about combox. + """ + + nodes = get_nodedirs(config) + + rel_path = relative_path(fpath, config) + + for node in nodes: + shard_glob = "%s.shard*" % path.join(node, rel_path) + # there's always only one shard in each node directory. So, + # the glob() will alawys return a list of size 1. + shard = glob(shard_glob)[0] + try: + os.remove(shard) + except OSError, e: + print e, "Something wrong. report bug to sravik@bgsu.edu" + + +def move_shards(src, dest, config): + """Move the shards in node directories. + + This function is used when a file is moved to different location + inside the combox directory. It moves the shards to the + corresponding location in the node directories. + + src: old path to the file that was moved. + dest: new path to the file that was moved. + config: a dictionary containing configuration info about combox. + """ + + nodes = get_nodedirs(config) + + src_rel_path = relative_path(src, config) + dest_rel_path = relative_path(dest, config) + + for node in nodes: + src_shard_glob = "%s.shard*" % path.join(node, src_rel_path) + # there's always only one shard in each node directory. So, + # the glob() will alawys return a list of size 1. + src_shard = glob(src_shard_glob)[0] + + # get shard number + shard_no = src_shard.partition('.shard')[2] + dest_shard = "%s.shard%s" % (path.join(node, dest_rel_path), + shard_no) + try: + os.rename(src_shard, dest_shard) + except OSError, e: + print e, "Something wrong. report bug to sravik@bgsu.edu" + + def purge_dir(p): """ Purge everything under the given directory `p'.