Home > Articles > Home & Office Computing > Mac OS X

This chapter is from the book

4.7 Interacting with the Filesystem

Most nontrivial programs need to interact with the filesystem in some way. On most UNIX-like systems, including OS X, the filesystem is the only persistent storage facility provided. User defaults is just a high-level interface to a small part of the filesystem, providing access to specific files via a dictionary-like interface.

How you want to interact with the filesystem depends a lot on the task at hand. Cocoa provides a number of facilities exposing files as UNIX-style streams of bytes, or as structured data of some kind. Which you should use depends on your requirements.

4.7.1 Bundles

Bundles are a very important part of OS X. They were used on NeXT systems and have gradually replaced resource forks from earlier versions of Mac OS. The big advantage is not needing any special filesystem support.

Applications on OS X are bundles and can have other resources as well as the code. On NeXT systems, application bundles were used to store different versions of the executable for different platforms; you could have a single .app on an NFS share and run it on OPENSTEP, Solaris, or any other platform that it supported. This legacy is still found in OS X today. The binary is in the Contents/MacOS directory inside the bundle. In theory, you could add binaries for other platforms, although this is not currently supported by the Apple tools.

Prior to the release (and naming) of OS X, the in-development successor to Classic MacOS was called Rhapsody. Three "boxes" were announced by Apple. Two eventually became part of OS X. Blue box was the virtualized compatibility layer for MacOS that was called Classic on early versions of OS X and is not present on Intel Macs. The yellow box was the OpenStep environment that was later rebranded Cocoa. The final box, the red box, never made it to a shipping product and was a Windows environment for OS X similar to WINE. There was also a planned Windows version of the yellow box, based on the OPENSTEP Enterprise (OSE) product from NeXT, including Project Builder and Interface Builder and allowing Cocoa applications to be developed for Windows.

It seems likely that Apple still maintains descendants of the Windows version of the yellow box internally and uses them for porting applications like Safari to Windows, although Apple does not use the bundle architecture for these applications. Although the red box was not shipped, it was seen as a possible future product for long enough for OS X to retain the ability to run application bundles with executables in entirely different formats.

OS X, like OPENSTEP, uses the Mach-O binary format, which supports different format executables in the same binary files (sharing constants and data when the endian is the same). This is more efficient than having independent binaries for each version and allows Intel and PowerPC, 32-bit and 64-bit executables to be included in the same file. NeXT called these fat binaries, while Apple opted for the more politically correct universal binaries.

Because applications are bundles, every application has at least one bundle that it will want to load resources from. In a very simple application this happens automatically. The main nib for the application will be loaded when it starts and connected to the application delegate and any other objects.

Other resources can be loaded from the application bundle with the NSBundle class. In general, you will have one instance of this class for each bundle you want to interact with. You can get the application bundle with

[NSBundle mainBundle];

Be careful when doing this. At some point in the future you may decide that your class is very useful and that you want to reuse it. When you do this, you will move it and a framework—another kind of bundle containing a loadable library, headers, and resources—along with any resources it might want to load. When you get the main bundle from your class, you will get the application bundle for the application that linked against the framework, rather than the framework bundle. If you are getting a bundle to load resources that are included with the class then this is not what you want. Instead, you should use

[NSBundle bundleForClass: [self class]];

This is relatively slow, so it is best done in the +initialize method for the class and cached in a file-static variable, like this:

static NSBundle *frameworkBundle;
+ (void) initialize
{
   frameworkBundle = [[NSBundle bundleForClass: self] retain];
}

In real code, you would probably want to wrap this in a check to ensure that it was only being called on the correct class, as shown in Chapter 3. Because this is a class method, it only needs to pass self, rather than [self class] as the parameter. You can also use +bundleWithIdentifier, which is generally faster. This loads the bundle that has the identifier provided as the argument. The bundle identifier is set in the bundle's property list by the CFBundleIdentifier key.

Once you have a bundle, you can load resources from it. This is a two-step process. The first is to find the path of the resource, using a method like this:

- (NSString*)pathForResource: (NSString*)name
                      ofType: (NSString*)extension
                 inDirectory: (NSString*)subpath
             forLocalization: (NSString*)localizationName

There are two wrapper versions of this method where the last parameters are filled in with default values. The simplest form just has the first two and finds resources using the user's preferred localization in the top-level resource directory in the bundle.

If you want to load all of the resources of a specific type in a bundle, there is a form that returns an array instead of a string:

- (NSArray*)pathsForResourcesOfType: (NSString*)extension
                        inDirectory: (NSString*)subpath

This, and the version that specifies a localization, finds all of the resources of a specific type, for example, all of the png files in a theme directory in the Resources directory in the bundle.

