Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more.
0

Freeing up more space on mac
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Freeing up more space on mac

by aaaidan on Tue Jan 21, 2020 3:23 am

Hi, this is my first post.

I found I was running out of space on my Trinket M0 super easily, and I wanted to share a tip for Mac OS that helped save me a lot of space (possibly dozens of KB on my very modest program). This is beyond clearing out needless visible files and hidden files (e.g. dotfiles).

While adafruit have done a really good job hinting to operating systems that they shouldn't write metadata to the drive (no_log etc), that doesn't stop Mac OS copying "extended attributes" on files if you copy-paste or drag them into the drive in the Finder. (This is explained by adafruit here under "Copy Files on Mac OSX Without Creating Hidden Files".)

For me, a newcomer to CircuitPython, these attributes were included when I copied the default main.py program, and some adafruit spi library files into the lib folder. A total of 5 files (~8.5KB of code). But it wouldn't fit in the 48KB drive, even after I cleared out all the junk I could find. (And emptied the trash ;P)

After a bit of research and learning about these extended attributes, I cleared them manually on every file. I was very surprised to see how much space it freed up on the drive. I went from having 0% free to about 50% free, which is over 20kb – enough for a decent main.py.

I love the command line, but I also find it super convenient to use the Finder. So I wrote a little bash script to automate stripping these attributes on all files it can find on the drive. I run this from within the /Volumes/CIRCUITPY dir when I'm running out of space.

strip_xattr.sh
Code: Select all | TOGGLE FULL SIZE
cd /Volumes/CIRCUITPY
echo "Warning: This program will erase extended attributes on all files in" `pwd`
echo "Proceed?"
echo
read -p "Press Ctrl-C to abort, any other key to proceed..."

PRE_FREE_SPACE_KB=`df -Pk . | sed 1d | grep -v used | awk '{ print $4 "\t" }'`

find ./* | (
  while read -r FILENAME; do
    echo Cleaning "$FILENAME"
    xattr "$FILENAME" | xargs -n 1 -I attr xattr -d attr "$FILENAME"
  done;
)

POST_FREE_SPACE_KB=`df -Pk . | sed 1d | grep -v used | awk '{ print $4 "\t" }'`

echo "Done."
echo
echo "Space before: " $PRE_FREE_SPACE_KB  "kb"
echo "Space after:  " $POST_FREE_SPACE_KB "kb"

To run it, use your editor to copy this program to a file on the CIRCUITPY volume, e.g. "strip_xattr.sh". In Terminal, cd /Volumes/CIRCUITPY then ./strip_xattr.sh

I'm running Mac OS (Catalina 10.15), so perhaps it's something that Apple changed recently.

Anyway, I hope this helps someone. I also hope adafruit can find a way to cajole MacOS into omitting these extended attributes when they're copied onto the drive, although I'd be surprised if this is possible. :(

aaaidan
 
Posts: 2
Joined: Tue Jan 21, 2020 2:39 am

Re: Freeing up more space on mac

by adafruit_support_mike on Tue Jan 21, 2020 6:08 am

Interesting.. thanks for posting the script!

adafruit_support_mike
 
Posts: 60992
Joined: Thu Feb 11, 2010 2:51 pm

Re: Freeing up more space on mac

by ednl on Fri Jan 24, 2020 1:54 pm

Much easier, single command to remove all attributes of all files in a directory: xattr -c *

ednl
 
Posts: 1
Joined: Fri Jan 24, 2020 1:52 pm

Re: Freeing up more space on mac

by aaaidan on Fri Jan 24, 2020 2:20 pm

@ednl
ednl wrote:Much easier, single command to remove all attributes of all files in a directory: xattr -c *


Oh wow. Of course. That's a bit embarassing! Thanks so much for pointing it out!

Recursive is important though, so `-r` is needs to be part of the command...

Code: Select all | TOGGLE FULL SIZE
xattr -c -r .

aaaidan
 
Posts: 2
Joined: Tue Jan 21, 2020 2:39 am

Please be positive and constructive with your questions and comments.