Example #1
0
/*
 * Return the name of the new backup file for file FILE, allocated with
 * malloc.  Return 0 if out of memory. FILE must not end with a '/' unless it
 * is the root directory. Do not call this function if backup_type == none.
 */
char *
find_backup_file_name(const char *file)
{
	char	*dir, *base_versions, *tmp_file;
	int	highest_backup;

	if (backup_type == simple)
		return concat(file, simple_backup_suffix);
	tmp_file = strdup(file);
	if (tmp_file == NULL)
		return NULL;
	base_versions = concat(basename(tmp_file), ".~");
	free(tmp_file);
	if (base_versions == NULL)
		return NULL;
	tmp_file = strdup(file);
	if (tmp_file == NULL) {
		free(base_versions);
		return NULL;
	}
	dir = dirname(tmp_file);
	if (dir == NULL) {
		free(base_versions);
		free(tmp_file);
		return NULL;
	}
	highest_backup = max_backup_version(base_versions, dir);
	free(base_versions);
	free(tmp_file);
	if (backup_type == numbered_existing && highest_backup == 0)
		return concat(file, simple_backup_suffix);
	return make_version_name(file, highest_backup + 1);
}
Example #2
0
char *
find_backup_file_name (char *file)
{
  char *dir;
  char *base_versions;
  int highest_backup;

  if (backup_type == simple)
    return concat (file, simple_backup_suffix);
  base_versions = concat (basename (file), ".~");
  if (base_versions == 0)
    return 0;
  dir = dirname (file);
  if (dir == 0)
    {
      free (base_versions);
      return 0;
    }
  highest_backup = max_backup_version (base_versions, dir);
  free (base_versions);
  free (dir);
  if (backup_type == numbered_existing && highest_backup == 0)
    return concat (file, simple_backup_suffix);
  return make_version_name (file, highest_backup + 1);
}
Example #3
0
char *
find_backup_file_name(char *file)
{
  char *dir;
  char *base_versions;
  int highest_backup;

  if (backup_type == simple)
    {
      char *s = malloc (strlen (file) + strlen (simple_backup_suffix) + 1);
      strcpy (s, file);
      addext (s, simple_backup_suffix, '~');
      return s;
    }
  base_versions = concat (basename (file), ".~");
  if (base_versions == 0)
    return 0;
  dir = dirname (file);
  if (dir == 0)
    {
      free (base_versions);
      return 0;
    }
  highest_backup = max_backup_version (base_versions, dir);
  free (base_versions);
  free (dir);
  if (backup_type == numbered_existing && highest_backup == 0)
    return concat (file, simple_backup_suffix);
  return make_version_name (file, highest_backup + 1);
}
char *
find_backup_file_name (const char *file, enum backup_type backup_type)
{
  size_t backup_suffix_size_max;
  size_t file_len = strlen (file);
  size_t numbered_suffix_size_max = INT_STRLEN_BOUND (int) + 4;
  char *s;
  const char *suffix = simple_backup_suffix;

  /* Allow room for simple or `.~N~' backups.  */
  backup_suffix_size_max = strlen (simple_backup_suffix) + 1;
  if (HAVE_DIR && backup_suffix_size_max < numbered_suffix_size_max)
    backup_suffix_size_max = numbered_suffix_size_max;

  s = malloc (file_len + backup_suffix_size_max + numbered_suffix_size_max);
  if (s)
    {
      strcpy (s, file);

#if HAVE_DIR
      if (backup_type != simple)
	{
	  int highest_backup;
	  size_t dir_len = basename (s) - s;

	  strcpy (s + dir_len, ".");
	  highest_backup = max_backup_version (file + dir_len, s);
	  if (! (backup_type == numbered_existing && highest_backup == 0))
	    {
	      char *numbered_suffix = s + (file_len + backup_suffix_size_max);
	      sprintf (numbered_suffix, ".~%d~", highest_backup + 1);
	      suffix = numbered_suffix;
	    }
	  strcpy (s, file);
	}
#endif /* HAVE_DIR */

      addext (s, suffix, '~');
    }
  return s;
}