\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#118: Enhance [file attributes] and [file copy] on Mac OS X \& BSD}
\date{November 01, 2002}
\author{Daniel A. Steffen}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#118}&\textbf{Enhance [file attributes] and [file copy] on Mac OS X \& BSD}\\\hline
Author&

Daniel A. Steffen \tipmail{das}{users.sourceforge.net}
\\
Created&Friday, \(\text{1}^{\text{st}}\) November 2002\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.7 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes a set of changes to \textit{[file attributes]} and \textit{[file copy]} to make them function better on MacOS X and other BSD-Unix systems.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Summary of Proposed Changes}

This TIP proposes five sets of changes to \textit{[file attributes]} and \textit{[file copy]}:
\begin{enumerate}
\item{}
Add support for the Macintosh and Windows specific flag \textit{[file attributes -readonly]} to Unixes that support the \textit{chflags()} API, to allow the \textit{user immutable} flag to be accessed \& modified from Tcl.
\setcounter{enumi}{1}
\item{}
Add support for the Macintosh specific flags \textit{[file attributes -readonly -creator -type -hidden]} to Mac OS X via the POSIX level API \textit{getattrlist()}.
\setcounter{enumi}{2}
\item{}
Add a new flag \textit{[file attributes -rsrclength]} to Mac OS X and Mac OS 9 that gives the length of the resource fork of a file; 0 is the only value that this attribute can be set to, which strips the resource fork off a file.
\setcounter{enumi}{3}
\item{}
Change \textit{[file attributes]} to return the list of attributes that can be retrieved without error for a given file, instead of aborting the whole command when any error occurs.
\setcounter{enumi}{4}
\item{}
Enhance \textit{[file copy]} on Mac OS X (more precisely, the native file-system \textit{Tcl\_FSCopyFile}) to copy finder attributes (i.e. \textit{-readonly -creator -type -hidden}) and resource forks transparently.
\end{enumerate}

\section{Rationale}

There is currently no way to access and modify HFS file-system metadata from Tcl on Mac OS X whereas Tcl on Mac OS 9 (or Classic) on the same Macintosh has that capability. Worse, \textit{[file copy]} (and potentially even \textit{[file rename]} if it results in a copy) on Mac OS X can be a destructive operation at present if it operates on a file that has essential data in its resource fork or its HFS metadata. This again in contrast to the same operations in Tcl on Mac OS 9 where this information is preserved. This TIP seeks to rectify these asymmetries in order to better hide such file-system related platform specificities from the scripter.

\section{Details}

