Mac file copy function

Hello!

Urgently needed Mac File Copy function with only two parameters: source and destination POSIX-pathname.

OSErr MacSimpleFileCopy( const char * srcName, const char * dstName );

OR two CFURLRef parameters:

OSErr MacSimpleFileCopy( CFURLRef srcName, CFURLRef dstName );

Required: Mac OS X/Carbon, copying data & resource forks
Unneeded: network support, Mac OS Classic support

I'm newbie in mac world, and completely confused with FSSpec, FSRef, dirID etc.
LVL 1
Korsar_13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bachra04Commented:
This is very simple copy file function that you can use in Mac (or Windows):

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int copyFile (const char *inPath, const char * outPath) {
  int inF, ouF;
  char line[512];
  int bytes;


  if((inF = open( inPath , O_RDONLY)) == -1) {
    perror("open");
    exit(-1);
  }

  if((ouF = open(outPath , O_WRONLY | O_CREAT)) == -1) {
    perror("open");
    exit(-1);
  }

  while((bytes = read(inF, line, sizeof(line))) > 0)
    write(ouF, line, bytes);

  close(inF);
  close(ouF);
}

0
Korsar_13Author Commented:
unfortunately, this routine don't copy FileType/FileCreator tags and resource fork

carbon-dev@lists.apple.com suggest me use FSCopyOject, but it's parameters is very strange to me:

/* This routine acts as the top level of the copy engine.  */
OSErr FSCopyObject(  const FSRef        *source,
          const FSRef        *destDir,
           ItemCount        maxLevels,
           FSCatalogInfoBitmap    whichInfo,
           DupeAction        dupeAction,
           const HFSUniStr255    *newObjectName,  /* can be NULL */
          Boolean          wantFSSpec,
          Boolean          wantName,
          CopyObjectFilterProcPtr filterProcPtr,  /* can be NULL */
          void          *yourDataPtr,  /* can be NULL */
          FSRef          *newObjectRef,  /* can be NULL */
          FSSpec          *newObjectSpec)  /* can be NULL */

0
Alex CuryloCommented:
Well, there should be only two tricky bits, if you're doing this for a simple file. First off, you need to change your source path into an FSRef. Do that like this.

   FSRef srcRef;
   if (!CFURLGetFSRef (srcURLPath, &srcRef))
     return bdNamErr;

Next, you need an FSRef to the directory you're copying to; get that the same way.

If you're not changing the name, then you're done, call FSCopyObject with those two FSRefs and everything else NULL.

If you are changing the name, then you need one more parameter, the HFSUniStr255 of the new name you want. Put the new name into a CFStringRef from whatever source format you have it in, then use this to get an HFSUniStr255 out of that.

HFSUniStr255 name;
name.length = CFStringGetBytes( input, CFRangeMake(0, MIN(CFStringGetLength(input), 255)),
        kCFStringEncodingUnicode, 0, false, (UInt8 *)(name.unicode), 255, NULL );
if ( output->length == 0 )
    ;// handle error
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.