<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Tue May 21 04:10:06 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='118'>
<header><title>Enhance [file attributes] and [file copy] on Mac OS X &amp; BSD</title><author address="mailto:das@users.sourceforge.net">Daniel A. Steffen</author><status type='project' state='final' tclversion="8.5" vote='after'>$Revision: 1.7 $</status><history></history><created day='1' month='nov' year='2002' /></header>
<abstract>This TIP proposes a set of changes to <emph style="italic">[file attributes]</emph> and <emph style="italic">[file copy]</emph> to make them function better on MacOS X and other BSD-Unix systems.</abstract>
<body><section title="Summary of Proposed Changes">
<para>This TIP proposes five sets of changes to <emph style="italic">[file attributes]</emph> and <emph style="italic">[file copy]</emph>:</para>
<enumerate><item.e index='1'><para>Add support for the Macintosh and Windows specific flag <emph style="italic">[file attributes -readonly]</emph> to Unixes that support the <emph style="italic">chflags()</emph> API, to allow the <emph style="italic">user immutable</emph> flag to be accessed &amp; modified from Tcl.</para></item.e><item.e index='2'><para>Add support for the Macintosh specific flags <emph style="italic">[file attributes -readonly -creator -type -hidden]</emph> to Mac OS X via the POSIX level API <emph style="italic">getattrlist()</emph>.</para></item.e><item.e index='3'><para>Add a new flag <emph style="italic">[file attributes -rsrclength]</emph> 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.</para></item.e><item.e index='4'><para>Change <emph style="italic">[file attributes]</emph> 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.</para></item.e><item.e index='5'><para>Enhance <emph style="italic">[file copy]</emph> on Mac OS X (more precisely, the native file-system <emph style="italic">Tcl_FSCopyFile</emph>) to copy finder attributes (i.e. <emph style="italic">-readonly -creator -type -hidden</emph>) and resource forks transparently.</para></item.e></enumerate>
</section>
<section title="Rationale">
<para>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, <emph style="italic">[file copy]</emph> (and potentially even <emph style="italic">[file rename]</emph> 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.</para>
</section>
<section title="Details">
<para>Additional information &amp; examples:</para>
<enumerate><item.e index='1'><para>Unix versions that support <emph style="italic">chflags()</emph> include BSD &gt;= 4.4 and Darwin/Mac OS X (where <emph style="italic">user immutable</emph> is the flag corresponding to the file locked state on the HFS file-system, which is what <emph style="italic">[file attributes -readonly]</emph> controls on Mac OS 9).</para></item.e><item.e index='2'><para>The use of <emph style="italic">getattrlist()</emph> 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).</para></item.e><item.e index='3'><para>The new attribute <emph style="italic">-rsrclength</emph> 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 <emph style="italic">[file size]</emph> returns the size of the data fork only). Stripping a file&apos;s resource fork (by setting <emph style="italic">-rsrclength</emph> 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.</para><verbatim><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdA==</vline><vline encoding='base64'>LWdyb3VwIGFkbWluIC1vd25lciBzdGVmZmVuIC1wZXJtaXNzaW9ucyAwMDY0NCAtcmVhZG9ubHkgMCAtY3JlYXRvciBEb3VnIC10eXBlIFJTUkMgLWhpZGRlbiAwIC1yc3JjbGVuZ3RoIDMxNA==</vline><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdCAtcnNyY2xlbmd0aCA1</vline><vline encoding='base64'>c2V0dGluZyBub256ZXJvIHJzcmNsZW5ndGggbm90IHN1cHBvcnRlZA==</vline><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdCAtcnNyY2xlbmd0aCAw</vline><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdA==</vline><vline encoding='base64'>LWdyb3VwIGFkbWluIC1vd25lciBzdGVmZmVuIC1wZXJtaXNzaW9ucyAwMDY0NCAtcmVhZG9ubHkgMCAtY3JlYXRvciBEb3VnIC10eXBlIFJTUkMgLWhpZGRlbiAwIC1yc3JjbGVuZ3RoIDA=</vline></verbatim></item.e><item.e index='4'><para>On Mac OS X, trying to retrieve the new attributes <emph style="italic">-creator -type -rsrclength</emph> fails on non-regular files &amp; directories (and on any file located on a non-HFS file-system that doesn&apos;t support <emph style="italic">getattrlist()</emph>). 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 <emph style="italic">[file attributes]</emph> 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.</para><verbatim><vline encoding='base64'>JSBjbG9zZSBbb3BlbiB0ZXN0IHdd</vline><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdA==</vline><vline encoding='base64'>LWdyb3VwIGFkbWluIC1vd25lciBzdGVmZmVuIC1wZXJtaXNzaW9ucyAwMDY0NCAtcmVhZG9ubHkgMCAtY3JlYXRvciB7fSAtdHlwZSB7fSAtaGlkZGVuIDAgLXJzcmNsZW5ndGggMA==</vline><vline encoding='base64'>JSBmaWxlIGRlbGV0ZSB0ZXN0</vline><vline encoding='base64'>JSBmaWxlIG1rZGlyIHRlc3Q=</vline><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdA==</vline><vline encoding='base64'>LWdyb3VwIGFkbWluIC1vd25lciBzdGVmZmVuIC1wZXJtaXNzaW9ucyAwNDA3NTUgLXJlYWRvbmx5IDAgLWhpZGRlbiAw</vline><vline encoding='base64'>JSBmaWxlIGRlbGV0ZSB0ZXN0</vline></verbatim></item.e><item.e index='5'><para>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. <emph style="italic">[file copy]</emph> should hide such platform specificities and copy a file in the same way as the Finder:</para><verbatim><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdA==</vline><vline encoding='base64'>LWdyb3VwIGFkbWluIC1vd25lciBzdGVmZmVuIC1wZXJtaXNzaW9ucyAwMDY0NCAtcmVhZG9ubHkgMCAtY3JlYXRvciBEb3VnIC10eXBlIFJTUkMgLWhpZGRlbiAwIC1yc3JjbGVuZ3RoIDMxNA==</vline><vline encoding='base64'>JSBmaWxlIGNvcHkgdGVzdCB0ZXN0MQ==</vline><vline encoding='base64'>JSBmaWxlIGF0dHJpYnV0ZXMgdGVzdDE=</vline><vline encoding='base64'>LWdyb3VwIGFkbWluIC1vd25lciBzdGVmZmVuIC1wZXJtaXNzaW9ucyAwMDY0NCAtcmVhZG9ubHkgMCAtY3JlYXRvciBEb3VnIC10eXBlIFJTUkMgLWhpZGRlbiAwIC1yc3JjbGVuZ3RoIDMxNA==</vline><vline encoding='base64'>JSBmaWxlIGRlbGV0ZSB0ZXN0MQ==</vline></verbatim></item.e></enumerate>
</section>
<section title="Comments">
<para>Additional implementation details:</para>
<itemize><item.i><para>To support the new attributes <emph style="italic">-creator -type</emph>, routines to convert from numerical OSTypes (<emph style="italic">u_int32_t</emph>) to the usual four character human readable format have been adapted from <emph style="italic">mac/tclMacResource.c</emph>; 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. <emph style="italic">-creator {} -type {}</emph>) 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 <emph style="italic">UtfToExternal/ExternalToUtf</emph> conversions.</para></item.i><item.i><para><emph style="italic">macRoman</emph> 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 &amp; SuperGetInfo that all use <emph style="italic">macRoman</emph> to display creator/type codes; this encoding is probably what most people would expect. It&apos;s unfortunate that this means that use of <emph style="italic">[file attributes]</emph> on Darwin/Mac OS X will cause the non-builtin <emph style="italic">macRoman</emph> encoding to load. ASCII-only OSTypes will still work properly if <emph style="italic">macRoman</emph> is not available, fallback to <emph style="italic">latin1</emph> in that case could also be added if deemed necessary. However, the Tk Aqua port already relies on <emph style="italic">macRoman</emph> being present so in the most common usage pattern <emph style="italic">macRoman</emph> should be present and loaded anyway.</para></item.i><item.i><para>The Mac OS 9 implementation of <emph style="italic">[file attributes -creator -type]</emph> currently returns the bogus &apos;Fldr&apos; type &amp; creator for directories, this has been changed to return an error for consistency with the Mac OS X implementation.</para></item.i><item.i><para>Most of the implementation of the new Mac OS X specific features has been added added at the end of <emph style="italic">unix/tclUnixFCmd.c</emph>, it might be cleaner to move this code to a separate file <emph style="italic">macosx/tclMacOSXFCmd.c</emph>, but that would require several routines in both <emph style="italic">unix/tclUnixFCmd.c</emph> and <emph style="italic">mac/tclMacOSXFCmd.c</emph> to be made non-static. It&apos;s unclear whether this is an acceptable change just for the sake of code separation/cleanliness.</para></item.i></itemize>
</section>
<section title="Reference Implementation">
<para>SourceForge patch #626360 [<url ref="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=626360&amp;group_id=10894&amp;atid=310894"/>] implements this TIP as a patch to the current HEAD.</para>
<para>The patch has been tested on the SF compile-farm on hosts:</para>
<describe><item.d name='[Alpha] Linux 2.4 (Debian 3.0)'><para>where <emph style="italic">chflags()</emph> and <emph style="italic">getattrlist()</emph> are not available and no ill effects ensue (i.e. no new tests fail).</para></item.d><item.d name='[x86] FreeBSD (4.7-RC)'><para>where <emph style="italic">chflags()</emph> is available and <emph style="italic">[file attributes -readonly]</emph> can successfully be interrogated (but not set due to permission issues at SourceForge). No new tests fail.</para></item.d></describe>
<para>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.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>
