File name too long

Paul Slootman paul at
Wed Mar 12 20:27:38 EST 2003

On Tue 11 Mar 2003, jw schultz wrote:
> I threw this out there because it looked like we were
> heading towards a rewrite of the whole function and i wanted
> to rethink it instead of just reworking parts of it.  If a
> concensus is that this is the way to go i'm all for that.
> If the pointer arithmatic is too offensive that's fine too.

A rewrite is fine; and personally I like pointer arithmetic :-)

So this is a final draft IMHO:

 * get_tmpname() - create a tmp filename for a given filename
 *   If a tmpdir is defined, use that as the directory to put it in.
 *   Otherwise, the tmp filename is in the same directory as the given name.
 *   Note that there may be no directory at all in the given name!
 *   The tmp filename is basically the given filename with a dot prepended,
 *   and .XXXXXX appended (for mkstemp() to put its unique gunk in).
 *   Take care to not exceed either the MAXPATHLEN or NAME_MAX, esp. the
 *   last, as the basename basically becomes 8 chars longer. In that case,
 *   the original name is shortened sufficiently to make it all fit.
 *   Of course, there's no real reason for the tmp name to look like the
 *   original, except to satisfy us humans. As long as it's unique, rsync
 *   will work.
static int get_tmpname(char *fnametmp, char *fname)
	char	*f;
	int	length = 0;
	int	maxname;

	if (tmpdir) {
		strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
		length = strlen(fnametmp);
		strcpy(fnametmp + length++, "/");

	if ((f = strrchr(fname, '/'))) {
		if (!tmpdir) {
			/* copy up to and including the slash */
			length = f - fname;
			strlcpy(fnametmp, fname, length);
	else {
		f = fname;
	strcpy(fnametmp + length++, ".");

	maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);

	if (maxname < 1) {
		rprintf(FERROR, "temporary filename too long: %s\n", fname);
		return 0;

	strlcpy(fnametmp + length, f, maxname);
	strcat(fnametmp + length, ".XXXXXX");

	return 1;

The extra parentheses around the strrchr thing is to keep gcc quiet.

Paul Slootman

More information about the rsync mailing list