Esempio n. 1
0
static int storage_clear(const bt_addr_le_t *addr)
{
	char path[STORAGE_PATH_MAX];
	int err;

	if (addr) {
#if MAX_FILE_NAME >= STORAGE_FILE_NAME_LEN
		snprintk(path, STORAGE_PATH_MAX,
			 STORAGE_ROOT "/%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X%u",
			 addr->a.val[5], addr->a.val[4], addr->a.val[3],
			 addr->a.val[2], addr->a.val[1], addr->a.val[0],
			 addr->type);

		return unlink_recursive(path);
#else
		return -ENAMETOOLONG;
#endif
	}

	/* unlink_recursive() uses the given path as a buffer for
	 * constructing sub-paths, so we can't give it a string literal
	 * such as STORAGE_ROOT directly.
	 */
	strcpy(path, STORAGE_ROOT);

	err = unlink_recursive(path);
	if (err) {
		return err;
	}

	return fs_mkdir(STORAGE_ROOT);
}
Esempio n. 2
0
static INT64_T chirp_fs_local_rmall(const char *path)
{
	PREAMBLE("rmall(`%s')", path);
	RESOLVE(path)
	rc = unlink_recursive(path);
	PROLOGUE
}
Esempio n. 3
0
static int do_rm(int argc, char *argv[])
{
	int i, opt, recursive = 0;

	while ((opt = getopt(argc, argv, "r")) > 0) {
		switch (opt) {
		case 'r':
			recursive = 1;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (argc < 2)
		return COMMAND_ERROR_USAGE;

	i = optind;

	while (i < argc) {
		int ret;

		if (recursive)
			ret = unlink_recursive(argv[i], NULL);
		else
			ret = unlink(argv[i]);
		if (ret) {
			printf("could not remove %s: %s\n", argv[i], errno_str());
			return 1;
		}
		i++;
	}

	return 0;
}
Esempio n. 4
0
jboolean native_remove(JNIEnv* env, jclass clazz, jstring name) {
    if (!name)
        return JNI_FALSE;
    const char *path = (*env)->GetStringUTFChars(env, name, NULL);
    LOGI("native_remove %s", path);
    return unlink_recursive(path) >= 0;
}
Esempio n. 5
0
int uninstall_grub2(lickdir_t *lick) {
    char drive = mount_uefi_partition();
    if(drive == '\0') {
        if(!lick->err)
            lick->err = strdup2("Could not mount EFI partition.");
        return 0;
    }

    if(!fix_grub2_inner(lick, GRUB2_FIX_UNINSTALL, drive)) {
        unmount_uefi_partition(drive);
        if(!lick->err)
            lick->err = strdup2("Could not revert boot fix.");
        return 0;
    }

    char *grub_cfg = unix_path(concat_strs(2, lick->drive, "/lickgrub.cfg"));
    if(!has_valuable_info(grub_cfg)) {
        unlink_file(grub_cfg);
    }
    free(grub_cfg);

    char *lick_cert = strdup2("?:/lick.cer");
    lick_cert[0] = drive;
    unlink_file(lick_cert);
    free(lick_cert);

    char lick_dir[] = "?:/EFI/LICK";
    lick_dir[0] = drive;
    unlink_recursive(lick_dir);
    unmount_uefi_partition(drive);

    return 1;
}
static int sandbox_delete (const char *sandbox)
{
	int rc;

	CATCHUNIX(unlink_recursive(sandbox));

	rc = 0;
	goto out;
out:
	return rc;
}
Esempio n. 7
0
bool unlink_recursive(const char *path, bool include_self)
{
  if (!path) {
    return false;
  }
  
  DIR *dir;
  struct dirent *e;
  dir = opendir(path);
  if (!dir) {
    return false;
  }
  
  char *item = NULL;
  while ((e = readdir(dir)) != NULL) {
    if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) {
      continue;
    }
    
    item = string_concat(path, "/", e->d_name, NULL);
    if (!item) {
      goto fail;
    }
    
    if (e->d_type == DT_REG) {
      if (unlink(item) != 0) {
        goto fail;
      }
    
    } else if (e->d_type == DT_DIR) {
      if (unlink_recursive(item, true) != 0) {
        goto fail;
      }
    }
    
    free(item);
    item = NULL;
  }
  
  closedir(dir);
  
  if (include_self && rmdir(path) != 0) {
    return false;
  }
  
  return true;

fail:
  if (item) {
    free(item);
  }
  closedir(dir);
  return false;
}
Esempio n. 8
0
static int unlink_recursive(char path[STORAGE_PATH_MAX])
{
	size_t path_len;
	fs_dir_t dir;
	int err;

	err = fs_opendir(&dir, path);
	if (err) {
		return err;
	}

	/* We calculate this up-front so we can keep reusing the same
	 * buffer for the path when recursing.
	 */
	path_len = strlen(path);

	while (1) {
		struct fs_dirent entry;

		err = fs_readdir(&dir, &entry);
		if (err) {
			break;
		}

		if (entry.name[0] == '\0') {
			break;
		}

		snprintk(path + path_len, STORAGE_PATH_MAX - path_len, "/%s",
			 entry.name);

		if (entry.type == FS_DIR_ENTRY_DIR) {
			err = unlink_recursive(path);
		} else {
			err = fs_unlink(path);
		}

		if (err) {
			break;
		}
	}

	fs_closedir(&dir);

	/* Return to the original value */
	path[path_len] = '\0';

	fs_unlink(path);

	return err;
}
Esempio n. 9
0
/* return -1 on failure, with errno set to the first error */
static int unlink_recursive(const char* name, int flags)
{
    struct stat st;
    DIR *dir;
    struct dirent *de;
    int fail = 0;

    /* is it a file or directory? */
    if (stat(name, &st) < 0)
        return ((flags & OPT_FORCE) && errno == ENOENT) ? 0 : -1;

    /* a file, so unlink it */
    if (!S_ISDIR(st.st_mode))
        return unlink(name);

    /* a directory, so open handle */
    dir = opendir(name);
    if (dir == NULL)
        return -1;

    /* recurse over components */
    errno = 0;
    while((de = readdir(dir)) != NULL) {
        char dn[PATH_MAX];
        if(strcmp(de->d_name, "..") == 0 || strcmp(de->d_name, ".") == 0) {
            continue;
	}
        sprintf(dn, "%s/%s", name, de->d_name);
        if(unlink_recursive(dn, flags) < 0) {
            fail = 1;
            break;
        }
        errno = 0;
    }
    /* in case readdir or unlink_recursive failed */
    if (fail || errno) {
        int save = errno;
        closedir(dir);
        errno = save;
        return -1;
    }

    /* close directory handle */
    if (closedir(dir) < 0)
        return -1;

    /* delete target directory */
    return rmdir(name);
}
Esempio n. 10
0
int rm_main(int argc, char *argv[])
{
    int ret;
    int i, c;
    int flags = 0;

    if(argc < 2) return usage();

    /* check flags */
    while(1) {
        c = getopt(argc, argv, "frR");
        if(c == EOF) break;
        switch (c) {
        case 'f':
            flags |= OPT_FORCE;
            break;
        case 'r':
        case 'R':
            flags |= OPT_RECURSIVE;
            break;
        }
    }

    if(optind < 1 || optind >= argc) {
        usage();
        return -1;
    }

    /* loop over the file/directory args */
    for(i = optind; i < argc; i++) {

        if(flags & OPT_RECURSIVE) {
            ret = unlink_recursive(argv[i], flags);
        } else {
            ret = unlink(argv[i]);
        }

        if(ret < 0 && (errno != ENOENT || !(flags & OPT_FORCE))) {
            fprintf(stderr, "rm failed for %s, %s\n", argv[i], strerror(errno));
            return -1;
        }
    }

    return 0;
}
/* Remove partial or corrupted archive.
@return 1 if archive was successful, 0 if archive failed.
 */
static int makeflow_archive_remove_task(struct archive_instance *a, struct dag_node *n, struct batch_task *t) {
	/* Generate the task id */
	char *id = batch_task_generate_id(t);

	/* The archive name is binned by the first 2 characters of the id for compactness */
	char *archive_directory_path = string_format("%s/tasks/%.2s/%s", a->dir, id, id);
	debug(D_MAKEFLOW_HOOK, "removing corrupt archive for task %d at %s", t->taskid, archive_directory_path);
	free(id);

	if(!unlink_recursive(archive_directory_path)){
		debug(D_MAKEFLOW_HOOK, "unable to remove corrupt archive for task %d", t->taskid);
		free(archive_directory_path);
		return 0;
	}

	debug(D_MAKEFLOW_HOOK, "corrupt archive for task %d removed", t->taskid);

	free(archive_directory_path);
	return 1;
}
Esempio n. 12
0
bench_t* bm_init(void *param){
		bench_t* bp = (bench_t*)param;
		bench_t* bp_new;

		if(bp->mode == 0x0)
				bp->mode = BM_MODE_COUNTER;

		bp->processes = bp->num / BM_GROUP_THREADS;
		bp->groupthreads = BM_GROUP_THREADS;
		if(bp->num % BM_GROUP_THREADS > 0)
				bp->processes += 1;

		/*
		 * 通过pmalloc调用mmap
		 * 分配bench_child_t的空间
		 * bench_child_t的数量取决于子进程数
		 */
		pmalloc(sizeof(bench_t) + sizeof(bench_child_t)*(bp->processes),(void **)&mbp);
		memset(mbp,0,sizeof(bench_t) + sizeof(bench_child_t)*(bp->processes));
		memcpy(&mbp->bp,bp,sizeof(bench_t));

		INIT_LIST_HEAD(&mbp->bp.children);

		/*
		 * 	如果日志输出目录已经存在
		 * 	就删除掉
		 */
		int ret = 0;
		if(bp->outfile){
				//			ret = rmdir(bp->outfile);
				ret = unlink_recursive(bp->outfile);
				if(ret != 0){
						syslog(LOG_ERR,"rmdir %s faild - %m",bp->outfile);
				}
		}

		mbp->bp.pid = getpid();///<获得主进程pid
		return &mbp->bp;
}
Esempio n. 13
0
static void unlink_recursive (const char *dirpath)
{
    DIR *dir;
    struct dirent *dirent;
    char path[PATH_MAX + 1];
    struct stat sb;

    if ((dir = opendir (dirpath))) {
        while ((dirent = readdir (dir))) {
            if (!strcmp (dirent->d_name, ".") || !strcmp (dirent->d_name, ".."))
                continue;
            snprintf (path, sizeof (path), "%s/%s", dirpath, dirent->d_name);
            if (lstat (path, &sb) < 0)
                continue;
            if (S_ISDIR (sb.st_mode))
                unlink_recursive (path);
            else
                unlink (path);
        }
        closedir (dir);
        rmdir (dirpath);
    }
}
Esempio n. 14
0
int makeflow_clean_mount_target(const char *target) {
	file_type t_type;

	if(!target || !*target) return 0;

	/* Check whether target already exists. */
	if(access(target, F_OK)) {
		debug(D_DEBUG, "the target (%s) does not exist!\n", target);
		return 0;
	}

	/* Check whether the target is an absolute path. */
	if(target[0] == '/') {
		debug(D_DEBUG, "the target (%s) should not be an absolute path!\n", target);
		fprintf(stderr, "the target (%s) should not be an absolute path!\n", target);
		return -1;
	}

	/* check whether target includes .. */
	if(path_has_doubledots(target)) {
		debug(D_DEBUG, "the target (%s) include ..!\n", target);
		fprintf(stderr, "the target (%s) include ..!\n", target);
		return -1;
	}

	/* check whether target is REG, LNK, DIR */
	if((t_type = check_file_type(target)) == FILE_TYPE_UNSUPPORTED)
		return -1;

	if(unlink_recursive(target)) {
		debug(D_DEBUG, "Failed to remove %s!\n", target);
		fprintf(stderr, "Failed to remove %s!\n", target);
		return -1;
	}

	return 0;
}
Esempio n. 15
0
int main(int argc, char *argv[])
{
	int ret;
	int i;
	int flags = 0;

	if(argc < 2) {
		usage();
		return -1;
	}

	/* check flags */
	while(1) {
		int c = getopt(argc, argv, "frR");
		if(c == -1) break;
		switch (c) {
			case 'f':
				flags |= OPT_FORCE;
				break;
			case 'r':
			case 'R':
				flags |= OPT_RECURSIVE;
				break;
		}
	}

	if(optind < 1 || optind >= argc) {
		usage();
		return -1;
	}

	/* loop over the file/directory args */
	for (i = optind; i < argc; i++) {

		if (flags & OPT_RECURSIVE) {
			ret = unlink_recursive(argv[i], flags);
		} else {
//#ifdef _WIN32
			if(access(argv[i], F_OK) == 0 && access(argv[i], W_OK) != 0) {
				//if(flags & OPT_FORCE) chmod(argv[i], 666);
				if(!(flags & OPT_FORCE)) {
					//int a;
					printf("remove write-protected file \"%s\"? ", argv[i]);
/*
					a = getchar();
					if(a == 'Y' || a == 'y') chmod(argv[i], 666);
					else if(a == 'N' || a == 'n') return 1;
					else {
						fprintf(stderr, "Illegal input\n");
						return 1;
					}
*/
					switch(getchar()) {
						default:
							fprintf(stderr, "Illegal input\n");
						case 'N':
						case 'n':
							return 1;
						case 'Y':
						case 'y':
#if defined _WIN32 && !defined _WIN32_WNT_NATIVE
							chmod(argv[i], 666);
#endif
							break;
					}
				}
#if defined _WIN32 && !defined _WIN32_WNT_NATIVE
				else chmod(argv[i], 666);
#endif
			}
//#endif
			ret = unlink(argv[i]);
/*
			if (errno == ENOENT && !(flags & OPT_FORCE)) {
				return 0;
			}
*/
		}

		if(ret < 0 && (errno != ENOENT || !(flags & OPT_FORCE))) {
//#ifdef _WIN32_WCE
//			fprintf(stderr, "rm failed for %s\n", argv[i]);
//#else
			fprintf(stderr, "rm failed for %s, %s\n", argv[i], strerror(errno));
//#endif
			//if(errno != ENOENT || !(flags & OPT_FORCE)) return -1;
			return -1;
		}
	}

	return 0;
}
Esempio n. 16
0
void *linux_bootchart_thread (void *ignored) {
    struct cfgnode *node;
    char *save_to = "/var/log/bootchart.tgz";
    FILE *f;
    char try_acct = 1;
    signed int extra_wait = 0;

    if ((node = cfg_getnode ("configuration-bootchart-extra-waiting-time", NULL)) && node->value) {
        extra_wait = node->value;
    }

    char *buffer_ds = NULL;
    char *buffer_ps = NULL;
    char *buffer_st = NULL;

    while (!shutting_down && (linux_bootchart_have_thread || (extra_wait > 0))) {
        char *uptime = linux_bootchart_get_uptime();

        if (linux_bootchart_process_accounting && try_acct) {
            if (acct ("/dev/kernel_pacct") == -1)
                try_acct = 1;
        }

        if (uptime) {
            buffer_ds = linux_bootchart_update_ds (buffer_ds, uptime);
            buffer_ps = linux_bootchart_update_ps (buffer_ps, uptime);
            buffer_st = linux_bootchart_update_st (buffer_st, uptime);

            free (uptime);
            uptime = NULL;
        }

        usleep (linux_bootchart_sleep_time);
        if (!linux_bootchart_have_thread)
            extra_wait -= linux_bootchart_sleep_time;
    }

    if ((node = cfg_getnode ("configuration-bootchart-save-to", NULL)) && node->svalue) {
        save_to = node->svalue;
    }

    if (coremode & einit_mode_sandbox) {
        save_to = "bootchart.tgz";
    }

    mkdir ("/tmp/bootchart.einit", 0755);

    if (buffer_ds) {
        if ((f = fopen ("/tmp/bootchart.einit/proc_diskstats.log", "w"))) {
            fputs (buffer_ds, f);

            fclose (f);
        }

        free (buffer_ds);
        buffer_ds = NULL;
    }

    if (buffer_ps) {
        if ((f = fopen ("/tmp/bootchart.einit/proc_ps.log", "w"))) {
            fputs (buffer_ps, f);

            fclose (f);
        }

        free (buffer_ps);
        buffer_ps = NULL;
    }

    if (buffer_st) {
        if ((f = fopen ("/tmp/bootchart.einit/proc_stat.log", "w"))) {
            fputs (buffer_st, f);

            fclose (f);
        }

        free (buffer_st);
        buffer_st = NULL;
    }

    if (linux_bootchart_process_accounting) {
        char *r = readfile ("/dev/kernel_pacct");
        if (r) {
            if ((f = fopen ("/tmp/bootchart.einit/kernel_pacct", "w"))) {
                fputs (r, f);

                fclose (f);
            }

            unlink ("/dev/kernel_pacct");
        }

        acct(NULL);
    }

    if ((f = fopen ("/tmp/bootchart.einit/header", "w"))) {
        char *t, buffer[BUFFERSIZE];
        time_t ti = time(NULL);
        /* we're emulating bootchartd-0.8/0.9's format... */
        eputs ("version = 0.8\n", f);

        if (gethostname (buffer, BUFFERSIZE) == 0) {
            eprintf (f, "title = eINIT Boot Chart for %s, %s", buffer, ctime(&ti));
        } else {
            eprintf (f, "title = eINIT Boot Chart, %s", ctime(&ti));
        }

        fprintf (f, "system.uname = %s %s %s %s\n", osinfo.sysname, osinfo.release, osinfo.version, osinfo.machine);

        if ((t = readfile ("/etc/gentoo-release"))) {
            strtrim (t);
            eprintf (f, "system.release = %s\n", t);
            free (t);
        } else {
            eputs ("system.release = unknown\n", f);
        }

        if ((t = readfile ("/proc/cpuinfo"))) {
            char **r = str2set ('\n', t);
            char *n = NULL;
            int i;

            if (r) {
                for (i = 0; r[i]; i++) {
                    if (strstr (r[i], "model name") == r[i]) {
                        n = r[i];
                        break;
                    }
                }

                if (n)
                    eprintf (f, "system.cpu = %s\n", n);
                else
                    eputs ("system.cpu = unknown\n", f);
            }

            free (t);
        } else {
            eputs ("system.cpu = unknown\n", f);
        }

        if ((t = readfile ("/proc/cmdline"))) {
            eprintf (f, "system.kernel.options = %s\n", t);
            free (t);
        }

        fclose (f);
    }

    char buffer[BUFFERSIZE];
    if (coremode & einit_mode_sandbox) {
        esprintf (buffer, BUFFERSIZE, "export pwx=`pwd`; cd /tmp/bootchart.einit; tar czf \"${pwx}/%s\" *", save_to);
    } else {
        esprintf (buffer, BUFFERSIZE, "cd /tmp/bootchart.einit; tar czf %s *", save_to);
    }
    system (buffer);

    unlink_recursive ("/tmp/bootchart.einit/", 1);

    char *di = cfg_getstring ("configuration-bootchart-chart-directory", NULL);
    char *fo = cfg_getstring ("configuration-bootchart-chart-format", NULL);
    esprintf (buffer, BUFFERSIZE, "bootchart -o %s -f %s %s", di, fo, save_to);

    return NULL;
}
Esempio n. 17
0
static int do_loadenv(int argc, char *argv[])
{
	char *filename = NULL, *dirname;
	unsigned flags = 0;
	int opt;
	int scrub = 0;
	int defaultenv = 0;

	while ((opt = getopt(argc, argv, "nsd")) > 0) {
		switch (opt) {
		case 'n':
			flags |= ENV_FLAG_NO_OVERWRITE;
			break;
		case 's':
			scrub = 1;
			break;
		case 'd':
			defaultenv = 1;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (argc - optind < 2)
		dirname = "/env";
	else
		dirname = argv[optind + 1];

	if (argc - optind < 1) {
		filename = default_environment_path_get();
	} else {
		char *str = normalise_path(argv[optind]);

		/*
		 * /dev/defaultenv use to contain the defaultenvironment.
		 * we do not have this file anymore, but maintain compatibility
		 * to the 'loadenv -s /dev/defaultenv' command to restore the
		 * default environment for some time.
		 */
		if (!strcmp(str, "/dev/defaultenv"))
			defaultenv = 1;
		else
			filename = argv[optind];

		free(str);
	}

	if (scrub) {
		int ret;

		ret = unlink_recursive(dirname, NULL);
		if (ret && ret != -ENOENT) {
			eprintf("cannot remove %s: %s\n", dirname,
					strerror(-ret));
			return 1;
		}

		ret = mkdir(dirname, 0);
		if (ret) {
			eprintf("cannot create %s: %s\n", dirname,
					strerror(-ret));
			return ret;
		}
	}

	printf("loading environment from %s\n", defaultenv ? "defaultenv" : filename);

	if (defaultenv)
		return defaultenv_load(dirname, flags);
	else
		return envfs_load(filename, dirname, flags);
}
Esempio n. 18
0
int makeflow_clean(struct dag *d, struct batch_queue *queue, makeflow_clean_depth clean_depth)//, struct makeflow_wrapper *w, struct makeflow_monitor *m)
{
	struct dag_file *f;
	char *name;

	hash_table_firstkey(d->files);
	while(hash_table_nextkey(d->files, &name, (void **) &f)) {
		int silent = 1;
		if(dag_file_should_exist(f))
			silent = 0;

		/* We have a record of the file, but it is no longer created or used so delete */
		if(dag_file_is_source(f) && dag_file_is_sink(f) && !set_lookup(d->inputs, f))
			makeflow_clean_file(d, queue, f, silent);

		if(dag_file_is_source(f)) {
			if(f->source && (clean_depth == MAKEFLOW_CLEAN_CACHE || clean_depth == MAKEFLOW_CLEAN_ALL)) { /* this file is specified in the mountfile */
				if(makeflow_clean_mount_target(f->filename)) {
					fprintf(stderr, "Failed to remove %s!\n", f->filename);
					return -1;
				}
			}
			continue;
		}

		if(clean_depth == MAKEFLOW_CLEAN_ALL) {
			makeflow_clean_file(d, queue, f, silent);
		} else if(set_lookup(d->outputs, f) && (clean_depth == MAKEFLOW_CLEAN_OUTPUTS)) {
			makeflow_clean_file(d, queue, f, silent);
		} else if(!set_lookup(d->outputs, f) && (clean_depth == MAKEFLOW_CLEAN_INTERMEDIATES)){
			makeflow_clean_file(d, queue, f, silent);
		}
	}

	/* clean up the cache dir created due to the usage of mountfile */
	if(clean_depth == MAKEFLOW_CLEAN_CACHE || clean_depth == MAKEFLOW_CLEAN_ALL) {
		if(d->cache_dir && unlink_recursive(d->cache_dir)) {
			fprintf(stderr, "Failed to clean up the cache dir (%s) created due to the usage of the mountfile!\n", d->cache_dir);
			dag_mount_clean(d);
			return -1;
		}
		dag_mount_clean(d);
	}

	struct dag_node *n;
	for(n = d->nodes; n; n = n->next) {
		/* If the node is a Makeflow job, then we should recursively call the *
		 * clean operation on it. */
		if(n->nested_job) {
			char *command = xxmalloc(sizeof(char) * (strlen(n->command) + 4));
			sprintf(command, "%s -c", n->command);

			/* XXX this should use the batch job interface for consistency */
			makeflow_node_export_variables(d, n);
			system(command);
			free(command);
		}
	}

	return 0;
}
Esempio n. 19
0
/**
 * Make the current environment persistent
 * @param[in] filename where to store
 * @param[in] dirname what to store (all files in this dir)
 * @param[in] flags superblock flags (refer ENVFS_FLAGS_* macros)
 * @return 0 on success, anything else in case of failure
 *
 * Note: This function will also be used on the host! See note in the header
 * of this file.
 */
int envfs_save(const char *filename, const char *dirname, unsigned flags)
{
	struct envfs_super *super;
	int envfd, size, ret;
	struct action_data data = {};
	void *buf = NULL, *wbuf;
	struct envfs_entry *env;

	if (!filename)
		filename = default_environment_path_get();

	if (!dirname)
		dirname = "/env";

	data.writep = NULL;
	data.base = dirname;

#ifdef __BAREBOX__
	defaultenv_load(TMPDIR, 0);
#endif

	if (flags & ENVFS_FLAGS_FORCE_BUILT_IN) {
		size = 0; /* force no content */
	} else {
		/* first pass: calculate size */
		recursive_action(dirname, ACTION_RECURSE, file_action,
				NULL, &data, 0);
		recursive_action("/.defaultenv", ACTION_RECURSE,
				file_remove_action, NULL, &data, 0);
		size = 0;

		for (env = data.env; env; env = env->next) {
			size += PAD4(env->size);
			size += sizeof(struct envfs_inode);
			size += PAD4(strlen(env->name) + 1);
			size += sizeof(struct envfs_inode_end);
		}
	}

	buf = xzalloc(size + sizeof(struct envfs_super));
	data.writep = buf + sizeof(struct envfs_super);

	super = buf;
	super->magic = ENVFS_32(ENVFS_MAGIC);
	super->major = ENVFS_MAJOR;
	super->minor = ENVFS_MINOR;
	super->size = ENVFS_32(size);
	super->flags = ENVFS_32(flags);

	if (!(flags & ENVFS_FLAGS_FORCE_BUILT_IN)) {
		/* second pass: copy files to buffer */
		env = data.env;
		while (env) {
			struct envfs_entry *next = env->next;

			envfs_save_inode(&data, env);

			free(env->buf);
			free(env->name);
			free(env);
			env = next;
		}
	}

	super->crc = ENVFS_32(crc32(0, buf + sizeof(struct envfs_super), size));
	super->sb_crc = ENVFS_32(crc32(0, buf, sizeof(struct envfs_super) - 4));

	envfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
	if (envfd < 0) {
		printf("could not open %s: %s\n", filename, errno_str());
		ret = -errno;
		goto out1;
	}

	ret = protect(envfd, ~0, 0, 0);

	/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
	if (ret && errno != ENOSYS && errno != EOPNOTSUPP) {
		printf("could not unprotect %s: %s\n", filename, errno_str());
		goto out;
	}

	ret = erase(envfd, ~0, 0);

	/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
	if (ret && errno != ENOSYS && errno != EOPNOTSUPP) {
		printf("could not erase %s: %s\n", filename, errno_str());
		goto out;
	}

	size += sizeof(struct envfs_super);

	wbuf = buf;

	while (size) {
		ssize_t now = write(envfd, wbuf, size);
		if (now < 0) {
			ret = -errno;
			goto out;
		}

		wbuf += now;
		size -= now;
	}

	ret = protect(envfd, ~0, 0, 1);

	/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
	if (ret && errno != ENOSYS && errno != EOPNOTSUPP) {
		printf("could not protect %s: %s\n", filename, errno_str());
		goto out;
	}

	ret = 0;

out:
	close(envfd);
out1:
	free(buf);
#ifdef __BAREBOX__
	unlink_recursive(TMPDIR, NULL);
#endif
	return ret;
}
Esempio n. 20
0
void cleanup_directory_recursive (const struct cleaner *c)
{
    if (c && c->arg)
        unlink_recursive (c->arg);
}
Esempio n. 21
0
int main(int argc, char *argv[])
{
	int port = WORK_QUEUE_DEFAULT_PORT;
	const char *port_file=0;
	const char *project_name=0;
	int monitor_flag = 0;
	int c;

	while((c = getopt(argc, argv, "d:o:mN:p:Z:vh"))!=-1) {
		switch (c) {
		case 'd':
			debug_flags_set(optarg);
			break;
		case 'o':
			debug_config_file(optarg);
			break;
		case 'p':
			port = atoi(optarg);
			break;
		case 'm':
			monitor_flag = 1;
			break;
		case 'N':
			project_name = optarg;
			break;
		case 'Z':
			port_file = strdup(optarg);
			port = 0;
			break;
		case 'v':
			cctools_version_print(stdout, argv[0]);
			return 0;
			break;
		case 'h':
			show_help(path_basename(argv[0]));
			return 0;
		default:
			show_help(path_basename(argv[0]));
			return 1;
		}
	}

	struct work_queue *q = work_queue_create(port);
	if(!q) fatal("couldn't listen on any port!");

	printf("listening on port %d...\n", work_queue_port(q));

	if(port_file) {
		FILE *file = fopen(port_file,"w");
		if(!file) fatal("couldn't open %s: %s",port_file,strerror(errno));
		fprintf(file,"%d\n",work_queue_port(q));
		fclose(file);
	}

	if(project_name) {
		work_queue_specify_name(q,project_name);
	}

	if(monitor_flag) {
		unlink_recursive("work-queue-test-monitor");
		work_queue_enable_monitoring(q, "work-queue-test-monitor");
		work_queue_specify_category_mode(q, NULL, WORK_QUEUE_ALLOCATION_MODE_MAX_THROUGHPUT);

		work_queue_specify_transactions_log(q, "work-queue-test-monitor/transactions.log");
	}


	int result = work_queue_mainloop(q);

	work_queue_delete(q);

	return result;
}