Monday, April 22, 2013

NSFileManager linkItemAtPath example ios


linkItemAtPath :toPath:error:

Creates a hard link between the items at the specified paths.
- (BOOL)linkItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPatherror:(NSError **)error
Parameters
srcPath
The path that specifies the item you wish to link to. The value in this parameter must not be nil.
dstPath
The path that identifies the location where the link will be created. The value in this parameter must not be nil.
error
On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
Return Value
YES if the hard link was created or NO if an error occurred. This method also returnsNO if a file, directory, or link already exists at dstPath.
Discussion of [NSFileManager linkItemAtPath]
Use this method to create hard links between files in the current file system. IfsrcPath is a directory, this method creates a new directory at dstPath and then creates hard links for the items in that directory. If srcPath is (or contains) a symbolic link, the symbolic link is copied to the new location and not converted to a hard link.
Prior to linking each item, the file manager asks its delegate if it should actually create the link. It does this by calling thefileManager:shouldLinkItemAtURL:toURL: method; if that method is not implemented it calls the fileManager:shouldLinkItemAtPath:toPath: method instead. [NSFileManager linkItemAtPath] If the delegate method returns YES, or if the delegate does not implement the appropriate methods, the file manager creates the hard link. If there is an error moving one out of several items, the file manager may also call the delegate’sfileManager:shouldProceedAfterError:linkingItemAtURL:toURL: orfileManager:shouldProceedAfterError:linkingItemAtPath:toPath:method to determine how to proceed.
Example of [NSFileManager linkItemAtPath]
- (BOOL)linkItemAtPath:(NSString *)path
                toPath:(NSString *)otherPath
                 error:(NSError **)error {
  LOG_OP(@"[0x%x] linkItemAtPath: %@", [NSThread currentThread], path); 

  NSString* p_src = [rootPath_ stringByAppendingString:path];
  NSString* p_dst = [rootPath_ stringByAppendingString:otherPath];
  return [[NSFileManager defaultManager] linkItemAtPath :p_src
                                                 toPath:p_dst
                                                  error:error];  
}