In addition to resources, you can load code from bundles, too. Listing 4.16 shows a simple framework loader. Because frameworks are just another kind of bundle with a well-known layout, the standard bundle loading code can be used to load them.

This example is taken from Étoilé's LangaugeKit and is used to allow scripts loaded and compiled by a running program to specify frameworks that they depend upon, without requiring the program that loads them to link against every possible framework that a script might want.

This example shows a number of Cocoa features. The first is the file manager, which we will look at in the next section. This is used in line 24 to test whether the framework exists at a given path. If it does, then NSBundle is used on lines 27 and 28 to load the code in the framework.

Listing 4.16. A simple framework loader. [from: examples/Loader/simpleLoader.m]

 7| @implementation SimpleLoader
 8| + (BOOL) loadFramework: (NSString*)framework
 9| {
10|     NSFileManager *fm = [NSFileManager defaultManager];
11|     NSArray *dirs =
12|         NSSearchPathForDirectoriesInDomains(
13|             NSLibraryDirectory,
14|             NSAllDomainsMask,
15|             YES);
16|     FOREACH(dirs, dir, NSString*)
17|     {
18|         NSString *f =
19|             [[[dir stringByAppendingPathComponent: @"Frameworks"]
20|                 stringByAppendingPathComponent: framework]
21|                     stringByAppendingPathExtension: @"framework"];
22|         // Check that the framework exists and is a directory.
23|         BOOL isDir = NO;
24|         if ([fm fileExistsAtPath: f isDirectory: &isDir]
25|             && isDir)
26|         {
27|             NSBundle *bundle = [NSBundle bundleWithPath: f];
28|             if ([bundle load])
29|             {
30|                 NSLog(@"Loaded_bundle_%@", f);
31|                 return YES;
32|             }
33|         }
34|     }
35|     return NO;
36| }
37| @end

The function on line 11 is one of the most useful, and most overlooked, parts of Cocoa, since it allows you to avoid hard-coding paths in a lot of instances. Line 19 shows some of NSString's path manipulation code. This is used to assemble the correct path by appending the Frameworks directory, then the framework name as path components, and then the .framework extension. This could be done with -stringWithFormat: for OS X, but doing it this way means that it will continue to work if you try to move your code to a different platform with different path formats.

4.7.2 Workspace and File Management

Cocoa provides two ways of interacting with the filesystem, NSFileManager and NSWorkspace. The latter is part of AppKit and provides a higher-level interface. The NSWorkspace class does file operations in the background and posts a notification when they are done, while NSFileManager works synchronously. Both classes are singletons; you will only ever have (at most) one instance for each in an application.

We saw an example of one of the things you can do with a file manager in Listing 4.16. This used the -fileExistsAtPath:isDirectory: method, to see if a file existed. The second argument to this is a pointer to a BOOL, which is set to YES if the file is found and is a directory.

Most other common file manipulation operations are supported by the file manager, such as copying, moving, and linking files and directories. It can also enumerate the contents of folders and compare files. Most of NSFileManager's functionality is exposed by a single method in NSWorkspace:

- (BOOL)performFileOperation: (NSString*)operation
                      source: (NSString*)source
                 destination: (NSString*)destination
                       files: (NSArray*)files
                         tag: (NSInteger)tag

This takes a source and destination directory as arguments and an array of files. It performs move, copy, link, destroy, or recycle operations and sets the value of the integer pointed to by tag to indicate whether the operation succeeded.

Most of NSWorkspace's functionality deals with higher-level operations on files. While NSFileManager is for dealing with files as UNIX-style streams of bytes, NSWorkspace is for dealing with files as a user-level abstraction, representing documents or applications. Methods like openFile: are examples of this. This method opens a specified file with the default application and is used to implement the command-line open tool.

The low-level file manager methods are very easy to use. Listing 4.17 shows a simple tool for copying a file. This uses the user defaults system to read command-line arguments and then uses the file manager to copy the specified file.

Note that this example code does not check whether the input file exists or that the output is a valid destination. The file manager will call a delegate method in case of an error, but we did not set a handler on line 12, and so this will not allow error checking either. Implementing the handler is not required, it simply allows you to track the progress of the operation and to decide whether to proceed in case of an error. The return value from this method is a boolean indicating whether the copy succeeded. You can run this simple tool like this:

$ gcc -framework Foundation FileCopy.m -o FileCopy
$ ./FileCopy -in FileCopy -out CopyOfFileCopy
$ ls
CopyOfFileCopy FileCopy       FileCopy.m

Note that the file manager automatically resolved relative paths. These are treated as being relative to whatever the file manager returns from -currentDirectoryPath. You can alter the working directory for a running program by sending the file manager a -changeCurrentDirectoryPath: message. The working directory is much more important for command-line tools than it is for graphical applications. A command-line tool inherits its current working directory from the shell. The concept of a current directory does not make sense for an application invoked via the Finder or from the Dock.

