/*
  To be compatible with legacy USTAR implementations, we try fill
  the "name" fields with as much as we can, and only use the
  "prefix" field in case we need it.

  Only time we step away from this is in the case where the last
  part of the path is larger than NAME_FIELD_SIZE but all of the
  path fits into the "prefix" field. Might be that some other USTAR
  implementations can't read that, but it is within the USTAR
  standard.
*/

static union block *
write_ustar_long_name (const char *name)
{
  size_t length = strlen (name);
  union block *header;

  /*
    Most obvious overflow case, but note that even if we are within
    the 256 character limit, we can still fail to fit the path into
    the header
  */
  if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
    {
      ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
	      quotearg_colon (name),
	      PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
      return NULL;
    }

  /* Clear the memory block for the header */
  header = find_next_block ();
  memset (header->buffer, 0, sizeof (header->buffer));

  /*
    The most simple successful case, we would not need the "prefix" at
    all, as the complete path fits into the "name" field
  */
  if (length <= NAME_FIELD_SIZE)
    {
      memcpy (header->header.name, name, length);
      return header;
    }

  /*
    We try split, placing as much as possible into the "name" field.

    We search for the last slash when still within the "name" field
    size, split there, and check if we can store the first part into
    the "prefix" field.
  */
  {
    size_t prefix_length, name_length, name_start;
    size_t i, last_slash = 0;

    /*
      When we split we remove the implicit slash between the parts we
      put into the "prefix" field and the "name" field. This causes two
      problems.

      If the last part of the path is exactly 101 characters including
      a trailing slash, then we end up with 100 characters in the "name"
      field and an empty "prefix" field, in effect making the absolute
      path become a relative one. We solve this problem by not visiting
      index 0 in the loop below.

      If the path is a directory and exactly 101 characters in size
      including the ending slash, we hit a similar problem. We get 99
      characters into the "prefix" field, and an empty "name" field.
      The empty "name" field will confuse some USTAR implementations.
      We solve this by never visiting the last index in the loop below.

      FIXME if "//" is the same as "/", we could of course put a slash
      into the field the is empty in the cases described above, would
      likely fool other USTAR implementations to unpack correctly.
    */
    for (i = length - 2; i > 0 && (length - i - 1) <=  NAME_FIELD_SIZE; i--) {
      if (ISSLASH (name[i]))
	last_slash = i;
      /*      printf("i = %d\n", i);*/
    }

    prefix_length = last_slash;
    name_start    = last_slash + 1;
    name_length   = length - name_start;

    if (last_slash != 0 &&
	prefix_length <= PREFIX_FIELD_SIZE &&
	name_length <= NAME_FIELD_SIZE)
      {
	memcpy (header->header.prefix, name,              prefix_length);
	memcpy (header->header.name,   name + name_start, name_length);
	return header;
      }
  }

  /*
    Unfortunate case, we could not split the path, yet the complete
    path fits into the "prefix" field, but not the "name" field. This
    will not be readable by some USTAR implementations.

    FIXME maybe remove it then, and fail?!
  */

  if (length > NAME_FIELD_SIZE && length <= PREFIX_FIELD_SIZE)
    {
      memcpy (header->header.prefix, name, length);
      return header;
    }

  ERROR ((0, 0,
	  _("%s: file name is too long (cannot be split); not dumped"),
	  quotearg_colon (name)));
  return NULL;			/* We failed fit the name into the header */
}

