Mac OS 10.7.5 BSD copyfile()EINVAL在复制名称中带有双引号的文件时
我正在尝试使用BSD copyfile。。。要使用如下Obj-C++代码将文件复制到装载的AFP共享/卷/交换,请执行以下操作: 最简单的例子:Mac OS 10.7.5 BSD copyfile()EINVAL在复制名称中带有双引号的文件时,c,macos,bsd,file-management,C,Macos,Bsd,File Management,我正在尝试使用BSD copyfile。。。要使用如下Obj-C++代码将文件复制到装载的AFP共享/卷/交换,请执行以下操作: 最简单的例子: #include <string> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <errno.h> #include <copyfi
#include <string>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <copyfile.h>
#include <stdio.h>
#import <Cocoa/Cocoa.h>
int copyfile_callback(int what, int stage, copyfile_state_t state, const char * src, const char * dst, void * ctx);
void copy_file(const std::string& src, const std::string& dst);
NSString* StringToNSString ( const std::string& Str );
int main ()
{
[NSAutoreleasePool new];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id appName = [[NSProcessInfo processInfo] processName];
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
id window = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200)
styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]
autorelease];
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[window setTitle:appName];
[window makeKeyAndOrderFront:nil];
copy_file(
"copyfile.mm",
"/Volumes/exchange/filename(\"[~!@#$%^&*,.']\").MP4"
);
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return 0;
}
void copy_file(const std::string& fromPath, const std::string& toPath)
{
NSLog(@"copyfile: %s -> %s", fromPath.c_str(), toPath.c_str());
copyfile_state_t s = copyfile_state_alloc();
copyfile_state_set(s, COPYFILE_STATE_STATUS_CB, (void*)©file_callback);
int returnCode = copyfile(fromPath.c_str(), toPath.c_str(), s, COPYFILE_ALL);
if( returnCode ) {
NSLog(@"copyfile error code: %d, errno=%d", returnCode, errno);
}
copyfile_state_free(s);
}
int copyfile_callback(int what, int stage, copyfile_state_t state, const char * src, const char * dst, void * ctx)
{
bool bContinue = true;
switch( what ) {
case COPYFILE_COPY_DATA:
if( stage == COPYFILE_PROGRESS ) {
int src_fd;
off_t bytes_completed = 0L, total_bytes = 0L;
copyfile_state_get(state, COPYFILE_STATE_COPIED, (void*)&bytes_completed);
copyfile_state_get(state, COPYFILE_STATE_SRC_FD, (void*)&src_fd);
struct stat fstat_info;
if( src_fd > 0 && 0 == fstat(src_fd, &fstat_info) ) {
total_bytes = fstat_info.st_size;
}
NSLog(@"copyfile_callback: Copied %lld/%lld bytes so far.", bytes_completed, total_bytes);
} else if( stage == COPYFILE_ERR ) {
NSLog(@"copyfile: COPYFILE_COPY_DATA COPYFILE_ERR");
bContinue = false;
}
break;
case COPYFILE_ERR:
bContinue = false;
break;
}
return bContinue ? COPYFILE_CONTINUE : COPYFILE_QUIT;
}
NSString* StringToNSString ( const std::string& Str )
{
NSString *pString = [NSString stringWithCString:Str.c_str()
encoding:[NSString defaultCStringEncoding]
];
return pString;
}
除非我的目标文件名toPath包含双引号符号,否则它可以正常工作。然后我得到
returnCode == -1;
errno == 22; // EINVAL
也适用于普通C++,但使用Obj-C++失败。使用Obj-C++-我还可以在system.log中看到以下消息:
5/21/13 11:47:51.314 PM myAppName: open on /Volumes/exchange/filename("[~!@#$%^&*,.']").MP4: Invalid argument
Mac OS 10.7.5和gcc 4.2.1:
$ g++ -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~148/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~148/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
正如Eric Postphil所指出的,谢谢这是网络共享的问题。升级到最新的NetTalk版本并将正确的设置设置设置为NetTalk config后,我的代码可以正常工作,无需任何更改
很抱歉回复晚了,伙计们:当我在AFP卷上用copyfilex.c、filename\[~!@$%^&*、.]\.MP4、s、COPYFILE\u尝试时,效果很好。你应该展示一个.@ EricPostpischil,我用SGSCE C++文件尝试用G++编译,它也对我有用。当我从Obj-C++调用它时,问题就出现了。我对“xcodebuild”不是很在行,因此需要一些时间来提供适当的SSCE…@EricPostphischil我添加了一些简单的示例,请检查您使用的是哪个操作系统版本、编译器和编译器版本?请注意,G++可能不是GNU C++编译器。@ 2CAN:您的源代码对我来说是没有变化的。我假设提供AFP卷的主机系统不支持文件名中的引号。它是Microsoft Windows主机吗?您可以使用不同的主机进行测试。我想您可能必须避免在文件名中使用引号。
$ g++ -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~148/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~148/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)