|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tsystem.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tconvert.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
#define UNICODE // per le funzioni di conversione da/a UNC
|
|
Toshihiro Shimizu |
890ddd |
#include <windows.h></windows.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <lm.h></lm.h>
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool TSystem::isUNC(const TFilePath &path)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// si assume che il path e' gia' in formato UNC se inizia con "\\"
|
|
Toshihiro Shimizu |
890ddd |
wstring pathStr = path.getWideString();
|
|
Toshihiro Shimizu |
890ddd |
return pathStr.length() > 2 && pathStr.substr(0, 2) == L"\\\\";
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TFilePath TSystem::toUNC(const TFilePath &fp)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
if (QString::fromStdWString(fp.getWideString()).startsWith('+'))
|
|
Toshihiro Shimizu |
890ddd |
return fp;
|
|
Toshihiro Shimizu |
890ddd |
if (isUNC(fp))
|
|
Toshihiro Shimizu |
890ddd |
return fp;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string fpStr = toString(fp.getWideString());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (fpStr.length() > 1 && fpStr.c_str()[1] == ':') {
|
|
Toshihiro Shimizu |
890ddd |
string drive = fpStr.substr(0, 3);
|
|
Toshihiro Shimizu |
890ddd |
UINT uiDriveType = GetDriveTypeA(drive.c_str());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (uiDriveType & DRIVE_REMOTE) {
|
|
Toshihiro Shimizu |
890ddd |
// il drive e' montato
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
DWORD cbBuff = _MAX_PATH; // Size of buffer
|
|
Toshihiro Shimizu |
890ddd |
char szBuff[_MAX_PATH]; // Buffer to receive information
|
|
Toshihiro Shimizu |
890ddd |
REMOTE_NAME_INFO *prni = (REMOTE_NAME_INFO *)&szBuff;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Pointers to head of buffer
|
|
Toshihiro Shimizu |
890ddd |
UNIVERSAL_NAME_INFO *puni = (UNIVERSAL_NAME_INFO *)&szBuff;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
DWORD dwResult = WNetGetUniversalNameW(toWideString(fpStr).c_str(),
|
|
Toshihiro Shimizu |
890ddd |
UNIVERSAL_NAME_INFO_LEVEL,
|
|
Toshihiro Shimizu |
890ddd |
(LPVOID)&szBuff,
|
|
Toshihiro Shimizu |
890ddd |
&cbBuff);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
switch (dwResult) {
|
|
Toshihiro Shimizu |
890ddd |
case NO_ERROR:
|
|
Toshihiro Shimizu |
890ddd |
return TFilePath(toString(puni->lpUniversalName));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
case ERROR_NOT_CONNECTED:
|
|
Toshihiro Shimizu |
890ddd |
// The network connection does not exists.
|
|
Toshihiro Shimizu |
890ddd |
throw TException("The path specified doesn't refer to a shared folder");
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
case ERROR_CONNECTION_UNAVAIL:
|
|
Toshihiro Shimizu |
890ddd |
// The device is not currently connected,
|
|
Toshihiro Shimizu |
890ddd |
// but it is a persistent connection.
|
|
Toshihiro Shimizu |
890ddd |
throw TException("The shared folder is not currently connected");
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
default:
|
|
Toshihiro Shimizu |
890ddd |
throw TException("Cannot convert the path specified to UNC");
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
// It's a local drive so search for a share to it...
|
|
Toshihiro Shimizu |
890ddd |
NET_API_STATUS res;
|
|
Toshihiro Shimizu |
890ddd |
PSHARE_INFO_502 BufPtr, p;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
DWORD er = 0,
|
|
Toshihiro Shimizu |
890ddd |
tr = 0,
|
|
Toshihiro Shimizu |
890ddd |
resume = 0,
|
|
Toshihiro Shimizu |
890ddd |
i;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int iBestMatch = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string csTemp,
|
|
Toshihiro Shimizu |
890ddd |
csTempDrive,
|
|
Toshihiro Shimizu |
890ddd |
csBestMatch;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
do {
|
|
Toshihiro Shimizu |
890ddd |
res = NetShareEnum(NULL, 502, (LPBYTE *)&BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// If the call succeeds,
|
|
Toshihiro Shimizu |
890ddd |
if (res == ERROR_SUCCESS || res == ERROR_MORE_DATA) {
|
|
Toshihiro Shimizu |
890ddd |
p = BufPtr;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Loop through the entries;
|
|
Toshihiro Shimizu |
890ddd |
for (i = 1; i <= er; i++) {
|
|
Toshihiro Shimizu |
890ddd |
if (p->shi502_type == STYPE_DISKTREE) {
|
|
Toshihiro Shimizu |
890ddd |
//#ifdef IS_DOTNET
|
|
Toshihiro Shimizu |
890ddd |
// shi502_path e' una wstring, aanche se la dichiarazione di PSHARE_INFO_502 non lo sa!
|
|
Toshihiro Shimizu |
890ddd |
wstring shareLocalPathW = (LPWSTR)(p->shi502_path);
|
|
Toshihiro Shimizu |
890ddd |
string shareLocalPath = toString(shareLocalPathW);
|
|
Toshihiro Shimizu |
890ddd |
//#else
|
|
Toshihiro Shimizu |
890ddd |
//string shareLocalPath = toString(p->shi502_path);
|
|
Toshihiro Shimizu |
890ddd |
//#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (toLower(fpStr).find(toLower(shareLocalPath)) == 0) {
|
|
Toshihiro Shimizu |
890ddd |
string hostName = TSystem::getHostName().toStdString();
|
|
Toshihiro Shimizu |
890ddd |
// #ifdef IS_DOTNET
|
|
Toshihiro Shimizu |
890ddd |
// shi502_netname e' una wstring, anche se la dichiarazione di PSHARE_INFO_502 non lo sa!
|
|
Toshihiro Shimizu |
890ddd |
wstring shareNetNameW = (LPWSTR)(p->shi502_netname);
|
|
Toshihiro Shimizu |
890ddd |
string shareNetName = toString(shareNetNameW);
|
|
Toshihiro Shimizu |
890ddd |
// #else
|
|
Toshihiro Shimizu |
890ddd |
//string shareNetName = toString(p->shi502_netname);
|
|
Toshihiro Shimizu |
890ddd |
//#endif
|
|
Toshihiro Shimizu |
890ddd |
shareNetName.append("\\");
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string fp(fpStr);
|
|
Toshihiro Shimizu |
890ddd |
string uncpath = "\\\\" + hostName + "\\" +
|
|
Toshihiro Shimizu |
890ddd |
fp.replace(0, shareLocalPath.size(), shareNetName);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return TFilePath(uncpath);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
p++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Free the allocated buffer.
|
|
Toshihiro Shimizu |
890ddd |
NetApiBufferFree(BufPtr);
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
//TRACE(_T("Error: %ld\n"), res);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Continue to call NetShareEnum while
|
|
Toshihiro Shimizu |
890ddd |
// there are more entries.
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
} while (res == ERROR_MORE_DATA); // end do
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//throw TException("Cannot convert the path specified to UNC");
|
|
Toshihiro Shimizu |
890ddd |
return fp;
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
//throw TException("Cannot convert the path specified to UNC");
|
|
Toshihiro Shimizu |
890ddd |
return fp;
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TFilePath TSystem::toLocalPath(const TFilePath &fp)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
if (!isUNC(fp))
|
|
Toshihiro Shimizu |
890ddd |
return TFilePath(fp);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string pathStr = toString(fp.getWideString());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// estrae hostname e il nome dello share dal path UNC
|
|
Toshihiro Shimizu |
890ddd |
string::size_type idx = pathStr.find_first_of("\\", 2);
|
|
Toshihiro Shimizu |
890ddd |
if (idx == string::npos)
|
|
Toshihiro Shimizu |
890ddd |
throw TException("The path specified is not a valid UNC path");
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string hostname = pathStr.substr(2, idx - 2);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (toLower(hostname) != toLower(TSystem::getHostName().toStdString()))
|
|
Toshihiro Shimizu |
890ddd |
throw TException("The UNC path specified does not refer to the local host");
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string::size_type idx2 = pathStr.find_first_of("\\", idx + 1);
|
|
Toshihiro Shimizu |
890ddd |
if (idx2 == string::npos)
|
|
Toshihiro Shimizu |
890ddd |
throw TException("The path specified is not a valid UNC path");
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string fpShareName = pathStr.substr(idx + 1, idx2 - idx - 1);
|
|
Toshihiro Shimizu |
890ddd |
string path = pathStr.substr(idx2 + 1, pathStr.size() - idx2);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
NET_API_STATUS res;
|
|
Toshihiro Shimizu |
890ddd |
do {
|
|
Toshihiro Shimizu |
890ddd |
PSHARE_INFO_502 BufPtr, p;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
DWORD er = 0, tr = 0, resume = 0;
|
|
Toshihiro Shimizu |
890ddd |
res = NetShareEnum(NULL, 502, (LPBYTE *)&BufPtr, DWORD(-1), &er, &tr, &resume);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// If the call succeeds,
|
|
Toshihiro Shimizu |
890ddd |
if (res == ERROR_SUCCESS || res == ERROR_MORE_DATA) {
|
|
Toshihiro Shimizu |
890ddd |
p = BufPtr;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Loop through the entries;
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 1; i <= (int)er; i++) {
|
|
Toshihiro Shimizu |
890ddd |
if (p->shi502_type == STYPE_DISKTREE) {
|
|
Toshihiro Shimizu |
890ddd |
//#ifdef IS_DOTNET
|
|
Toshihiro Shimizu |
890ddd |
//shi502_netname e' una wstring, anche se la dichiarazione di PSHARE_INFO_502 non lo sa!
|
|
Toshihiro Shimizu |
890ddd |
wstring shareNetNameW = (LPWSTR)(p->shi502_netname);
|
|
Toshihiro Shimizu |
890ddd |
string shareNetName = toString(shareNetNameW);
|
|
Toshihiro Shimizu |
890ddd |
// #else
|
|
Toshihiro Shimizu |
890ddd |
//string shareNetName = toString(p->shi502_netname);
|
|
Toshihiro Shimizu |
890ddd |
//#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (toLower(fpShareName) == toLower(shareNetName)) {
|
|
Toshihiro Shimizu |
890ddd |
//#ifdef IS_DOTNET
|
|
Toshihiro Shimizu |
890ddd |
// shi502_path e' una wstring, anche se la dichiarazione di PSHARE_INFO_502 non lo sa!
|
|
Toshihiro Shimizu |
890ddd |
wstring shareLocalPathW = (LPWSTR)(p->shi502_path);
|
|
Toshihiro Shimizu |
890ddd |
string shareLocalPath = toString(shareLocalPathW);
|
|
Toshihiro Shimizu |
890ddd |
//#else
|
|
Toshihiro Shimizu |
890ddd |
//string shareLocalPath = toString(p->shi502_path);
|
|
Toshihiro Shimizu |
890ddd |
//#endif
|
|
Toshihiro Shimizu |
890ddd |
string localPath = shareLocalPath + path;
|
|
Toshihiro Shimizu |
890ddd |
return TFilePath(localPath);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
p++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Free the allocated buffer.
|
|
Toshihiro Shimizu |
890ddd |
NetApiBufferFree(BufPtr);
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
//TRACE(_T("Error: %ld\n"), res);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Continue to call NetShareEnum while
|
|
Toshihiro Shimizu |
890ddd |
// there are more entries.
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
} while (res == ERROR_MORE_DATA); // end do
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
throw TException("Cannot convert to a local path");
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
throw TException("Cannot convert to a local path");
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|