bool os_link_symbolic_symlink(const std::string &target, const std::string &lname, void* transaction, bool* isdir) { #if (_WIN32_WINNT >= 0x0600) DWORD flags=0; if(isdir!=NULL && *isdir) { flags|=SYMBOLIC_LINK_FLAG_DIRECTORY; } else if(isdir==NULL && isDirectory(target, transaction)) { flags|=SYMBOLIC_LINK_FLAG_DIRECTORY; } BOOL rc; if(transaction==NULL) { rc=CreateSymbolicLink(ConvertToWchar(lname).c_str(), ConvertToWchar(target).c_str(), flags); } else { rc=CreateSymbolicLinkTransactedW(ConvertToWchar(lname).c_str(), ConvertToWchar(target).c_str(), flags, transaction); } if(rc==FALSE) { DWORD lasterr = GetLastError(); Log("Creating symbolic link from \""+lname+"\" to \""+target+"\" failed with error "+convert((int)GetLastError()), LL_ERROR); SetLastError(lasterr); } return rc==TRUE; #else return false; #endif }
static bool S_create_test_symlinks(void) { #ifdef CHY_HAS_WINDOWS_H if (!CreateSymbolicLink("_fstest\\bar\\banana", "_fstest\\foo\\boffo", 0) || !CreateSymbolicLink("_fstest\\bar\\bazooka", "_fstest\\foo", 1) ) { return false; } #else if (symlink("_fstest/foo/boffo", "_fstest/bar/banana") || symlink("_fstest/foo", "_fstest/bar/bazooka") ) { return false; } #endif return true; }
/* Creates a symbolic link. (VARARGS) */ int CreateSymbolicLinkPath(const char *name, const char *path, ...) { char buffer[4096]; va_list arg; va_start(arg, path); vsnprintf(buffer, sizeof(buffer), path, arg); va_end(arg); return CreateSymbolicLink(name, buffer); }
bool createSymlink(const QString &source, const QString &target, QString &error) { QString src = QDir::toNativeSeparators(QDir::cleanPath(source)); QString tgt = QDir::toNativeSeparators(QDir::cleanPath(target)); #ifdef _WIN32 LPCWSTR lpSymlinkFileName = (LPCWSTR)tgt.utf16(); LPCWSTR lpTargetFileName = (LPCWSTR)src.utf16(); if (CreateSymbolicLink(lpSymlinkFileName, lpTargetFileName, SYMBOLIC_LINK_FLAG_DIRECTORY)) return true; #endif error = getLastWinError(); return false; }
bool os_link_symbolic_symlink(const std::wstring &target, const std::wstring &lname) { #if (_WIN32_WINNT >= 0x0600) DWORD flags=0; if(isDirectory(target)) flags|=SYMBOLIC_LINK_FLAG_DIRECTORY; DWORD rc=CreateSymbolicLink(lname.c_str(), target.c_str(), flags); if(rc==FALSE) { #ifndef OS_FUNC_NO_SERVER Server->Log(L"Creating symbolic link from \""+lname+L"\" to \""+target+L"\" failed with error "+convert((int)GetLastError()), LL_ERROR); #endif } return rc!=0; #else return true; #endif }
int symlink(const char *oldpath, const char *newpath) { return CreateSymbolicLink(newpath, oldpath, 0) ? 0 : -1; }
nfsstat3 CNFS3Prog::ProcedureSYMLINK(void) { PrintLog("SYMLINK"); char* path; post_op_fh3 obj; post_op_attr obj_attributes; wcc_data dir_wcc; nfsstat3 stat; diropargs3 where; symlinkdata3 symlink; DWORD targetFileAttr; DWORD dwFlags; std::string dirName; std::string fileName; ReadDirectory(dirName, fileName); path = GetFullPath(dirName, fileName); Read(&symlink); _In_ LPTSTR lpSymlinkFileName = path; // symlink (full path) // TODO: Maybe revisit this later for a cleaner solution // Convert target path to windows path format, maybe this could also be done // in a safer way by a combination of PathRelativePathTo and GetFullPathName. // Without this conversion nested folder symlinks do not work cross platform. std::string strFromChar; strFromChar.append(symlink.symlink_data.path); // target (should be relative path)); std::replace(strFromChar.begin(), strFromChar.end(), '/', '\\'); _In_ LPTSTR lpTargetFileName = const_cast<LPSTR>(strFromChar.c_str()); std::string fullTargetPath = dirName + std::string("\\") + std::string(lpTargetFileName); // Relative path do not work with GetFileAttributes (directory are not recognized) // so we normalize the path before calling GetFileAttributes TCHAR fullTargetPathNormalized[MAX_PATH]; _In_ LPTSTR fullTargetPathString = const_cast<LPSTR>(fullTargetPath.c_str());; GetFullPathName(fullTargetPathString, MAX_PATH, fullTargetPathNormalized, NULL); targetFileAttr = GetFileAttributes(fullTargetPathNormalized); dwFlags = 0x0; if (targetFileAttr & FILE_ATTRIBUTE_DIRECTORY) { dwFlags = SYMBOLIC_LINK_FLAG_DIRECTORY; } BOOLEAN failed = CreateSymbolicLink(lpSymlinkFileName, lpTargetFileName, dwFlags); if (failed != 0) { stat = NFS3_OK; obj.handle_follows = GetFileHandle(path, &obj.handle); obj_attributes.attributes_follow = GetFileAttributesForNFS(path, &obj_attributes.attributes); } else { stat = NFS3ERR_IO; PrintLog("An error occurs or file already exists."); stat = CheckFile(path); if (stat != NFS3_OK) { stat = NFS3ERR_IO; } } dir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirName.c_str(), &dir_wcc.after.attributes); Write(&stat); if (stat == NFS3_OK) { Write(&obj); Write(&obj_attributes); } Write(&dir_wcc); return stat; }
bool my_createsoftlink(const TCHAR *path, const TCHAR *target) { return CreateSymbolicLink(path, target, my_existsdir (target) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0) != 0; }