E
E
Egorithm2020-06-29 17:11:38
Qt
Egorithm, 2020-06-29 17:11:38

What's wrong with the maximum filename length limit?

In general, I found that under Windows the limit is 255 characters, and under Linux it is 255 bytes, and I wrote this code to copy files with renaming (there, the coordinates of rectangles are stored in the name, and there can be a lot of them, not the best solution, but this is not I figured it out).

std::string filename = current_image->path().stem().string();

        #if OS == LINUX
        for (const auto& ch : filename)
            if (!std::isalpha(ch) && !std::isdigit(ch) &&
                ch != ' ' && ch != '.' &&ch != '-' && ch != '_')
            {
                QMessageBox::warning(
                    this, "Warning",
                    "Файл не будет сохранён, т.к. содержит недопустимые символы в "
                    "своём имени.\nДопустимые символы: латинские буквы, цифры, "
                    "пробел, точка, '-' и '_'."
                );

                goto filename_invalidity;
            }
        #endif

        for (const auto& rect : rect_grabber->getRects())
        {
            std::string rect_data = "_["
                + std::to_string(rect.x()) + "," + std::to_string(rect.y()) + ","
                + std::to_string(rect.width()) + "," + std::to_string(rect.height()) + ","
                + rect.getType() + "]";

            if (filename.size() + rect_data.size()
                    + current_image->path().extension().string().size() <= 255)
            {
                filename += rect_data;
            }
            else
            {
                QMessageBox::warning(
                    this, "Warning",
                    "Имя файла превысило максимально допустимую длину (255 символов)."
                    " Часть данных разметки не будет сохранена в имени файла."
                );

                break;
            }
        }

        filename += current_image->path().extension().string();

        fs::copy(current_image->path(),fs::path{load_window->getOutDirPath()/filename});
    }

All this crashes immediately after the second QMessageBox::warning is displayed, i.e. when saving with renaming. But the algorithm itself excludes the situation that more than 255 characters will be stored (including the dot extension). What's wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Korotenko, 2020-06-29
@firedragon

Under Windows, the limit is 32 kilobytes. Look for the corresponding functions, by the way, this is also in Linux. Plus there is a damn joke to create a folder, go into it and create a folder. In general, all descriptors on the file system are selected, and you can delete it only by going down and deleting folders by going up

Z
Ziptar, 2020-06-29
@Ziptar

In general, I found that under Windows the limit is 255 characters

Not in "windows", but specifically in the explorer. NTFS works great with much longer paths too - it doesn't care. But the conductor does not know how. Tellingly, some of the Windows application APIs ignore this restriction, causing the application to save the file with a longer path. Then it gets uncomfortable.

P
pindschik, 2020-09-13
@pindschik

Correction - this glitch is 255 characters - not only the name, but the whole path to it. But again - there is no restriction - this is the jamb of the default explorer and nothing more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question