Skip to content

Commit 1ca0a50

Browse files
committed
Update tips.rst to clarify handling of dot directory entries: #68
1 parent bbf5f84 commit 1ca0a50

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

docs/pages/reference/tips.rst

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
Tips
22
------------
33

4-
Sanitize dot-files or dot-directories
5-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
Handle dot directory entries (``.`` and ``..``)
5+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6+
The path components ``.`` (current directory) and ``..`` (parent directory) are reserved names according to the underlying file system specifications:
7+
8+
- POSIX: `IEEE Std 1003.1-2017, Section 4.13 Pathname Resolution <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html>`__ defines them as the special filenames *dot* and *dot-dot*, which always refer to the directory itself and its parent directory, respectively.
9+
- Windows: `Naming Files, Paths, and Namespaces <https://learn.microsoft.com/windows/win32/fileio/naming-a-file>`__ and `MS-FSCC 2.1.5.1 Dot Directory Names <https://learn.microsoft.com/openspecs/windows_protocols/ms-fscc/fccd0313-0364-45bd-b75c-924fd6a5662f>`__ likewise reserve them as dot directory names.
10+
11+
In principle they would be subject to both validation and sanitization.
12+
However, since these names appear very frequently in real-world inputs (e.g., relative paths), ``pathvalidate`` excludes them from the default reserved-name checks for both ``validate_*`` and ``sanitize_*`` functions.
13+
If you need to treat them as reserved, specify ``additional_reserved_names=[".", ".."]`` as shown in the sections below.
14+
15+
Sanitize dot directory entries
16+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
617
When you process filenames or filepaths containing ``.`` or ``..`` with the ``sanitize_filename`` function or the ``sanitize_filepath`` function, by default, ``sanitize_filename`` does nothing, and ``sanitize_filepath`` normalizes the filepaths:
718

819
.. code-block:: python
@@ -51,3 +62,44 @@ If you would like to replace ``.`` and ``..`` like other reserved words, you nee
5162
5263
._
5364
hoge/._/foo
65+
66+
Validate dot directory entries
67+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
By default, ``validate_filename`` and ``validate_filepath`` accept ``.`` and ``..`` as valid names and do not raise a ``ValidationError``:
69+
70+
.. code-block:: python
71+
72+
from pathvalidate import validate_filename
73+
74+
validate_filename(".")
75+
validate_filename("..")
76+
77+
If you would like to reject ``.`` and ``..`` like other reserved names, specify them via the ``additional_reserved_names`` argument:
78+
79+
.. code-block:: python
80+
81+
from pathvalidate import validate_filename
82+
from pathvalidate.error import ValidationError
83+
84+
try:
85+
validate_filename(".", additional_reserved_names=[".", ".."])
86+
except ValidationError as e:
87+
print(e)
88+
89+
.. code-block:: console
90+
91+
[PV1002] found a reserved name by a platform: '.' is a reserved name, platform=universal, reusable_name=False
92+
93+
The same option is available for ``is_valid_filename`` / ``is_valid_filepath`` to obtain a boolean result without raising:
94+
95+
.. code-block:: python
96+
97+
from pathvalidate import is_valid_filename
98+
99+
print(is_valid_filename("."))
100+
print(is_valid_filename(".", additional_reserved_names=[".", ".."]))
101+
102+
.. code-block:: console
103+
104+
True
105+
False

0 commit comments

Comments
 (0)