Maybe this helps, if not for you then for others: I do something similar when cleaning out accounts. I use shell scripts, feeding a list of user through a loop, with the inner part of the loop being:
gam user "${LEAVER}" print filelist corpora user anyowner fields id,name,owners.emailAddress,mimetype,shortcutdetails.targetmimetype,shortcutdetails.targetid,permissions.id,permissions.emailaddress,permissions.role showpermissionslast
This outputs a large list of files that particular user either owns or has access to, with all permissions in CSV format on one line per file.
Next, I use a few routines to parse those lines, so that per file, I can account for shortcuts and deal with each permission seperately like this:
[gam command as per above] | while read LINE; do
# grab and store header line
[[ -z ${ORIG_HEADERS} ]] && ORIG_HEADERS="$(echo $LINE | tr '.' '_')" && continue ## The tr is required because bash variable names cannot contain .s
# parse next line according to headers
DATA="${LINE}"
HEADERS="${ORIG_HEADERS}"
eval $(
until [[ -z $HEADERS ]]; do
echo "$(Chomp piece "$HEADERS")=\"$(Chomp piece "$DATA")\""
HEADERS="$(Chomp remainder "$HEADERS")"
DATA="$(Chomp remainder "$DATA")"
done
)
done
The loop inside the eval subprocess outputs a set of variable assignments, effectively turning every CSV header into a variable with (for this particular file) the value on the data line as a value assigned. From then on, you can do all the logic you want using those variables. Very usefully, if there are more than one permissions, gam outputs that number in a separate column, which is thus stored as the variable 'permissions'.
The Chomp function is this:
Chomp () {
ACTION="${1}"; shift
STRING="${1}"
case "${ACTION}" in
piece) echo "${STRING%%,*}" ;;
remainder)
if [[ ${STRING} =~ ',' ]]; then
echo "${STRING#*,}"
else
echo ""
fi
;;
*) echo "" ;;
esac
}
Further in the loop, this is how the permissions are processed:
if [[ -n "${permissions}" ]]; then
CTR=$((permissions - 1)) ## need to offset because the permissions are labeled from 0 up instead of from 1 up.
until [[ ${CTR} -eq -1 ]]; do
P_USER="permissions_${CTR}_emailAddress" ## the user that has the permission
P_ID="permissions_${CTR}_id" ## the permission (specific access right)
#######
####### Your logic goes here about what to do with what kind of permission/user combo
####### For instance skip/ignore ('continue') or save the data in some form by echo'ing it.
#######
CTR=$((CTR - 1))
done
The performance profile of this method is one fairly slow gam call per user. I tend to parallelise these jobs BUT you need to build in some safety measures to avoid overloading your machine. (I have an elaborate set of such measures but that is too detailed for this answer).
Maybe it is doable to have gam itself do the parallelisation by batching the very first call for file details. You need some way to separate out the output per file, so that you get clean sets of CSV headers + data for all files for a user. gam provides this but i admit that I have personally never gotten my head around those command options. gam could run 20 or 25 sessions in parallel, saving its data to clean and neat sets of headers+data. Then it is merely a matter of having a script tearing through that output to see what needs to be done with what.
Final note: with the exception of gam itself and a single tr command, all of this logic is bash internals. That means very little process creation and piping data. In Bash scripts, that easily causes 100 fold speed increases.
hth -- ymmv - Peter