Repository: xbmc/xbmc
File: xbmc/platform/win32/filesystem/Win32SMBDirectory.cpp (C++)

Code before:

using namespace XFILE;

using KODI::PLATFORM::WINDOWS::FromW;

namespace
{

uint64_t GetEntrySize(const WIN32_FIND_DATAW& findData)
{
  return (__int64(findData.nFileSizeHigh) << 32) + findData.nFileSizeLow;
}

KODI::TIME::FileTime GetEntryTime(CFileItemPtr& item, const WIN32_FIND_DATAW& findData)
{
  KODI::TIME::FileTime fileTime{};
  fileTime.lowDateTime = findData.ftLastWriteTime.dwLowDateTime;
  fileTime.highDateTime = findData.ftLastWriteTime.dwHighDateTime;

  KODI::TIME::FileTime localTime{};
  if (KODI::TIME::FileTimeToLocalFileTime(&fileTime, &localTime) == TRUE)
    return localTime;

  CDateTime dt{item->GetDateTime()};
  dt.SetValid(false);
  return dt;
}

} // Unnamed namespace

// local helper
static inline bool worthTryToConnect(const DWORD lastErr)
{
  return lastErr != ERROR_FILE_NOT_FOUND      && lastErr != ERROR_BAD_NET_NAME  &&
         lastErr != ERROR_NO_NET_OR_BAD_PATH  && lastErr != ERROR_NO_NETWORK    &&
         lastErr != ERROR_BAD_NETPATH;
}

/**
* Get servers recursively or get shares from specific server
* @param basePathToScanPtr the pointer to start point for scan
* @param urlPrefixForItems path prefix to be putted into list items
* @param items             the list of the collected items
* @param getShares         set to true to get shares, set for false to get servers
* @return true if succeed, false otherwise
*/
static bool localGetNetworkResources(struct _NETRESOURCEW* basePathToScanPtr, const std::string& urlPrefixForItems, CFileItemList& items, bool getShares);
static bool localGetShares(const std::wstring& serverNameToScan, const std::string& urlPrefixForItems, CFileItemList& items);
static bool localGetServers(const std::string& urlPrefixForItems, CFileItemList& items);

// check for empty string, remove trailing slash if any, convert to win32 form

Review comment (refactor):
The original code was a bit weird - but I think this tangles things more.
If I followed correctly, in original and here, item is always a brand new item and there is nothing to retrieve and invalidate since the m_dateTime member is initialized with a default CDateTime, which is in "not valid" state.
So might as well simplify and do nothing to the item's date in that case?

Code after:

using namespace XFILE;

using KODI::PLATFORM::WINDOWS::FromW;
using KODI::TIME::FileTime;

// local helper
static inline bool worthTryToConnect(const DWORD lastErr)
{
  return lastErr != ERROR_FILE_NOT_FOUND      && lastErr != ERROR_BAD_NET_NAME  &&
         lastErr != ERROR_NO_NET_OR_BAD_PATH  && lastErr != ERROR_NO_NETWORK    &&
         lastErr != ERROR_BAD_NETPATH;
}

/**
* Get servers recursively or get shares from specific server
* @param basePathToScanPtr the pointer to start point for scan
* @param urlPrefixForItems path prefix to be putted into list items
* @param items             the list of the collected items
* @param getShares         set to true to get shares, set for false to get servers
* @return true if succeed, false otherwise
*/
static bool localGetNetworkResources(struct _NETRESOURCEW* basePathToScanPtr, const std::string& urlPrefixForItems, CFileItemList& items, bool getShares);
static bool localGetShares(const std::wstring& serverNameToScan, const std::string& urlPrefixForItems, CFileItemList& items);
static bool localGetServers(const std::string& urlPrefixForItems, CFileItemList& items);

// check for empty string, remove trailing slash if any, convert to win32 form
inline static std::wstring prepareWin32SMBDirectoryName(const CURL& url)
{
  assert(url.IsProtocol("smb"));

  if (url.GetHostName().empty() || url.GetShareName().empty())
    return std::wstring(); // can't use win32 standard file API, return empty string

  std::wstring nameW(CWIN32Util::ConvertPathToWin32Form("\\\\?\\UNC\\" + url.GetHostName() + '\\' + url.GetFileName()));
  if (!nameW.empty() && nameW.back() == L'\\')
    nameW.pop_back(); // remove slash at the end if any

  return nameW;
}

CWin32SMBDirectory::CWin32SMBDirectory(void)
{}

CWin32SMBDirectory::~CWin32SMBDirectory(void)
{}

bool CWin32SMBDirectory::GetDirectory(const CURL& url, CFileItemList &items)
{
  assert(url.IsProtocol("smb"));
  items.Clear();