Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make ignored_files support re #33

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Configs on how the backup is made
"world"
],
"ignored_files": [
"session.lock"
"server/session.lock"
],
"follow_target_symlink": false,
"hash_method": "xxh128",
Expand Down Expand Up @@ -262,11 +262,7 @@ For example, for bukkit-like servers that split the world dimensions, you might
A list of file names to be ignored during backup. It contains `session.lock` by default
to solve the backup failure problem caused by `session.lock` being occupied by the server in Windows

If the name string starts with `*`, then it will ignore files with name ending with specific string,
e.g. `*.test` makes all files ends with `.test` be ignored, like `a.test`

If the name string ends with `*`, then it will ignore files with name starting with specific string,
e.g. `temp*` makes all files starts with `temp` be ignored, like `tempfile`
This item should contain a regular expression. Note that due to it being a JSON file, the escape character "" should be doubled, i.e., "\".

- Type: `List[str]`

Expand Down
8 changes: 2 additions & 6 deletions docs/config.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Prime Backup 在创建备份时的操作时序如下:
"world"
],
"ignored_files": [
"session.lock"
"server/session.lock"
],
"follow_target_symlink": false,
"hash_method": "xxh128",
Expand Down Expand Up @@ -261,11 +261,7 @@ Prime Backup 在创建备份时的操作时序如下:
在备份时忽略的文件名列表,默认仅包含 `session.lock`
以解决 Windows 下 `session.lock` 被服务端占用导致备份失败的问题

若文件名字符串以 `*` 开头,则将忽略以指定字符串结尾的文件,
如 `*.test` 表示忽略所有以 `.test` 结尾的文件,如 `a.test`

若文件名字符串以 `*` 结尾,则将忽略以指定字符串开头的文件,
如 `temp*` 表示忽略所有以 `temp` 开头的文件,如 `tempfile`
该项填写一个正则表达式,注意由于是json文件,转义符"\"需要打两遍即"\\"。

- 类型:`List[str]`

Expand Down
14 changes: 4 additions & 10 deletions prime_backup/config/backup_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from typing import List, Optional
import re

from mcdreforged.api.utils import Serializable

Expand All @@ -14,7 +15,7 @@ class BackupConfig(Serializable):
'world',
]
ignored_files: List[str] = [
'session.lock',
'server/session.lock',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

错误配置的默认值

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是等效的

Copy link
Contributor

@Fallen-Breath Fallen-Breath Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不等效。多了解下相关逻辑吧,这里有 4 个不同种类错误,不清楚就别改了

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 正则表达式里未转义 .
  2. 路径分隔符硬编码为 posix 下的实现,不兼容 windows
  3. 假定了 MCDR 配置的服务端路径为 server
  4. session.lock 的路径错误。其应当位于存档文件夹中

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 正则表达式里未转义 .

    1. 路径分隔符硬编码为 posix 下的实现,不兼容 windows

    2. 假定了 MCDR 配置的服务端路径为 server

    3. session.lock 的路径错误。其应当位于存档文件夹中

好的

]
follow_target_symlink: bool = False
hash_method: HashMethod = HashMethod.xxh128
Expand All @@ -34,14 +35,7 @@ def is_file_ignore(self, full_path: Path) -> bool:
"""
Apply to not only files
"""
# TODO: better rule?
name = full_path.name
for item in self.ignored_files:
if len(item) > 0:
if item[0] == '*' and name.endswith(item[1:]):
return True
if item[-1] == '*' and name.startswith(item[:-1]):
return True
if name == item:
return True
if re.match(item, str(full_path)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议用 Path.as_posix

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

return True
return False