Revision: 28931 http://sourceforge.net/p/bibdesk/svn/28931 Author: hofman Date: 2024-08-12 18:02:41 +0000 (Mon, 12 Aug 2024) Log Message: ----------- Replace files copied from SharedSupport to ApplicationSupport when they are an old revision. Check this by comparing the md5 hash of the current copied file with the hashes of old versions, which we register in a resource file. The SupportFileRevisions.plist file should be updated whenever a support file is changed.
Modified Paths: -------------- trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj trunk/bibdesk/NSFileManager_BDSKExtensions.m trunk/bibdesk/NSURL_BDSKExtensions.h trunk/bibdesk/NSURL_BDSKExtensions.m Added Paths: ----------- trunk/bibdesk/SupportFileRevisions.plist Modified: trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj =================================================================== --- trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj 2024-08-09 22:07:30 UTC (rev 28930) +++ trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj 2024-08-12 18:02:41 UTC (rev 28931) @@ -473,6 +473,7 @@ CE4241450D0EAEFE00F824E7 /* BDSKEditorTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = CE4241430D0EAEFE00F824E7 /* BDSKEditorTextField.m */; }; CE424A450D0F123500F824E7 /* BDSKCompletionServerProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = CE3A0BD50B1634D500233208 /* BDSKCompletionServerProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; CE4385E90BB81D0500A56987 /* BDSKSearchBookmarkController.m in Sources */ = {isa = PBXBuildFile; fileRef = CE4385E70BB81D0500A56987 /* BDSKSearchBookmarkController.m */; }; + CE43AF162C6A0E9600BA9FE5 /* SupportFileRevisions.plist in Resources */ = {isa = PBXBuildFile; fileRef = CE43AEF62C6A0E9600BA9FE5 /* SupportFileRevisions.plist */; }; CE4476DC2128907100DF38E1 /* DOMNode_BDSKExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = CE4476DA2128907100DF38E1 /* DOMNode_BDSKExtensions.m */; }; CE44BBA326370CB6008C7833 /* ComplexStringEditor.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE44BBA226370CB6008C7833 /* ComplexStringEditor.xib */; }; CE44BBCE26371AFB008C7833 /* BDSKWebGroupView.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE44BBCD26371AFB008C7833 /* BDSKWebGroupView.xib */; }; @@ -1560,6 +1561,7 @@ CE4241430D0EAEFE00F824E7 /* BDSKEditorTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BDSKEditorTextField.m; sourceTree = "<group>"; }; CE4385E60BB81D0500A56987 /* BDSKSearchBookmarkController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BDSKSearchBookmarkController.h; sourceTree = "<group>"; }; CE4385E70BB81D0500A56987 /* BDSKSearchBookmarkController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BDSKSearchBookmarkController.m; sourceTree = "<group>"; }; + CE43AEF62C6A0E9600BA9FE5 /* SupportFileRevisions.plist */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = SupportFileRevisions.plist; sourceTree = "<group>"; }; CE4476D92128907100DF38E1 /* DOMNode_BDSKExtensions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DOMNode_BDSKExtensions.h; sourceTree = "<group>"; }; CE4476DA2128907100DF38E1 /* DOMNode_BDSKExtensions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DOMNode_BDSKExtensions.m; sourceTree = "<group>"; }; CE44BBA226370CB6008C7833 /* ComplexStringEditor.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ComplexStringEditor.xib; sourceTree = "<group>"; }; @@ -2523,6 +2525,7 @@ CE12A16F0B44AED100416D19 /* SearchGroupServers.plist */, CE8962CA0CBEC10700EA2D98 /* TemplateOptions.plist */, CE62DE4D0F4B790C00BDF01E /* Preferences.plist */, + CE43AEF62C6A0E9600BA9FE5 /* SupportFileRevisions.plist */, CE18D6E321593FB60005CE3C /* Resources.r */, F9BF481E0BD69F6F0071094F /* MODS2MARC21slim.xsl */, ); @@ -4497,6 +4500,7 @@ CEB5D61A1072AE360001C135 /* BDSKSearchGroupView.xib in Resources */, CEB0C6F026385D8700B63A29 /* BDSKCharacterConversion.strings in Resources */, CEB5D61B1072AE360001C135 /* BDSKTypeInfoEditor.xib in Resources */, + CE43AF162C6A0E9600BA9FE5 /* SupportFileRevisions.plist in Resources */, CEB5D61C1072AE360001C135 /* BDSKURLGroupSheet.xib in Resources */, CEB5D61D1072AE360001C135 /* BDSKURLSheet.xib in Resources */, CEB0C6E626385D8700B63A29 /* BDSKScriptGroupSheet.strings in Resources */, Modified: trunk/bibdesk/NSFileManager_BDSKExtensions.m =================================================================== --- trunk/bibdesk/NSFileManager_BDSKExtensions.m 2024-08-09 22:07:30 UTC (rev 28930) +++ trunk/bibdesk/NSFileManager_BDSKExtensions.m 2024-08-12 18:02:41 UTC (rev 28931) @@ -189,31 +189,45 @@ return lyxPipeURL; } -- (void)copyFileFromSharedSupportToApplicationSupport:(NSString *)fileName overwrite:(BOOL)overwrite{ - NSURL *sourceURL = [[[NSBundle mainBundle] sharedSupportURL] URLByAppendingPathComponent:fileName isDirectory:NO]; +#define SUPPORT_FILE_REVISIONS_FILENAME @"SupportFileRevisions" + +- (void)copyRelativePath:(NSString *)fileName fromURL:(NSURL *)fromURL toURL:(NSURL *)toURL revisionHashes:(NSDictionary *)revisionHashes overwrite:(BOOL)overwrite{ + NSURL *sourceURL = [fromURL URLByAppendingPathComponent:fileName isDirectory:NO]; BOOL isDir = NO; - if ([self fileExistsAtPath:[sourceURL path] isDirectory:&isDir]) { - NSURL *targetURL = [[self applicationSupportDirectoryURL] URLByAppendingPathComponent:fileName isDirectory:isDir]; + if ([self fileExistsAtPath:[sourceURL path] isDirectory:&isDir] == NO) + return; + NSURL *targetURL = [toURL URLByAppendingPathComponent:fileName isDirectory:isDir]; + if (isDir) { + if ([self fileExistsAtPath:[targetURL path] isDirectory:&isDir] == NO) { + isDir = [self createDirectoryAtURL:targetURL withIntermediateDirectories:NO attributes:nil error:NULL]; + } else if (isDir && overwrite == NO && revisionHashes == nil) { + revisionHashes = [NSDictionary dictionaryWithContentsOfURL:[[NSBundle mainBundle] URLForResource:SUPPORT_FILE_REVISIONS_FILENAME withExtension:@"plist"]]; + } if (isDir) { - if ([self fileExistsAtPath:[targetURL path] isDirectory:&isDir] == NO) - isDir = [self createDirectoryAtURL:targetURL withIntermediateDirectories:NO attributes:nil error:NULL]; - if (isDir) { - for (NSString *file in [self contentsOfDirectoryAtPath:[sourceURL path] error:NULL]) { - if ([file hasPrefix:@"."] == NO) - [self copyFileFromSharedSupportToApplicationSupport:[fileName stringByAppendingPathComponent:file] overwrite:overwrite]; - } + for (NSString *file in [self contentsOfDirectoryAtPath:[sourceURL path] error:NULL]) { + if ([file hasPrefix:@"."] == NO) + [self copyRelativePath:[fileName stringByAppendingPathComponent:file] fromURL:fromURL toURL:toURL revisionHashes:revisionHashes overwrite:overwrite]; } - } else { - if ([targetURL checkResourceIsReachableAndReturnError:NULL]) { - if (overwrite == NO) + } + } else { + if ([targetURL checkResourceIsReachableAndReturnError:NULL]) { + if (overwrite == NO) { + if (revisionHashes == nil) + revisionHashes = [NSDictionary dictionaryWithContentsOfURL:[[NSBundle mainBundle] URLForResource:SUPPORT_FILE_REVISIONS_FILENAME withExtension:@"plist"]]; + NSArray *fileRevisionHashes = [revisionHashes objectForKey:fileName]; + if ([fileRevisionHashes count] == 0 || [fileRevisionHashes containsObject:[targetURL md5Hash]] == NO) return; - [self removeItemAtURL:targetURL error:NULL]; } - [self copyItemAtURL:sourceURL toURL:targetURL error:NULL]; + [self removeItemAtURL:targetURL error:NULL]; } + [self copyItemAtURL:sourceURL toURL:targetURL error:NULL]; } } +- (void)copyFileFromSharedSupportToApplicationSupport:(NSString *)fileName overwrite:(BOOL)overwrite{ + [self copyRelativePath:fileName fromURL:[[NSBundle mainBundle] sharedSupportURL] toURL:[self applicationSupportDirectoryURL] revisionHashes:nil overwrite:overwrite]; +} + #pragma mark Temporary files and directories - (NSURL *)temporaryFileURLWithBasename:(NSString *)fileName { Modified: trunk/bibdesk/NSURL_BDSKExtensions.h =================================================================== --- trunk/bibdesk/NSURL_BDSKExtensions.h 2024-08-09 22:07:30 UTC (rev 28930) +++ trunk/bibdesk/NSURL_BDSKExtensions.h 2024-08-12 18:02:41 UTC (rev 28931) @@ -64,6 +64,8 @@ @property (nonatomic, readonly) NSArray *openMetaTags; @property (nonatomic, readonly) double openMetaRating; +@property (nonatomic, readonly) NSData *md5Hash; + @property (nonatomic, readonly) NSAttributedString *linkedText; @property (nonatomic, readonly) NSAttributedString *linkedFileName; @property (nonatomic, readonly) NSAttributedString *icon; Modified: trunk/bibdesk/NSURL_BDSKExtensions.m =================================================================== --- trunk/bibdesk/NSURL_BDSKExtensions.m 2024-08-09 22:07:30 UTC (rev 28930) +++ trunk/bibdesk/NSURL_BDSKExtensions.m 2024-08-12 18:02:41 UTC (rev 28931) @@ -46,6 +46,7 @@ #import <SkimNotesBase/SkimNotesBase.h> #import "NSFileManager_BDSKExtensions.h" #import "NSAttributedString_BDSKExtensions.h" +#import <CommonCrypto/CommonDigest.h> #define OPEN_META_TAGS_KEY @"com.apple.metadata:kMDItemOMUserTags" #define OPEN_META_RATING_KEY @"com.apple.metadata:kMDItemStarRating" @@ -328,6 +329,29 @@ return [[[SKNExtendedAttributeManager sharedNoSplitManager] propertyListFromExtendedAttributeNamed:OPEN_META_RATING_KEY atPath:[self path] traverseLink:YES error:NULL] doubleValue]; } +- (NSData *)md5Hash { + NSData *data = [NSData dataWithContentsOfURL:self]; + CC_MD5_CTX md5context; + NSUInteger signatureLength = CC_MD5_DIGEST_LENGTH; + unsigned char signature[signatureLength]; + unsigned char hexSignature[signatureLength * 2 + 1]; + NSUInteger blockSize = 4096; + char buffer[blockSize]; + NSUInteger length = [data length]; + NSRange range = NSMakeRange(0, MIN(blockSize, length)); + + CC_MD5_Init(&md5context); + while (range.length > 0) { + [data getBytes:buffer range:range]; + CC_MD5_Update(&md5context, (const void *)buffer, (CC_LONG)range.length); + range.location = NSMaxRange(range); + range.length = MIN(blockSize, length - range.location); + } + CC_MD5_Final(signature, &md5context); + + return [NSData dataWithBytes:signature length:signatureLength]; +} + #pragma mark Templating - (NSAttributedString *)linkedText { Added: trunk/bibdesk/SupportFileRevisions.plist =================================================================== (Binary files differ) Index: trunk/bibdesk/SupportFileRevisions.plist =================================================================== --- trunk/bibdesk/SupportFileRevisions.plist 2024-08-09 22:07:30 UTC (rev 28930) +++ trunk/bibdesk/SupportFileRevisions.plist 2024-08-12 18:02:41 UTC (rev 28931) Property changes on: trunk/bibdesk/SupportFileRevisions.plist ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +application/octet-stream \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit