I badly needed some helper scripts for my ipfs node to manage the MFS, so having written them I thought I’d post them here. I’ll update this post as I go with new functions or bug fixes
#! /bin/bash
function ipfs_add_folder() {
local file
local fname
local folder
local file_hash
local existing
local isExisting=false
file="${1#'./'}"
folder="${2}"
fname=$(basename "${file}")
while read -r existing
do
if [[ "${fname}" = "${existing}" ]]; then
isExisting=true
fi
done < <(ipfs files ls "/${folder}")
if [[ ${isExisting} = true ]]; then
echo "Skiping existing file ${fname}"
return
fi
echo "Adding ${fname}"
file_hash=$(ipfs add --pin=false -Q "${file}")
ipfs files cp "/ipfs/${file_hash}" "/${folder}/${fname}"
}
function ipfs_find_add_folder() {
local patern
local folder
patern="${1}"
folder="${2}"
find . -type f -name "${patern}" -exec bash -c "ipfs_add_folder '{}' '${folder}'" \;
}
#### ipfs dht provide -r (Appears) to hang when trying to process
#### very large folder trees,
#### so here's a simple way to provide each file separately with a
#### short timeout to prevent getting stuck
function ipfs_provide_folder() {
local folder
local fname
local listing
local file_hash
local size
local len
folder="${1}"
echo "Recursing into ${folder}"
while read -r -a listing
do
size=${listing[-1]}
len=$((${#listing[@]} -2 ))
fname=$(basename "${listing[*]:0:$len}")
if [[ "${size}" -eq 0 ]]; then
ipfs_provide_folder "${folder}/${fname}"
else
file_hash=${listing[-2]}
echo "Providing ${fname} - ${file_hash}"
ipfs dht provide --timeout 900s "${file_hash}"
fi
done < <(ipfs files ls -l "/${folder}")
}