Additional information \& examples:
\begin{enumerate}
\item{}
Unix versions that support \textit{chflags()} include BSD \tiprangle{}= 4.4 and Darwin/Mac OS X (where \textit{user immutable} is the flag corresponding to the file locked state on the HFS file-system, which is what \textit{[file attributes -readonly]} controls on Mac OS 9).
\setcounter{enumi}{1}
\item{}
The use of \textit{getattrlist()} does not require linking with Carbon and thus allows access to HFS file-system metadata from Tcl on pure open-source Darwin systems (which is something no other scripting language can claim at present).
\setcounter{enumi}{2}
\item{}
The new attribute \textit{-rsrclength} is useful to check whether a file has a resource fork and to calculate total file size on Mac OS 9 and X (note that \textit{[file size]} returns the size of the data fork only). Stripping a file's resource fork (by setting \textit{-rsrclength} to 0) is a common operation on Mac OS when dealing with files that are destined for other platforms. This is a feature that has been requested several times and given that it ties in well with the implementation of the other new attributes it comes at essentially no additional cost.
\begingroup\small\begin{verbatim}
% file attributes test
-group admin -owner steffen -permissions 00644 -readonly 0 -creator Doug -type RSRC -hidden 0 -rsrclength 314
% file attributes test -rsrclength 5
setting nonzero rsrclength not supported
% file attributes test -rsrclength 0
% file attributes test
-group admin -owner steffen -permissions 00644 -readonly 0 -creator Doug -type RSRC -hidden 0 -rsrclength 0
\end{verbatim}
\endgroup
\setcounter{enumi}{3}
\item{}
On Mac OS X, trying to retrieve the new attributes \textit{-creator -type -rsrclength} fails on non-regular files \& directories (and on any file located on a non-HFS file-system that doesn't support \textit{getattrlist()}). Returning only the list of attributes that are valid seems like much more sensible behaviour in this case than failing with an error and not returning anything. In the case where no valid attributes can be retrieved at all, the error returned by the last attempt is passed upstream, to preserve existing error handling. This proposed change in behaviour of \textit{[file attributes]} seems necessary to allow the command to continue to work in a consistent way on all inputs and on all platforms; it should not impact existing code since for current attributes, failure to retrieve any one attribute is equivalent to failure to retrieve all attributes.
\begingroup\small\begin{verbatim}
% close [open test w]
% file attributes test
-group admin -owner steffen -permissions 00644 -readonly 0 -creator {} -type {} -hidden 0 -rsrclength 0
% file delete test
% file mkdir test
% file attributes test
-group admin -owner steffen -permissions 040755 -readonly 0 -hidden 0
% file delete test
\end{verbatim}
\endgroup
\setcounter{enumi}{4}
\item{}
Unlike the Finder and other HFS aware tools on Mac OS X, Tcl currently ignores HFS metadata and the resource fork, which will undoubtedly surprise scripters unpleasantly. \textit{[file copy]} should hide such platform specificities and copy a file in the same way as the Finder:
\begingroup\small\begin{verbatim}
% file attributes test
-group admin -owner steffen -permissions 00644 -readonly 0 -creator Doug -type RSRC -hidden 0 -rsrclength 314
% file copy test test1
% file attributes test1
-group admin -owner steffen -permissions 00644 -readonly 0 -creator Doug -type RSRC -hidden 0 -rsrclength 314
% file delete test1
\end{verbatim}
\endgroup
\end{enumerate}

\section{Comments}

Additional implementation details:
\begin{itemize}
\item{}
To support the new attributes \textit{-creator -type}, routines to convert from numerical OSTypes (\textit{u\_int32\_t}) to the usual four character human readable format have been adapted from \textit{mac/tclMacResource.c}; the new versions accept/return strings of length 0-4 unlike the originals that only dealt with length 4. This is important because creator/type 0 (i.e. \textit{-creator \{\} -type \{\}}) is common on Mac OS X. The Mac OS 9 implementation of the OSType string representation code has been modified accordingly by adding support for strings of length 0-4 and missing \textit{UtfToExternal/ExternalToUtf} conversions.
\item{}
\textit{macRoman} is the encoding used for the string representation of OSTypes, for consistency with Tcl on Mac OS 9 as well as with common Mac OS X tools such as Resorcerer \& SuperGetInfo that all use \textit{macRoman} to display creator/type codes; this encoding is probably what most people would expect. It's unfortunate that this means that use of \textit{[file attributes]} on Darwin/Mac OS X will cause the non-builtin \textit{macRoman} encoding to load. ASCII-only OSTypes will still work properly if \textit{macRoman} is not available, fallback to \textit{latin1} in that case could also be added if deemed necessary. However, the Tk Aqua port already relies on \textit{macRoman} being present so in the most common usage pattern \textit{macRoman} should be present and loaded anyway.
\item{}
The Mac OS 9 implementation of \textit{[file attributes -creator -type]} currently returns the bogus 'Fldr' type \& creator for directories, this has been changed to return an error for consistency with the Mac OS X implementation.
\item{}
Most of the implementation of the new Mac OS X specific features has been added added at the end of \textit{unix/tclUnixFCmd.c}, it might be cleaner to move this code to a separate file \textit{macosx/tclMacOSXFCmd.c}, but that would require several routines in both \textit{unix/tclUnixFCmd.c} and \textit{mac/tclMacOSXFCmd.c} to be made non-static. It's unclear whether this is an acceptable change just for the sake of code separation/cleanliness.
\end{itemize}

\section{Reference Implementation}

SourceForge patch \#626360 [\url{http://sourceforge.net/tracker/index.php?func=detail&aid=626360&group_id=10894&atid=310894}] implements this TIP as a patch to the current HEAD.

The patch has been tested on the SF compile-farm on hosts:
\begin{description}
\item[[Alpha] Linux 2.4 (Debian 3.0)]
where \textit{chflags()} and \textit{getattrlist()} are not available and no ill effects ensue (i.e. no new tests fail).
\item[[x86] FreeBSD (4.7-RC)]
where \textit{chflags()} is available and \textit{[file attributes -readonly]} can successfully be interrogated (but not set due to permission issues at SourceForge). No new tests fail.
\end{description}

as well as on Mac OS 9, X 10.1.5 and X 10.2.1, where all the new functionality is available and no new tests fail.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
