NSDocument and NSFileWrapping saving bad names

135 views Asked by At

My NSDocument subclass using a bundle document type which uses fileWrapperOfType:error: to write data to disk. This works perfectly but eventually it will start to save files with garbled names like "2_#$!@%!#_info.plist" instead of overwriting the proper file.

I don't know what changes to cause the problem but I think it's permissions related because Finder is not letting my write inside the bundle even though "stat" returns "drwxr-xr-x" which is writeable by the owner.

Did NSFileWrapper or NSDocument mess up the permissions or something? I'm new to this API but here is the code I'm using below (in Objective Pascal) which is hopefully readable by all.

function TScriptDocument.fileWrapperOfType_error (typeName: NSString; outError: NSErrorPtr): NSFileWrapper;
var
    fileWrappers: NSDictionary;
    propertiesFileWrapper: NSFileWrapper;
    data: NSData;
    propertiesName: NSString;
begin
    if documentFileWrapper = nil then
        documentFileWrapper := NSFileWrapper.alloc.initDirectoryWithFileWrappers(nil);      

    fileWrappers := documentFileWrapper.fileWrappers;
    propertiesName := NSSTR('info.plist');
    propertiesFileWrapper := fileWrappers.objectForKey(propertiesName);
    if propertiesFileWrapper <> nil then
        documentFileWrapper.removeFileWrapper(propertiesFileWrapper);
    data := NSPropertyListSerialization.dataWithPropertyList_format_options_error(script.GetProperties, NSPropertyListXMLFormat_v1_0, 0, nil);
    if data <> nil then
        documentFileWrapper.addRegularFileWithContents_preferredFileName(data, propertiesName);

    result := documentFileWrapper;
end;
1

There are 1 answers

0
GenericPtr On

I found that retaining the file wrapper from initDirectoryWithFileWrappers across calls to fileWrapperOfType_error was causing the problem. I now initialize a new instance of NSFileWrapper every time the document is saved using initWithURL_options_error or initDirectoryWithFileWrappers.

However, it's still a problem that I can't modify the documents bundle contents in the Finder even though I chmod'd the directory 0777. Maybe some Finder-specific restriction it places on these files.