Exemple #1
0
int gitfo_move_file(char *from, char *to)
{
	if (!link(from, to)) {
		gitfo_unlink(from);
		return GIT_SUCCESS;
	}

	if (!rename(from, to))
		return GIT_SUCCESS;

	return git_os_error();
}
Exemple #2
0
int remove_object_files(const char *odb_dir, object_data *d)
{
	if (gitfo_unlink(d->file) < 0) {
		fprintf(stderr, "can't delete object file \"%s\"\n", d->file);
		return -1;
	}
	if ((gitfo_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) {
		fprintf(stderr, "can't remove object directory \"%s\"\n", d->dir);
		return -1;
	}

	if (gitfo_rmdir(odb_dir) < 0) {
		fprintf(stderr, "can't remove directory \"%s\"\n", odb_dir);
		return -1;
	}

	return 0;
}
Exemple #3
0
void git_filebuf_cleanup(git_filebuf *file)
{
    if (file->fd >= 0)
        gitfo_close(file->fd);

    if (file->fd >= 0 && file->path_lock && gitfo_exists(file->path_lock) == GIT_SUCCESS)
        gitfo_unlink(file->path_lock);

    if (file->digest)
        git_hash_free_ctx(file->digest);

    free(file->buffer);
    free(file->z_buf);

    deflateEnd(&file->zs);

    free(file->path_original);
    free(file->path_lock);
}
Exemple #4
0
void git_filebuf_cleanup(git_filebuf *file)
{
	if (file->fd >= 0)
		gitfo_close(file->fd);

	if (gitfo_exists(file->path_lock) == GIT_SUCCESS)
		gitfo_unlink(file->path_lock);

	if (file->digest)
		git_hash_free_ctx(file->digest);

	free(file->buffer);

#ifdef GIT_FILEBUF_THREADS
	free(file->buffer_back);
#endif

	free(file->path_original);
	free(file->path_lock);
}
Exemple #5
0
int gitfo_mv(const char *from, const char *to)
{
#ifdef GIT_WIN32
	/*
	 * Win32 POSIX compilance my ass. If the destination
	 * file exists, the `rename` call fails. This is as
	 * close as it gets with the Win32 API.
	 */
	return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING) ? GIT_SUCCESS : GIT_EOSERR;
#else
	/* Don't even try this on Win32 */
	if (!link(from, to)) {
		gitfo_unlink(from);
		return GIT_SUCCESS;
	}

	if (!rename(from, to))
		return GIT_SUCCESS;

	return GIT_EOSERR;
#endif
}
Exemple #6
0
static int lock_file(git_filebuf *file, int flags)
{
    if (gitfo_exists(file->path_lock) == 0) {
        if (flags & GIT_FILEBUF_FORCE)
            gitfo_unlink(file->path_lock);
        else
            return git__throw(GIT_EOSERR, "Failed to lock file");
    }

    /* create path to the file buffer is required */
    if (flags & GIT_FILEBUF_FORCE) {
        file->fd = gitfo_creat_locked_force(file->path_lock, 0644);
    } else {
        file->fd = gitfo_creat_locked(file->path_lock, 0644);
    }

    if (file->fd < 0)
        return git__throw(GIT_EOSERR, "Failed to create lock");

    if ((flags & GIT_FILEBUF_APPEND) && gitfo_exists(file->path_original) == 0) {
        git_file source;
        char buffer[2048];
        size_t read_bytes;

        source = gitfo_open(file->path_original, O_RDONLY);
        if (source < 0)
            return git__throw(GIT_EOSERR, "Failed to lock file. Could not open %s", file->path_original);

        while ((read_bytes = gitfo_read(source, buffer, 2048)) > 0) {
            gitfo_write(file->fd, buffer, read_bytes);
            if (file->digest)
                git_hash_update(file->digest, buffer, read_bytes);
        }

        gitfo_close(source);
    }

    return GIT_SUCCESS;
}
Exemple #7
0
int remove_loose_object(const char *repository_folder, git_object *object)
{
	static const char *objects_folder = "objects/";

	char *ptr, *full_path, *top_folder;
	int path_length, objects_length;

	assert(repository_folder && object);

	objects_length = strlen(objects_folder);
	path_length = strlen(repository_folder);
	ptr = full_path = git__malloc(path_length + objects_length + GIT_OID_HEXSZ + 3);

	strcpy(ptr, repository_folder);
	strcpy(ptr + path_length, objects_folder);

	ptr = top_folder = ptr + path_length + objects_length;
	*ptr++ = '/';
	git_oid_pathfmt(ptr, git_object_id(object));
	ptr += GIT_OID_HEXSZ + 1;
	*ptr = 0;

	if (gitfo_unlink(full_path) < 0) {
		fprintf(stderr, "can't delete object file \"%s\"\n", full_path);
		return -1;
	}

	*top_folder = 0;

	if ((gitfo_rmdir(full_path) < 0) && (errno != ENOTEMPTY)) {
		fprintf(stderr, "can't remove object directory \"%s\"\n", full_path);
		return -1;
	}

	free(full_path);

	return GIT_SUCCESS;
}
Exemple #8
0
static int lock_file(git_filebuf *file, int flags)
{
	if (gitfo_exists(file->path_lock) == 0) {
		if (flags & GIT_FILEBUF_FORCE)
			gitfo_unlink(file->path_lock);
		else
			return GIT_EOSERR;
	}

	file->fd = gitfo_creat(file->path_lock, 0644);

	if (file->fd < 0)
		return GIT_EOSERR;

	/* TODO: do a flock() in the descriptor file_lock */

	if ((flags & GIT_FILEBUF_APPEND) && gitfo_exists(file->path_original) == 0) {
		git_file source;
		char buffer[2048];
		size_t read_bytes;

		source = gitfo_open(file->path_original, O_RDONLY);
		if (source < 0)
			return GIT_EOSERR;

		while ((read_bytes = gitfo_read(source, buffer, 2048)) > 0) {
			gitfo_write(file->fd, buffer, read_bytes);
			if (file->digest)
				git_hash_update(file->digest, buffer, read_bytes);
		}

		gitfo_close(source);
	}

	return GIT_SUCCESS;
}