• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is cloning the same as copying in APFS?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is cloning the same as copying in APFS?


  • Subject: Re: Is cloning the same as copying in APFS?
  • From: Rick Mann <email@hidden>
  • Date: Tue, 27 Jun 2017 14:17:55 -0700

Thanks for that, Charles!

I wasn't sure because of the existence of the clonefile() call in
<sys/clonefile.h>, implying that operation was different from copyfile(), but
looking more closely at the sample code, I see that you can call copyfile()
with COPYFILE_CLONE, which seems like it will clone if possible and copy if not.

But there didn't seem to be equivalent calls in NSFileManager, hence my
question.

> On Jun 27, 2017, at 13:55 , Charles Srstka <email@hidden> wrote:
>
>> On Jun 27, 2017, at 12:28 AM, Jens Alfke <email@hidden> wrote:
>>
>>> On Jun 26, 2017, at 7:38 PM, Rick Mann <email@hidden> wrote:
>>>
>>> But there's actually a POSIX "clone" API, and so I wonder if a copy is
>>> different from a clone.
>>
>> The low level file copying API that I’m aware of is <copyfiles.h>, which is
>> Apple specific. It has options for doing copy-on-write.
>> I don’t know if NSFileManager’s copy method clones files. I would assume
>> that it does, on APFS.
>
> Well, I mean, you could always *try* it ;-)
>
> import Foundation
>
> func unclonedSize(of url: URL) throws -> off_t {
>       var list = attrlist(bitmapcount: UInt16(ATTR_BIT_MAP_COUNT),
>               reserved: 0,
>               commonattr: 0,
>               volattr: 0,
>               dirattr: 0,
>               fileattr: 0,
>               forkattr: attrgroup_t(ATTR_CMNEXT_PRIVATESIZE))
>
>       let bufsize = 4 + MemoryLayout<off_t>.size
>       let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: bufsize)
>       defer { buf.deallocate(capacity: bufsize) }
>
>       let err = url.withUnsafeFileSystemRepresentation { getattrlist($0,
> &list, buf, bufsize, UInt32(FSOPT_ATTR_CMN_EXTENDED)) }
>
>       if err != 0 { throw POSIXError.Code(rawValue: errno).map {
> POSIXError($0) } ?? CocoaError(.fileReadUnknown) }
>
>       let attrsize = buf.withMemoryRebound(to: UInt32.self, capacity: 1) {
> $0.pointee }
>
>       if attrsize < bufsize { throw CocoaError(.fileReadUnknown) }
>
>       var unclonedSize: off_t = 0
>
>       _ = withUnsafeMutableBytes(of: &unclonedSize) { memcpy($0.baseAddress!,
> buf + 4, MemoryLayout<off_t>.size) }
>
>       return unclonedSize
> }
>
> do {
>       let sourceURL = URL(fileURLWithPath: "/tmp/tempfile1")
>       let destURL = URL(fileURLWithPath: "/tmp/tempfile2")
>
>       let sortaRandomData = Data(bytes: (0..<100 * 1024).map {
> UInt8(extendingOrTruncating: $0) })
>
>       try sortaRandomData.write(to: sourceURL)
>
>       print("Uncloned size of source file: \(try unclonedSize(of:
> sourceURL))")
>
>       try FileManager.default.copyItem(at: sourceURL, to: destURL)
>
>       print("Uncloned size of dest file: \(try unclonedSize(of: destURL))")
> } catch {
>       print("Error occurred: \(error.localizedDescription)")
> }
>
> - - - - - - - - -
>
> outputs:
>
> Uncloned size of source file: 102400
> Uncloned size of dest file: 0
>
> So, it seems quite clear that (NS)FileManager does, in fact, clone the files
> you tell it to copy. :-)
>
> Charles
>


--
Rick Mann
email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Is cloning the same as copying in APFS?
      • From: Charles Srstka <email@hidden>
References: 
 >Is cloning the same as copying in APFS? (From: Rick Mann <email@hidden>)
 >Re: Is cloning the same as copying in APFS? (From: Sandor Szatmari <email@hidden>)
 >Re: Is cloning the same as copying in APFS? (From: Rick Mann <email@hidden>)
 >Re: Is cloning the same as copying in APFS? (From: Jens Alfke <email@hidden>)
 >Re: Is cloning the same as copying in APFS? (From: Charles Srstka <email@hidden>)

  • Prev by Date: Re: Is cloning the same as copying in APFS?
  • Next by Date: Re: Is cloning the same as copying in APFS?
  • Previous by thread: Re: Is cloning the same as copying in APFS?
  • Next by thread: Re: Is cloning the same as copying in APFS?
  • Index(es):
    • Date
    • Thread