Listing 4.17. A simple tool for copying files. [from: examples/FileCopy/FileCopy.m]

 1| #import <Foundation/Foundation.h>
 2|
 3| int main(void)
 4| {
 5|     [NSAutoreleasePool new];
 6|     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 7|     NSString *source = [defaults stringForKey: @"in"];
 8|     NSString *destination = [defaults stringForKey: @"out"];
 9|     NSFileManager *fm = [NSFileManager defaultManager];
10|     [fm copyPath: source
11|           toPath: destination
12|          handler: nil];
13|     return 0;
14| }

Starting with 10.5, Apple began using the uniform type identifier (UTI) system for identifying file types. A UTI is a hierarchical arrangement of types. NSWorkspace is used to map between file extensions and UTIs.

4.7.3 Working with Paths

When working with filesystem paths, there are a number of helpful methods provided by NSString. These allow you to decompose paths into components and create various individual components without worrying about the kind of path that is represented.

On UNIX platforms, a tilde (~) is commonly used as a shorthand for the user's home directory. You can get this explicitly by calling NSHomeDirectory() but often users will enter strings containing this shorthand and expect it to work. If you select Go to Folder in the Finder's Go menu, then enter "~/Documents" then it will open a new window showing the Documents folder in your home directory.

The NSString class provides a convenient method for strings that may contain a tilde. If you send the string a -stringByExpandingTildeInPath message, then you will get back a new string containing the absolute path, without a tilde. Although less useful, it is also possible to go the other way by sending a full path a -stringByAbbreviatingWithTildeInPath message. If the path points to something inside the user's home directory, then it will be collapsed to only use a tilde character for this part of the path.

When interacting with the filesystem, you very often need to decompose a path into three parts: the file name, the file extension, and the path of the directory containing the file. You can do all of these from NSString, like this:

NSString *fullPath = @"/tmp/folder/file.extension";
// ext = @"extension";
NSString *ext = [fullPath pathExtension];
// file = @"file";
NSString *file = [[fullPath lastPathComponent]
    stringByDeletingPathExtension];
// dir = @"/tmp/folder";
NSString *dir = [fullPath stringByDeletingLastPathComponent];

There are also methods for constructing a path from individual parts, including appending components and setting the extension. Before you start writing code for parsing path strings yourself, make sure that NSString doesn't already have methods for doing what you need.

4.7.4 File Access

While NSFileManager lets you interact with the filesystem, and NSWorkspace lets you open files in other applications, neither provides a means of accessing the contents of files.

There is nothing stopping you from using the C library and POSIX functions for doing this, but they are not very convenient. Cocoa includes a wrapper around them in the form of the NSFileHandle class. This wraps a file handle as would be returned by open(). Four singleton instances exist, representing the C standard input, output and error streams, and a placeholder that discards all data written to it. Additional file handles can be created for reading, writing, or updating, with named constructors.

You can use the NSFileHandle class anywhere you have a C file handle using this initializer:

- (id)initWithFileDescriptor: (int)fileDescriptor
              closeOnDealloc: (BOOL)flag

The file descriptor is any C file descriptor, for example, the kind returned by open() or socket(). Whether the resulting object supports reading or writing depends on the underlying file descriptor. If you set flag to YES, then you can use this as a simple way of tracking how many parts of your program are using the file and ensuring that it is closed at the correct point. As long as you then only use the file descriptor with the object, it will stay open as long as some parts of your program are using it and close itself when it is no longer required.

If all you want to do is read data from a file, NSData objects can be created from a file using this method:

+ (id)dataWithContentsOfFile: (NSString*)path
                     options: (NSUInteger)mask
                       error: (NSError**)errorPtr

This creates a new NSData object from the contents of the file pointed to by path and sets the errorPtr to an NSError instance if it fails. The mask parameter allows two options to be set: NSMappedRead and NSUncachedRead. The first uses mmap() instead of file reading operations. As discussed earlier, this is a good idea if you know that the file is not going to be modified, for example, for read-only resources in an application bundle. If the system is low on memory, it can very cheaply free mapped data and load it back from the original file, while data read in will have to be written out to the swap file, even if they have not been modified. The second option allows the data to bypass the operating system's cache. If you know that you are just going to read the data once and then discard it, then it can improve performance. Otherwise it will use less RAM, but require more disk accesses.

NSData can also be used to write data back to a file. For small file output, using an NSMutableData to construct the file in memory and then using the writeToFile:atomically: or writeToURL:atomically: methods to output it is very simple. The second parameter to each of these is a BOOL, which, if set to YES, will write the data first to a temporary file and then rename the temporary file, ensuring on-disk consistency.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020