`rn` stores the new name verbatim (`./z`, `../z`, `/z`, `z/`); `a` normalizes the same spellings, and `d` cannot select the renamed item by name

Aperta
#306 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
backend, cli

Direzione di ricerca

Start with CPP/7zip/UI/Common/ArchiveCommandLine.cpp around AddRenamePair, then compare how archive operands are normalized in CPP/Common/Wildcard.cpp and how rename paths reach the 7z handler in CPP/7zip/UI/Common/Update.cpp and CPP/7zip/Archive/7z/7zHandlerOut.cpp. Reproduce with the rn and d commands in the issue, including names with leading and trailing separators. Done means renamed paths follow the chosen normalization or rejection behavior and can be selected consistently by d, l, and x.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment
  • 7-Zip (z) 26.06 (arm64), ip7z/7zip @ d3296e3, built with CPP/7zip/cmpl_mac_arm64.mak (Bundles/Alone2)
  • macOS 26.4.1 arm64, Apple clang 21.0.0
Description

7zz a arc.7z ./z stores the item as z (a leading ./ is removed on purpose, see #55). 7zz rn arc.7z a ./z stores the literal name ./z. The same happens for ../z and /z. A new name that ends in / (rn arc a z/) is stored on a file item, so the file is listed as z/ and extracts as z/_. The operand of d goes through the ./ stripping and the stored name does not, so d arc ./z and d arc z both leave the item in the archive. Only the wildcard * removes it.

Steps to reproduce
Z=7zz
for n in './z' '../z' '/z'; do
  d=$(mktemp -d) && cd "$d" && mkdir w && cd w
  printf AAA > a; printf BBB > ab
  $Z a -bd -y ../arc.7z a ab >/dev/null
  $Z rn -bd -y ../arc.7z a "$n" >/dev/null; echo "rn a '$n' rc=$?"
  $Z l -slt -bd ../arc.7z | grep '^Path' | grep -v arc.7z      # Path = ./z (or ../z, /z), Path = ab
  for sp in "$n" z; do
    cp ../arc.7z ../c.7z; $Z d -bd -y ../c.7z "$sp" >/dev/null; echo "  d '$sp' rc=$?"
    $Z l -slt -bd ../c.7z | grep '^Path' | grep -v c.7z          # item still present
  done
  mkdir ../w3 && cd ../w3 && printf A > z && $Z a -bd ../r.7z "$n" >/dev/null 2>&1
  $Z l -slt -bd ../r.7z | grep '^Path' | grep -v r.7z           # contrast: a stores 'z'
done
# trailing slash on a file item:
d=$(mktemp -d) && cd "$d" && printf AAA > a
$Z a -bd s.7z a >/dev/null; $Z rn -bd s.7z a z/ >/dev/null
$Z l -slt -bd s.7z | grep -E '^(Path|Size)' | grep -v s.7z   # Path = z/  Size = 3

The item can still be reached with a literal old name (rn arc ./z y gives y) and with d arc '*'.

Expected

The documentation says only: "rn : Rename files in archive" (7zz --help) and "Renames files in archive." (https://7-zip.opensource.jp/chm/cmdline/commands/rename.htm). It does not say that the new name is stored byte for byte. The maintainer's position in #55 is that stored names lose ./ and absolute prefixes ("7-zip removes path prefixes for absolute paths and for paths starting from ./"). rn should apply the same normalization, or reject such new names, so that the stored name can be selected by an operand spelling that d, l and x accept.

Actual

l -slt shows Path = ./z, ../z, /z or z/. d with either spelling leaves the item in place (rc=0, no message). a with the same spelling stores z.

Root cause (analysis on d3296e3)
  1. CPP/7zip/UI/Common/ArchiveCommandLine.cpp:500-508 (AddRenamePair) stores newName as given. CRenamePair::Prepare() (Update.cpp:288-295) checks only the old name for wildcards.
  2. CPP/7zip/UI/Common/Update.cpp:337 (CRenamePair::GetNewPath) builds dest = NewName + src.Ptr(num), and Update.cpp:526 queues it. UpdateCallback.cpp:495-496 hands it to the handler as kpidPath, and CPP/7zip/Archive/7z/7zHandlerOut.cpp:527-536 stores it after ReplaceSlashes_OsToUnix only.
  3. For a, the ./.. parts and a leading / are dropped when the operand is added to the censor: CPP/Common/Wildcard.cpp:664-680 (numSkipParts is raised past a . or .. part) and GetNumPrefixParts (Wildcard.cpp:557-602, line 602 treats a leading empty part as a prefix). rn new names never pass through this code.
Suggested fix

Normalize NewName in AddRenamePair with the same rules that the censor applies to a operands (drop a leading ./, drop ./.. parts, drop a leading /), or throw CArcCmdLineException("Unsupported rename command:") for such new names. Handle a trailing separator on a non-directory item the same way.

Related
  • #290: rn arc d/ NEW loses a path separator. #55: ./ prefixes are stripped on purpose for a; this report is about rn storing its new name verbatim while a normalizes it.

Found with a differential test setup (a model of the documented behavior, and for some reports an independent tool such as libarchive/bsdtar, compared against the real binary) and then checked by hand: every command above was re-run verbatim on the tested build, and the root cause was read in the source. Tested commit: d3296e3 (26.06).

Lingua principale
C++
Stelle
4.6k
Fork
519
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ip7z/7zip

Tutte le issue di ip7z/7zip

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.