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

Open
#306 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend, cli

Research direction

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.

Written by the indexing model from the issue text.

Description

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).

Dominant language
C++
Stars
4.6k
Forks
519
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ip7z/7zip

All issues in ip7z/7zip

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.