/filesystems-objc/YTFS/YTFS_Filesystem.m

http://macfuse.googlecode.com/ · Objective C · 125 lines · 88 code · 15 blank · 22 comment · 10 complexity · 3312ef2a1cff78c61173ac7b36adf004 MD5 · raw file

  1. // ================================================================
  2. // Copyright (C) 2008 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // ================================================================
  16. //
  17. // YTFS_Filesystem.m
  18. // YTFS
  19. //
  20. // Created by ted on 12/7/08.
  21. //
  22. #import "YTFS_Filesystem.h"
  23. #import "YTVideo.h"
  24. #import "NSImage+IconData.h"
  25. #import <MacFUSE/MacFUSE.h>
  26. // Category on NSError to simplify creating an NSError based on posix errno.
  27. @interface NSError (POSIX)
  28. + (NSError *)errorWithPOSIXCode:(int)code;
  29. @end
  30. @implementation NSError (POSIX)
  31. + (NSError *)errorWithPOSIXCode:(int) code {
  32. return [NSError errorWithDomain:NSPOSIXErrorDomain code:code userInfo:nil];
  33. }
  34. @end
  35. @implementation YTFS_Filesystem
  36. #pragma mark Directory Contents
  37. - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error {
  38. if ([path isEqualToString:@"/"]) {
  39. return [videos_ allKeys];
  40. }
  41. *error = [NSError errorWithPOSIXCode:ENOENT];
  42. return nil;
  43. }
  44. #pragma mark Getting Attributes
  45. - (NSDictionary *)attributesOfItemAtPath:(NSString *)path
  46. userData:(id)userData
  47. error:(NSError **)error {
  48. if ([self videoAtPath:path]) {
  49. return [NSDictionary dictionary];
  50. }
  51. return nil;
  52. }
  53. #pragma mark File Contents
  54. - (NSData *)contentsAtPath:(NSString *)path {
  55. YTVideo* video = [self videoAtPath:path];
  56. if (video) {
  57. return [video xmlData];
  58. }
  59. return nil;
  60. }
  61. #pragma mark FinderInfo and ResourceFork (Optional)
  62. - (NSDictionary *)finderAttributesAtPath:(NSString *)path
  63. error:(NSError **)error {
  64. NSDictionary* attribs = nil;
  65. if ([self videoAtPath:path]) {
  66. NSNumber* finderFlags = [NSNumber numberWithLong:kHasCustomIcon];
  67. attribs = [NSDictionary dictionaryWithObject:finderFlags
  68. forKey:kGMUserFileSystemFinderFlagsKey];
  69. }
  70. return attribs;
  71. }
  72. - (NSDictionary *)resourceAttributesAtPath:(NSString *)path
  73. error:(NSError **)error {
  74. NSMutableDictionary* attribs = nil;
  75. YTVideo* video = [self videoAtPath:path];
  76. if (video) {
  77. attribs = [NSMutableDictionary dictionary];
  78. NSURL* url = [video playerURL];
  79. if (url) {
  80. [attribs setObject:url forKey:kGMUserFileSystemWeblocURLKey];
  81. }
  82. url = [video thumbnailURL];
  83. if (url) {
  84. NSImage* image = [[[NSImage alloc] initWithContentsOfURL:url] autorelease];
  85. NSData* icnsData = [image icnsDataWithWidth:256];
  86. [attribs setObject:icnsData forKey:kGMUserFileSystemCustomIconDataKey];
  87. }
  88. }
  89. return attribs;
  90. }
  91. #pragma mark Init and Dealloc
  92. - (id)initWithVideos:(NSDictionary *)videos {
  93. if ((self = [super init])) {
  94. videos_ = [videos retain];
  95. }
  96. return self;
  97. }
  98. - (void)dealloc {
  99. [videos_ release];
  100. [super dealloc];
  101. }
  102. - (YTVideo *)videoAtPath:(NSString *)path {
  103. NSArray* components = [path pathComponents];
  104. if ([components count] != 2) {
  105. return nil;
  106. }
  107. YTVideo* video = [videos_ objectForKey:[components objectAtIndex:1]];
  108. return video;
  109. }
  110. @end