Repository: xbmc/xbmc
File: xbmc/platform/posix/filesystem/PosixDirectory.cpp (C++)

Code before:
  std::string root = url.Get();

  constexpr std::array<std::string_view, 3> exclusions = {".", "..", "lost+found"};
  const auto notExcluded = exclusions.end();

  if (IsAliasShortcut(root, true))
    TranslateAliasShortcut(root);

  if (!URIUtils::HasSlashAtEnd(root))
    root += "/";

  DIR *dir = opendir(root.c_str());
  if (!dir)
    return false;

  struct stat buffer;

  std::vector<CFileItemPtr> fileItems;
  struct dirent* entry;
  while ((entry = readdir(dir)) != NULL)
  {
    const std::string& strName = entry->d_name;

    if (std::ranges::find(exclusions, strName) == notExcluded)
    {
      std::string itemPath(root + strName);
      std::string itemLabel(strName);
      CCharsetConverter::unknownToUTF8(itemLabel);
      CFileItemPtr& pItem = fileItems.emplace_back(std::make_shared<CFileItem>(itemLabel));

      // Unix-based readdir implementations may return an incorrect dirent.d_ino value that
      // is not equal to the (correct) stat() obtained one. In this case the file type
      // could not be determined and the value of dirent.d_type is set to DT_UNKNOWN.
      // In order to get a correct value we have to incur the cost of calling stat.
      bool isDir = entry->d_type == DT_DIR;
      if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK ||
          !(m_flags & DIR_FLAG_NO_FILE_INFO))
      {
        if (stat(itemPath.c_str(), &buffer) == 0)
        {
          isDir = S_ISDIR(buffer.st_mode);

          KODI::TIME::FileTime fileTime, localTime;
          KODI::TIME::TimeTToFileTime(buffer.st_mtime, &fileTime);
          KODI::TIME::FileTimeToLocalFileTime(&fileTime, &localTime);

          pItem->SetDateTime(localTime);
          pItem->SetSize(isDir ? 0 : buffer.st_size);
        }
      }


Review comment (bug):
Is this equivalent to `URIUtils::AddFileToFolder`? Can there be edge cases where a simple concatenation is wrong?

Code after:

  constexpr std::array<std::string_view, 3> exclusions = {".", "..", "lost+found"};

  if (IsAliasShortcut(root, true))
    TranslateAliasShortcut(root);

  if (!URIUtils::HasSlashAtEnd(root))
    root.push_back('/');

  DIR* dir = opendir(root.c_str());
  if (!dir)
    return false;

  struct stat buffer;

  std::vector<std::shared_ptr<CFileItem>> fileItems;
  struct dirent* entry;
  while ((entry = readdir(dir)) != NULL)
  {
    const std::string& name = entry->d_name;

    if (std::ranges::find(exclusions, name) == exclusions.end())
    {
      std::string itemPath(root + name);
      std::string itemLabel(name);
      CCharsetConverter::unknownToUTF8(itemLabel);
      const auto& item = fileItems.emplace_back(std::make_shared<CFileItem>(itemLabel));

      // Unix-based readdir implementations may return an incorrect dirent.d_ino value that
      // is not equal to the (correct) stat() obtained one. In this case the file type
      // could not be determined and the value of dirent.d_type is set to DT_UNKNOWN.
      // In order to get a correct value we have to incur the cost of calling stat.
      bool isDir = entry->d_type == DT_DIR;
      if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK ||
          !(m_flags & DIR_FLAG_NO_FILE_INFO))
      {
        if (stat(itemPath.c_str(), &buffer) == 0)
        {
          isDir = S_ISDIR(buffer.st_mode);

          KODI::TIME::FileTime fileTime;
          KODI::TIME::FileTime localTime;
          KODI::TIME::TimeTToFileTime(buffer.st_mtime, &fileTime);
          KODI::TIME::FileTimeToLocalFileTime(&fileTime, &localTime);

          item->SetDateTime(localTime);
          item->SetSize(isDir ? 0 : buffer.st_size);
        }
      }

      if (isDir)