On Mon, 3 Jun 2024, Jeremy Drake via Cygwin wrote:

> I'm not necessarily sure that the subject is clear enough, so I want to be
> explicit that I'm talking about files (or I guess potentially directories,
> though I've never seen that) generated by the `try_to_bin` function in
> winsup/cygwin/syscalls.cc.  Specifically, you can generate one with this
> simple bash reproducer:
>
> cp /usr/bin/sleep.exe .
> ./sleep 1000 &
> rm -f ./sleep.exe
> kill %1
> fg
>
> My questions are (starting at the beginning with what I'm trying to
> accomplish, and wandering off into the weeds of the various things I've
> tried to do that).
>
> 1) is there some mechanism in Cygwin that I'm not seeing to clean up these
> files?  So far I've confirmed that their creation does not result in the
> recycle bin icon turning from 'empty' to 'full' on the desktop, and that
> emptying the recycle bin there (when it doesn't think it's already empty)
> does not remove them.
>
> 2) assuming there is not, I want to make a script using only things
> present in a "base system" to clean them up.

Now that the mount points are escaped and contain the Windows volume roots
starting with 3.6, here's my script.  It uses bash/find/gawk.  I'd welcome
any ideas on improving it, I've only just started messing with gawk.  (I'd
started out doing grep/cut then xargs printf "%b\0" to unescape, but with
gawk I can do that all in one program).

It's in
https://gist.github.com/jeremyd2019/4984ff0fa1f6fd8c99d7b8b244c52088

#!/bin/bash -ex

IFS=. read -r cygmajor cygminor _ < <(uname -r)

declare -a roots

if [ "${cygmajor}" -gt 3 -o "${cygmajor}" -eq 3 -a "${cygminor}" -ge 6 ]; then
  # as of cygwin 3.6, volume roots are parsable from /proc/mounts
  # (noumount is the option that indicates a cygdrive mount)
  readarray -t -d $'\0' roots < <(
    LC_CTYPE=C LC_COLLATE=C gawk -v 'ORS=\0' -l ordchr '
      $4 ~ /\<noumount\>/ {
        split($2,a,/\\0?[0-7]{1,3}/,seps)
        ret=a[1]
        for (x=2; x<=length(a); x++)
          ret = ret chr(strtonum("0" substr(seps[x-1],2))) a[x]
        print ret
      }' /proc/mounts)
else
  # before that, just punt and look at the root of the drive / is mounted on
  root="$(cygpath -w /)"
  roots=("$(cygpath -u "${root:0:2}")")
  unset root
fi

declare -a trash
readarray -t -d $'\0' trash < <(
  LC_CTYPE=C LC_COLLATE=C \
  find "${roots[@]}" -maxdepth 1 -iname '$Recycle.Bin' -print0 |
  LC_CTYPE=C LC_COLLATE=C \
  find -files0-from - -maxdepth 2 \( -name $'.\uDC6D\uDC73\uDC79\uDC73*' -o \
                                     -name $'.\uF76D\uF773\uF779\uF773*' -o \
                                     -name '.msys*' -o \
                                     -name $'.\uDC63\uDC79\uDC67*' -o \
                                     -name $'.\uF763\uF779\uF767*' -o \
                                     -name '.cyg*' \) -print0)
if (( ${#trash[@]} )); then
  ls -la "${trash[@]}"
  read -r -p "Remove? (y/N) "

  if [[ "${REPLY^^}" == "Y" ]]; then
    rm -f "${trash[@]}"
  fi
fi

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to