diff --git a/docs/deployment.md b/docs/deployment.md index b9ba4a7..33aaebf 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -2486,7 +2486,10 @@ read-only count of source folders and messages before copying. The size is shown only when the source reports it for every folder. Partial inventories identify how many folders were counted. Counts describe the source at the time of the check and include mail already copied on prior -passes. Confirmation starts a new copy pass with the same source +passes. On a subsequent pass, the preview can count messages after a +matching checkpoint in a bounded UID range. It leaves the estimate +unavailable for folders where it cannot do so reliably. Confirmation +starts a new copy pass with the same source credentials and uses the existing checkpoints. The preview expires after 30 minutes. Use **Import without preview** if you do not need an inventory or your provider does not supply usable folder counts. diff --git a/docs/panel-design.md b/docs/panel-design.md index dbf3ab0..38e2700 100644 --- a/docs/panel-design.md +++ b/docs/panel-design.md @@ -131,6 +131,14 @@ Show a blocked prerequisite beside the resource that needs it. Put the reason and retry action there for operators with the relevant permission. Give customers a short next step without showing internal service details. +### Import previews + +Separate a read-only preview from the action that starts a copy. Identify +the source and destination, show the snapshot totals first, and put the +per-folder or per-item breakdown below. Label partial counts and missing +estimates explicitly. Distinguish the full source inventory from work +remaining after a checkpoint. Do not display credentials or source content. + ## Review a UI change Compare the result with a Core page that serves the same purpose. In light diff --git a/src/coulobre/hosting/mailbox_import_preview.py b/src/coulobre/hosting/mailbox_import_preview.py index 0302996..994890d 100644 --- a/src/coulobre/hosting/mailbox_import_preview.py +++ b/src/coulobre/hosting/mailbox_import_preview.py @@ -26,7 +26,9 @@ from .resource_requests import lock_hosting_account LIFETIME = timedelta(minutes=30) INSPECT_SECONDS = 70 MAX_FOLDERS = 1000 -STAT = re.compile(rb"\b(MESSAGES|SIZE) ([0-9]{1,18})(?=[ )])") +STAT = re.compile( + rb"\b(MESSAGES|SIZE|UIDNEXT|UIDVALIDITY) ([0-9]{1,18})(?=[ )])" +) EXPIRED = "The preview expired. Enter the source password to check again." UNAVAILABLE = "This mailbox is no longer available for preview." @@ -162,7 +164,8 @@ def authorized_mailbox(preview): def folder_status(client, name, include_size): - attributes = "(MESSAGES SIZE)" if include_size else "(MESSAGES)" + attributes = "(MESSAGES UIDNEXT UIDVALIDITY" + attributes += " SIZE)" if include_size else ")" try: result, rows = client.status(imap.quote(name), attributes) except imaplib.IMAP4.abort: @@ -172,14 +175,45 @@ def folder_status(client, name, include_size): if result != "OK" and include_size: return folder_status(client, name, False) if result != "OK" or not rows or not isinstance(rows[0], bytes): - return None, None + return {} # Match attributes after the folder name, which may contain spaces. fields = rows[0].rsplit(b"(", 1)[-1] - values = {key: int(count) for key, count in STAT.findall(fields)} - return values.get(b"MESSAGES"), values.get(b"SIZE") + return {key: int(count) for key, count in STAT.findall(fields)} -def inventory(client, mapping): +def messages_after_checkpoint(client, name, status, checkpoint, deadline): + count = status.get(b"MESSAGES") + if checkpoint is None: + return count + validity = status.get(b"UIDVALIDITY") + next_uid = status.get(b"UIDNEXT") + if ( + count is None + or next_uid is None + or not validity + or validity != checkpoint.source_validity + or checkpoint.pending + ): + return None + if not checkpoint.last_uid: + return count + first = checkpoint.last_uid + 1 + if next_uid <= first: + return 0 + if next_uid - first > imports.UID_WINDOW or monotonic() >= deadline: + return None + try: + actual_validity, actual_next = imap.select(client, name, readonly=True) + if actual_validity == validity and actual_next == next_uid: + return len(imap.search_uids(client, first, last=next_uid - 1)) + except imaplib.IMAP4.abort: + raise + except (imap.MailboxImportError, imaplib.IMAP4.error): + pass + return None + + +def inventory(client, mapping, checkpoints=None): deadline = monotonic() + INSPECT_SECONDS caps = {cap.upper() for cap in client.capabilities} include_size = bool({"STATUS=SIZE", "IMAP4REV2"} & caps) @@ -187,12 +221,19 @@ def inventory(client, mapping): for source_name, _ in mapping[:MAX_FOLDERS]: if monotonic() >= deadline: break - count, size = folder_status(client, source_name, include_size) + status = folder_status(client, source_name, include_size) folders.append( { "name": imports.display_name(source_name), - "count": count, - "bytes": size, + "count": status.get(b"MESSAGES"), + "bytes": status.get(b"SIZE"), + "remaining": messages_after_checkpoint( + client, + source_name, + status, + (checkpoints or {}).get(imports.folder_key(source_name)), + deadline, + ), } ) readable = [folder for folder in folders if folder["count"] is not None] @@ -206,9 +247,28 @@ def inventory(client, mapping): if complete and all(folder["bytes"] is not None for folder in readable) else None, "complete": complete, + "incremental": bool(checkpoints), + "remaining": sum(folder["remaining"] for folder in folders) + if complete + and all(folder["remaining"] is not None for folder in folders) + else None, } +def checkpoints_for(preview): + job = MailboxImport.objects.filter( + mailbox=preview.mailbox, + finished_at__isnull=False, + source_host=preview.source_host, + source_user=preview.source_user, + target_host=preview.target_host, + target_user=preview.target_user, + ).first() + if not job: + return {} + return {folder.source_key: folder for folder in job.folders.all()} + + @sensitive_variables() def run_preview(preview): mailbox = authorized_mailbox(preview) @@ -243,4 +303,4 @@ def run_preview(preview): raise imap.MailboxImportError( "The source has no selectable folders to copy." ) - return inventory(source, mapping) + return inventory(source, mapping, checkpoints_for(preview)) diff --git a/src/coulobre/hosting/templates/hosting/mailbox_import.html b/src/coulobre/hosting/templates/hosting/mailbox_import.html index b18824d..739fd60 100644 --- a/src/coulobre/hosting/templates/hosting/mailbox_import.html +++ b/src/coulobre/hosting/templates/hosting/mailbox_import.html @@ -79,10 +79,15 @@ {% elif preview.problem %}
{{ preview.problem }}
{% else %} +Source snapshot. These counts include previously copied messages. The import skips messages recorded at its checkpoints.
| Source folder | Messages | Source size | |
|---|---|---|---|
| Source folder | Messages | Source size | {% if preview.report.incremental %}After checkpoint | {% endif %}
| {{ folder.name }} | {{ folder.count|default_if_none:"Not available" }} | {% if folder.bytes is not None %}{{ folder.bytes|filesizeformat }}{% else %}Not available{% endif %} | + {% if preview.report.incremental %}{{ folder.remaining|default_if_none:"Not available" }} | {% endif %}