Можно ли удалить разделы разреженного файла?

Если вы запустите rm -Rf /, rmвыведет сообщение об ошибке и остановится, как указано в POSIX:

if an operand resolves to the root directory, rm shall write a diagnostic message to standard error and do nothing more with such operands.

В других случаях, или если вы принудительно rmобрабатываете /(, предполагая, что ваша версия может быть принудительно запущена, , например. GNU rmс опцией --no-preserve-root), rmудаляет файлы и каталоги, как только может. Он обрабатывает каталоги в глубине -первого порядка, так что он может удалять каталоги по мере их опустошения. Таким образом, за ваши пять секунд он, скорее всего, удалит файлы и каталоги.

Это указано в POSIX (см. ссылку выше):

For each file the following steps shall be taken:

  1. If the file does not exist:

    a. If the -f option is not specified, rm shall write a diagnostic message to standard error.

    b. Go on to any remaining files.

  2. If file is of type directory, the following steps shall be taken:

    a. If neither the -R option nor the -r option is specified, rm shall write a diagnostic message to standard error, do nothing more with file, and go on to any remaining files.

    b. If file is an empty directory, rm may skip to step 2d. If the -f option is not specified, and either the permissions of file do not permit writing and the standard input is a terminal or the -i option is specified, rm shall write a prompt to standard error and read a line from the standard input. If the response is not affirmative, rm shall do nothing more with the current file and go on to any remaining files.

    c. For each entry contained in file, other than dot or dot-dot, the four steps listed here (1 to 4) shall be taken with the entry as if it were a file operand. The rm utility shall not traverse directories by following symbolic links into other parts of the hierarchy, but shall remove the links themselves.

    d. If the -i option is specified, rm shall write a prompt to standard error and read a line from the standard input. If the response is not affirmative, rm shall do nothing more with the current file, and go on to any remaining files.

  3. If file is not of type directory, the -f option is not specified, and either the permissions of file do not permit writing and the standard input is a terminal or the -i option is specified, rm shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, rm shall do nothing more with the current file and go on to any remaining files.

  4. If the current file is a directory, rm shall perform actions equivalent to the rmdir() function defined in the System Interfaces volume of POSIX.1-2017 called with a pathname of the current file used as the path argument. If the current file is not a directory, rm shall perform actions equivalent to the unlink() function defined in the System Interfaces volume of POSIX.1-2017 called with a pathname of the current file used as the path argument.

    If this fails for any reason, rm shall write a diagnostic message to standard error, do nothing more with the current file, and go on to any remaining files.

The rm utility shall be able to descend to arbitrary depths in a file hierarchy, and shall not fail due to path length limitations (unless an operand specified by the user exceeds system limitations).

2
20.03.2020, 17:34
1 ответ

В Linux вы можете пробивать дыры в файле, используяfallocate()флаг FALLOC_FL_PUNCH_HOLE(в сочетании сFALLOC_FL_KEEP_SIZE):

fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, length);

В файловых системах, которые поддерживают эту операцию, это увеличит разреженность файла, если комбинация смещения и длины покрывает целые блоки; все остальное будет только обнулено. В файловых системах, которые не поддерживают эту операцию, будет возвращена ошибка.

См. также Что такое функции для управления разреженными файлами в Linux?

2
28.04.2021, 23:20

Теги

Похожие вопросы