Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 14 additions & 10 deletions docs/command_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,16 @@ Gotchas:

- With no profile yet, it asks you to log in. With several profiles, it asks which one, unless you give
`--user_name`. When it cannot prompt (no terminal, or `MAPILLARY_TOOLS_PROMPT_DISABLED=YES`), it fails instead.
- Image sequences, videos and zip files already uploaded from this computer are skipped. Each is compared as a
whole, by a checksum: a sequence with one image added or removed counts as new and is uploaded again, and a
video is recognized by its file alone, even if its track changed. The upload history is kept on this computer
only. `--dry_run` neither reads nor writes it.
- To look files up in the history, `upload` reads each one in full to compute its checksum, with a progress bar.
The checksums are cached in the upload history folder by file path, size and modification time, so the next
run skips uploaded files without reading them.
- Image sequences, videos and zip files already uploaded from this computer are skipped. Files are looked up one
by one: a sequence is skipped when all its images were uploaded, also as part of a larger sequence, and a
sequence with one image added is uploaded whole again. A video or zip file is recognized by its file alone, even
if a video's track changed or the file was renamed. The upload history is kept on this computer only.
`--dry_run` neither reads nor writes it.
- To look a file up, `upload` compares its size with the sizes of the files uploaded before. Only when one
matches does it read the file's fingerprint: 64 KiB from its start, middle and end. Files uploaded with earlier
versions were recorded by checksum instead, so they are not found, and are uploaded once more.
- A file of the same size as an uploaded file that differs from it only outside these three blocks is taken as
uploaded. Captures from cameras don't differ that way; `--reupload` uploads such a file anyway.
- An interrupted upload resumes where it stopped the next time you upload the same files.
- `--organization_key` takes a numeric ID. Organization names are 2 to 32 letters, digits, `-` and `_`, starting
with a letter, and are case-sensitive. Saving a name again with another ID replaces it.
Expand Down Expand Up @@ -502,9 +505,10 @@ mapillary_tools check_upload_history IMPORT_PATH [IMPORT_PATH ...] [--desc_path

Tells whether the processed image sequences, videos and zip files were uploaded from this computer before. Takes
the same import paths and description file as `upload`, and prints a JSON array with one entry per sequence,
video or zip file: its file type, sequence UUID, checksum, file names, `already_uploaded_filenames`, and
`already_uploaded`, which is true only when all its files are in the history. Images are looked up one by one,
also in sequences uploaded before, so it can report as uploaded a sequence that `upload` would upload again.
video or zip file: its file type, sequence UUID, an empty `sequence_md5sum` (no checksum is computed; kept for
compatibility), file names, `already_uploaded_filenames`, and `already_uploaded`, which is true only when all its
files are in the history. Files are looked up as `upload` looks them up, so `upload` skips the entries reported
as uploaded. A file is read only when a file of the same size was uploaded, and then only for its fingerprint.

It reads only the local upload history: it needs no login, makes no network requests, and changes nothing.
Uploads from another computer, or with `--dry_run`, are not in it.
Expand Down
67 changes: 67 additions & 0 deletions mapillary_tools/fingerprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the BSD license found in the
# LICENSE file in the root directory of this source tree.

"""
Fingerprints identify files in the upload history without reading them whole.

A fingerprint is a BLAKE2b hash of the file size and of three blocks of the
file: at the start, in the middle and at the end. A file no larger than the
three blocks is hashed whole. Every block of a compressed image or video
differs from one capture to another, and an MP4 index sits at one end of the
file, so two different captures of the same size do not share a fingerprint
in practice. Computing one takes three small reads, whatever the file size.

Two files of the same size that differ only outside the three blocks have
the same fingerprint: one is taken as the other in the upload history.
"""

from __future__ import annotations

import hashlib
import os
import typing as T
from pathlib import Path

BLOCK_SIZE = 64 * 1024

# Hashed along, so that no other hash of the same bytes is taken for a
# fingerprint, and a new way to fingerprint can be told apart
_PERSONALIZATION = b"mly-fingerprint1"

FINGERPRINT_LENGTH = 32


def fingerprint_file(path: Path) -> str:
"""Return the fingerprint of the file, as 32 hexadecimal digits"""
hasher = hashlib.blake2b(digest_size=16, person=_PERSONALIZATION)
with path.open("rb") as fp:
size = os.fstat(fp.fileno()).st_size
hasher.update(size.to_bytes(8, "little"))
if size <= 3 * BLOCK_SIZE:
hasher.update(fp.read())
else:
for offset in (0, (size - BLOCK_SIZE) // 2, size - BLOCK_SIZE):
fp.seek(offset)
hasher.update(fp.read(BLOCK_SIZE))
return hasher.hexdigest()


def combine_fingerprints(fingerprints: T.Iterable[str]) -> str:
"""Return a fingerprint for files taken together, in this order"""
hasher = hashlib.blake2b(digest_size=16, person=_PERSONALIZATION)
for fingerprint in fingerprints:
hasher.update(fingerprint.encode("ascii"))
return hasher.hexdigest()


_LOWERCASE_HEXDIGITS = frozenset("0123456789abcdef")


def is_fingerprint(value: T.Any) -> bool:
return (
isinstance(value, str)
and len(value) == FINGERPRINT_LENGTH
and set(value) <= _LOWERCASE_HEXDIGITS
)
Loading
Loading