Skip to content

Commit

Permalink
create: correct volume name
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Jan 13, 2014
1 parent 69c7f7f commit 54361b7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ results

npm-debug.log
node_modules
build
8 changes: 8 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "volume",
"sources": [ "src/volume.cc" ]
}
]
}
4 changes: 3 additions & 1 deletion lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var path = require('path');
var assert = require('assert');
var encode = require('./encode');

var addon = require('../build/Release/volume.node');

var findVolume = function (startPath, startStat) {

var lastDev = startStat.dev;
Expand Down Expand Up @@ -65,7 +67,7 @@ module.exports = exports = function (targetPath) {
};

info.volume = {
name: path.basename(volumePath),
name: addon.getVolumeName(volumePath),
created: volumeStat.ctime,
signature: 'H+',
type: (volumePath === '/' ? 'local' : 'other')
Expand Down
51 changes: 51 additions & 0 deletions src/impl-apple.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFString.h>

using namespace v8;

Local<String> MYCFStringGetV8String(CFStringRef aString) {

if (aString == NULL) {
return String::New("");
}

CFIndex length = CFStringGetLength(aString);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
char *buffer = (char *) malloc(maxSize);

if (CFStringGetCString(aString, buffer, maxSize, kCFStringEncodingUTF8)) {

Local<String> result = String::New(buffer);
free(buffer);

return result;
}

return String::New("");
}

Handle<Value> MethodGetVolumeName(const Arguments& args) {
HandleScope scope;

String::AsciiValue aPath(args[0]);

CFStringRef out;
CFErrorRef error;

CFStringRef volumePath = CFStringCreateWithCString(NULL, *aPath, kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, volumePath, kCFURLPOSIXPathStyle, true);

if(CFURLCopyResourcePropertyForKey(url, kCFURLVolumeNameKey, &out, &error)) {

Local<String> result = MYCFStringGetV8String(out);

return scope.Close(result);
} else {

Local<String> desc = MYCFStringGetV8String(CFErrorCopyDescription(error));
ThrowException(Exception::Error(desc)->ToObject());

return scope.Close(Undefined());
}
}
15 changes: 15 additions & 0 deletions src/volume.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#include <node.h>
#include <v8.h>

#ifdef __APPLE__
#include "impl-apple.cc"
#else
#error This platform is not implemented yet
#endif

void init(v8::Handle<v8::Object> exports) {
exports->Set(v8::String::NewSymbol("getVolumeName"), v8::FunctionTemplate::New(MethodGetVolumeName)->GetFunction());
}

NODE_MODULE(volume, init)

0 comments on commit 54361b7

Please sign in to comment.