APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version


Copying Mac Resource Forks with Perl

by Karl Young

The company I'm working for now uses Director to write PC/Mac applications. We're working on a web install process, and have the problem that if we move a Mac executable to our Linux web server, we lose the resource fork unless we use Stuffit first.

We found a Director Xtra that lets us open the resource and data forks separately and save them as separate files to put on the install server, then recombine them in the Mac install client.

I then wanted to write a Perl script to split all the Mac files up, so I didn't have to use Director, which I hate working with. I used the following.


  my($infoFile) = $f . '.info';
  my($rsrcFile) = $f . '.rsrc';
  my($rsrcFork) = $f . '/rsrc';
  my($dataFile) = $f . '.data';
  open(INFO, ">$infoFile") or die "Can't open $infoFile: $@";
  printInfo(getInfo($f));
  system("ditto",, "--norsrc", $f, $dataFile);
  system("cp", $rsrcFork, $rsrcFile);
  system("rm", $f);
 

getInfo uses the MacOSX::File module to get the type, flags, and creator, and printInfo just writes them in a format the install client can read.

 my($finfo) = MacOSX::File::Info->get($f);
 $info{"type"} = $finfo->type;
 $info{"Flags"} = sprintf("%016b",$finfo->fdFlags);
 $info{"creator"} = $finfo->creator;
 return(\%info);
 

The entire script:


#!/usr/bin/perl

use strict;
use MacOSX::File::Info;

sub getInfo($);
sub printInfo($;$);
sub walkDir($);

my($dir) = $ARGV[0];

walkDir($dir);

sub getInfo($)  {
  my($f) = $_[0];
  my(%info);
  my($finfo) = MacOSX::File::Info->get($f);
  $info{"type"} = $finfo->type;
  $info{"Flags"} = sprintf("%016b",$finfo->fdFlags);
  $info{"creator"} = $finfo->creator;
  return(\%info);
}

sub printInfo($;$)  {
  my($info, $fh) = @_[0,1];
  my($comma) = "";
  my($outString) = '[';
  foreach my $k (sort(keys(%$info)))  {
     $outString .= "$comma#$k: " . '"' . $$info{$k} . '"';
     $comma = ", ";
  }
  print INFO $outString, "]\n";
}


#------------------------------------------------------
# Walk Dir
#------------------------------------------------------
sub walkDir($) {
  my ($dir) = $_[0];
  my(@files,$nam,$lvldir);

  opendir STDIR,$dir;
  @files = readdir STDIR;
  closedir STDIR;

  foreach $nam (@files)
  {
     next if (substr($nam,0,1) eq '.');

     $lvldir = "$dir/$nam";

     if (-d $lvldir)  {
        walkDir($lvldir);

     } else {
        my($infoFile) = $lvldir . '.info';
        my($rsrcFile) = $lvldir . '.rsrc';
        my($rsrcFork) = $lvldir . '/rsrc';
        my($dataFile) = $lvldir . '.data';
        open(INFO, ">$infoFile") or die "Can't open $infoFile: $@";
        printInfo(getInfo($lvldir));
        system("ditto",, "--norsrc", $lvldir, $dataFile);
        system("cp", $rsrcFork, $rsrcFile);
        system("rm", $lvldir);
     }

  }
}
 


If this page was useful to you, please help others find it:  





1 comment




More Articles by Karl Young



Click here to add your comments





Sat Jan 7 13:42:04 2006: 1484   TonyLawrence

gravatar
One thing to watch here: on UFS file systems, Mac doesn't use filename/rsrc





Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

g_face.jpg

This post tagged:

       - Disks/Filesystems
       - MacOSX
       - Perl
       - Scripting


















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals