Esempio n. 1
0
int os_copy_no_clobber(const char *source_path, const char *dest_dir,
        const char *prefix, const char *dest_extension,
        ByteBuffer &out_path, Sha256Hasher *hasher)
{
    ByteBuffer dir_plus_prefix;
    os_path_join(dir_plus_prefix, dest_dir, prefix);

    ByteBuffer full_path;
    int out_fd;
    for (int counter = 0;; counter += 1) {
        full_path = dir_plus_prefix;
        if (counter != 0) {
            ByteBuffer counter_buf;
            counter_buf.format("%d", counter);
            full_path.append(counter_buf);
        }
        full_path.append(dest_extension);
        out_fd = open(full_path.raw(), O_CREAT|O_WRONLY|O_EXCL, 0660);
        if (out_fd == -1) {
            if (errno == EEXIST) {
                continue;
            } else if (errno == ENOMEM) {
                return GenesisErrorNoMem;
            } else {
                return GenesisErrorFileAccess;
            }
        }
        break;
    }

    FILE *out_f = fdopen(out_fd, "wb");
    if (!out_f) {
        close(out_fd);
        os_delete(full_path.raw());
        return GenesisErrorNoMem;
    }

    FILE *in_f = fopen(source_path, "rb");
    if (!in_f) {
        fclose(out_f);
        os_delete(full_path.raw());
        return GenesisErrorFileAccess;
    }

    int err;
    if ((err = copy_open_files(in_f, out_f, hasher))) {
        fclose(in_f);
        fclose(out_f);
        os_delete(full_path.raw());
        return err;
    }
    out_path = full_path;

    return 0;
}
Esempio n. 2
0
int os_copy(const char *source_path, const char *dest_path, Sha256Hasher *hasher) {
    FILE *in_f = fopen(source_path, "rb");
    if (!in_f)
        return GenesisErrorFileAccess;

    FILE *out_f = fopen(dest_path, "wb");
    if (!out_f) {
        fclose(in_f);
        return GenesisErrorFileAccess;
    }

    int err;
    if ((err = copy_open_files(in_f, out_f, hasher))) {
        fclose(in_f);
        fclose(out_f);
        os_delete(dest_path);
        return err;
    }

    return 0;
}
Esempio n. 3
0
File: rmbin.c Progetto: olebole/iraf
/* RMBIN -- Remove all binaries in a directory or in a directory tree.
 * We chdir to each directory to minimize path searches.
 */
static void
rmbin (
    char *dir, 
    int	  recurse, 
    char *path 			/* pathname of current directory */
)
{
	char	newpath[SZ_PATHNAME+1];
	char	fname[SZ_PATHNAME+1];
	int	dp, ftype;

	if ((dp = os_diropen (dir)) == ERR) {
	    fprintf (stderr, "cannot open directory `%s'\n", dir);
	    fflush (stderr);
	    return;
	}

	sprintf (newpath, "%s%s/", path, dir);

	/* Descend into the subdirectory.
	 */
	if (strcmp (dir, ".") != 0)
	    if (os_chdir (dir) == ERR) {
		os_dirclose (dp);
		fprintf (stderr, "cannot change directory to `%s'\n", newpath);
		fflush (stderr);
		return;
	    }

	/* Scan through the directory.
	 */
	while (os_gfdir (dp, fname, SZ_PATHNAME) > 0) {
	    if (os_symlink (fname, 0, 0))
		continue;

	    if ((ftype = os_filetype (fname)) == DIRECTORY_FILE)
		rmbin (fname, recurse, newpath);
	    else {
		if (only[0] != NULL) {
		    if (exclude_file (fname))
			continue;
		} else if (ftype != BINARY_FILE || exclude_file (fname))
		    continue;

		/* We have a binary file which is not excluded from deletion
		 * by its extension, so delete it.
		 */
		if (interactive && (verify_delete (fname, newpath) == NO))
		    continue;

		if (verbose) {
		    printf ("%s%s\n", newpath, fname);
		    fflush (stdout);
		}

		if (execute)
		    if (os_delete (fname) == ERR) {
			fprintf (stderr, "cannot delete `%s'\n", fname);
			fflush (stderr);
		    }
	    }
	}

	/* Return from the subdirectory.
	 */
	if (strcmp (dir, ".") != 0)
	    if (os_chdir ("..") == ERR) {
		fprintf (stderr, "cannot return from subdirectory `%s'\n",
		    newpath);
		fflush (stderr);
	    }

	os_dirclose (dp);
}