示例#1
0
TEST(QiOs, tmpdir_tmpdir)
{
  std::string temp1 = qi::os::mktmpdir();
  std::string temp2 = qi::os::mktmpdir();
  EXPECT_NE(temp1, temp2);
  clean_dir(temp1);
  clean_dir(temp2);
}
示例#2
0
// clean run directory
void preproc_clean_run(void) {
	int max_pids=32769;
	int start_pid = 100;
	// extract real max_pids
	FILE *fp = fopen("/proc/sys/kernel/pid_max", "r");
	if (fp) {
		int val;
		if (fscanf(fp, "%d", &val) == 1) {
			if (val > 4194304)	// this is the max value supported on 64 bit Linux kernels
				val = 4194304;
			if (val >= max_pids)
				max_pids = val + 1;
		}
		fclose(fp);
	}
	int *pidarr = malloc(max_pids * sizeof(int));
	if (!pidarr)
		errExit("malloc");

	memset(pidarr, 0, max_pids * sizeof(int));

	// open /proc directory
	DIR *dir;
	if (!(dir = opendir("/proc"))) {
		// sleep 2 seconds and try again
		sleep(2);
		if (!(dir = opendir("/proc"))) {
			fprintf(stderr, "Error: cannot open /proc directory\n");
			exit(1);
		}
	}

	// read /proc and populate pidarr with all active processes
	struct dirent *entry;
	char *end;
	while ((entry = readdir(dir)) != NULL) {
		pid_t pid = strtol(entry->d_name, &end, 10);
		pid %= max_pids;
		if (end == entry->d_name || *end)
			continue;

		if (pid < start_pid)
			continue;
		pidarr[pid] = 1;
	}
	closedir(dir);

	// clean profile and name directories
	clean_dir(RUN_FIREJAIL_PROFILE_DIR, pidarr, start_pid, max_pids);
	clean_dir(RUN_FIREJAIL_NAME_DIR, pidarr, start_pid, max_pids);

	free(pidarr);
}
int
main (int argc, char **argv)
{
  const char *prev_arg;
  int i;
  int saw_doubledash;
  const char *dirname;

  saw_doubledash = FALSE;
  dirname = NULL;
  prev_arg = NULL;
  i = 1;
  while (i < argc)
    {
      const char *arg = argv[i];

      if (strcmp (arg, "--help") == 0 ||
          strcmp (arg, "-h") == 0 ||
          strcmp (arg, "-?") == 0)
        usage (0);
      else if (strcmp (arg, "--version") == 0)
        version ();
      else if (!saw_doubledash)
	{
          if (strcmp (arg, "--") == 0)
            saw_doubledash = TRUE;
          else if (*arg == '-')
            usage (1);
	}
      else
        {
          if (dirname != NULL)
            {
              fprintf (stderr, "dbus-cleanup-sockets only supports a single directory name\n");
              exit (1);
            }

          dirname = arg;
        }

      prev_arg = arg;

      ++i;
    }

  /* Default to session socket dir, usually /tmp */
  if (dirname == NULL)
    dirname = DBUS_SESSION_SOCKET_DIR;

#ifdef AF_UNIX
  clean_dir (dirname);

  printf ("Cleaned up %d sockets in %s; %d sockets are still in use; %d in unknown state\n",
          cleaned_count, dirname, alive_count, unhandled_count);
#else
  printf ("This system does not support UNIX domain sockets, so dbus-cleanup-sockets does nothing\n");
#endif

  return 0;
}
示例#4
0
static void cd_comics_dir(int clean)
{
	if (!comics_dir) {
#ifdef WIN32
		char *homedrive = getenv("HOMEDRIVE");
		char *homepath = getenv("HOMEPATH");
		char home[64];
		snprintf(home, sizeof(home), "%s%s", homedrive, homepath);
#else
		char *home = getenv("HOME");
#endif

		if (home) {
			comics_dir = must_alloc(strlen(home) + 10);
			sprintf(comics_dir, "%s/comics", home);
		} else
			comics_dir = must_strdup("comics");
	}

	if (chdir(comics_dir)) {
		my_perror(comics_dir);
		exit(1);
	}

	if (clean)
		clean_dir();
}
示例#5
0
TEST(QiOs, tmpdir_prefix_accentuated)
{
  char utf8[]     = { (char) 0xC5, (char) 0xAA, (char) 0x6E, (char) 0xC4, (char) 0xAD, (char) 0x63, (char) 0xC5, (char) 0x8D, (char) 0x64, (char) 0x65, (char) 0xCC, (char) 0xBD, (char) 0 };

  std::string temp = qi::os::mktmpdir(utf8);

  test_writable_and_empty(temp);
  clean_dir(temp);
}
示例#6
0
TEST(QiOs, tmpdir_parent)
{
  std::string temp = qi::os::mktmpdir("plaf");

  boost::filesystem::path ppa(temp, qi::unicodeFacet());
  ppa = ppa.parent_path();

  boost::filesystem::path ppatmp(qi::os::tmp(), qi::unicodeFacet());
  EXPECT_TRUE(boost::filesystem::equivalent(ppa, ppatmp));

  clean_dir(temp);
}
示例#7
0
TEST(QiOs, tmpdir_prefix)
{
  std::string temp = qi::os::mktmpdir("plaf");

  test_writable_and_empty(temp);

  boost::filesystem::path pp(temp, qi::unicodeFacet());
  std::string tempfname = pp.filename().string(qi::unicodeFacet());

  EXPECT_EQ(tempfname[0], 'p');
  EXPECT_EQ(tempfname[1], 'l');
  EXPECT_EQ(tempfname[2], 'a');
  EXPECT_EQ(tempfname[3], 'f');

  clean_dir(temp);
}
示例#8
0
文件: bccemu.c 项目: NUOG/ejudge
int
main(int argc, char *argv[])
{
  FILE *f;
  char buf[1024];
  int pid, stat;
  struct stat ss;
  int fd;

  if (argc != 3) myerr("wrong number of arguments: %d", argc);

#if defined EJUDGE_LOCAL_DIR
  snprintf(emupath, sizeof(emupath), "%s/dosemu/run", EJUDGE_LOCAL_DIR);
#elif defined EJUDGE_CONTESTS_HOME_DIR
  snprintf(emupath, sizeof(emupath), "%s/dosemu/run", EJUDGE_CONTESTS_HOME_DIR);
#else
  snprintf(emupath, sizeof(emupath), "/home/judges/dosemu/run");
#endif

  atexit(cleanup_hnd);
  if (chmod(emupath, 0700) < 0) myerr("chmod failed: %s", strerror(errno));
  clean_dir(emupath);
  snprintf(buf, sizeof(buf), "%s/command.txt", emupath);
  if (!(f = fopen(buf, "w")))
    myerr("fopen w failed on %s: %s", buf, strerror(errno));
  fprintf(f,
          "\r\n"
          "\r\n"
          "output.txt\r\n"
          "errors.txt\r\n"
          "bcc\r\n"
          "-ml\r\n"
          "program.c\r\n");
  fclose(f);

  snprintf(buf, sizeof(buf), "%s/output.txt", emupath);
  if (!(f = fopen(buf, "w")))
    myerr("fopen failed on %s: %s", buf, strerror(errno));
  fclose(f);
  snprintf(buf, sizeof(buf), "%s/errors.txt", emupath);
  if (!(f = fopen(buf, "w")))
    myerr("fopen failed on %s: %s", buf, strerror(errno));
  fclose(f);

  snprintf(buf, sizeof(buf), "%s/program.c", emupath);
  copy_file(argv[1], buf);
  fflush(0);

  if ((pid = fork()) < 0) myerr("fork failed: %s", strerror(errno));
  if (!pid) {
    snprintf(buf, sizeof(buf), "%s/../bin/dos", emupath);
    if ((fd = open("/dev/null", O_RDONLY)) < 0)
      myerr("open(/dev/null failed: %s", strerror(errno));
    dup2(fd, 0);
    close(fd);
    if ((fd = open("/dev/null", O_WRONLY)) < 0)
      myerr("open(/dev/null failed: %s", strerror(errno));
    dup2(fd, 1);
    dup2(fd, 2);
    close(fd);
    execl(buf, buf, "-I", "keystroke \"\\r\" video { none } dpmi 4096", NULL);
    myerr("execl failed: %s", strerror(errno));
    _exit(1);
  }
  wait(&stat);
  if (WIFSIGNALED(stat)) myerr("dos terminated by signal");
  if (!WIFEXITED(stat)) myerr("dos terminated by unknown reason");
  if (WEXITSTATUS(stat)) myerr("dos exited with code %d", WEXITSTATUS(stat));

  snprintf(buf, sizeof(buf), "%s/output.txt", emupath);
  cat_file(buf);
  snprintf(buf, sizeof(buf), "%s/errors.txt", emupath);
  cat_file(buf);

  fprintf(stderr, "\n\n");

  snprintf(buf, sizeof(buf), "%s/retcode.txt", emupath);
  if (!(f = fopen(buf, "r"))) myerr("fopen %s failed: %s",buf,strerror(errno));
  if (fscanf(f, "%d", &stat) != 1)
    myerr("cannot parse retcode.txt");
  fscanf(f, " ");
  if (fgetc(f) != EOF) myerr("garbage in retcode.txt");
  fclose(f);

  if (stat != 0) {
    myerr("compilation process error code is %d", stat);
  }

  snprintf(buf, sizeof(buf), "%s/program.exe", emupath);
  if (lstat(buf, &ss) < 0) myerr("output file %s does not exist", buf);
  copy_file(buf, argv[2]);

  return 0;
}
示例#9
0
文件: dir.c 项目: Chainie/mc
int
do_reload_dir (const vfs_path_t * vpath, dir_list * list, sortfn * sort, int count,
               gboolean lc_reverse, gboolean lc_case_sensitive, gboolean exec_ff, const char *fltr)
{
    DIR *dirp;
    struct dirent *dp;
    int next_free = 0;
    int i, status, link_to_dir, stale_link;
    struct stat st;
    int marked_cnt;
    GHashTable *marked_files;
    const char *tmp_path;

    dirp = mc_opendir (vpath);
    if (dirp == NULL)
    {
        message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
        clean_dir (list, count);
        return set_zero_dir (list) ? 1 : 0;
    }

    tree_store_start_check (vpath);

    marked_files = g_hash_table_new (g_str_hash, g_str_equal);
    alloc_dir_copy (list->size);
    for (marked_cnt = i = 0; i < count; i++)
    {
        dir_copy.list[i].fnamelen = list->list[i].fnamelen;
        dir_copy.list[i].fname = list->list[i].fname;
        dir_copy.list[i].f.marked = list->list[i].f.marked;
        dir_copy.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
        dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
        dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
        dir_copy.list[i].sort_key = NULL;
        dir_copy.list[i].second_sort_key = NULL;
        if (list->list[i].f.marked)
        {
            g_hash_table_insert (marked_files, dir_copy.list[i].fname, &dir_copy.list[i]);
            marked_cnt++;
        }
    }

    /* Add ".." except to the root directory. The ".." entry
       (if any) must be the first in the list. */
    tmp_path = vfs_path_get_by_index (vpath, 0)->path;
    if (!
        (vfs_path_elements_count (vpath) == 1 && (tmp_path[0] == PATH_SEP)
         && (tmp_path[1] == '\0')))
    {
        if (!set_zero_dir (list))
        {
            clean_dir (list, count);
            clean_dir (&dir_copy, count);
            return next_free;
        }

        if (get_dotdot_dir_stat (vpath, &st))
            list->list[next_free].st = st;

        next_free++;
    }

    while ((dp = mc_readdir (dirp)))
    {
        status = handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir, &stale_link);
        if (status == 0)
            continue;
        if (status == -1)
        {
            mc_closedir (dirp);
            /* Norbert (Feb 12, 1997):
               Just in case someone finds this memory leak:
               -1 means big trouble (at the moment no memory left),
               I don't bother with further cleanup because if one gets to
               this point he will have more problems than a few memory
               leaks and because one 'clean_dir' would not be enough (and
               because I don't want to spent the time to make it working,
               IMHO it's not worthwhile).
               clean_dir (&dir_copy, count);
             */
            tree_store_end_check ();
            g_hash_table_destroy (marked_files);
            return next_free;
        }

        list->list[next_free].f.marked = 0;

        /*
         * If we have marked files in the copy, scan through the copy
         * to find matching file.  Decrease number of remaining marks if
         * we copied one.
         */
        if (marked_cnt > 0)
        {
            if ((g_hash_table_lookup (marked_files, dp->d_name)))
            {
                list->list[next_free].f.marked = 1;
                marked_cnt--;
            }
        }

        list->list[next_free].fnamelen = NLENGTH (dp);
        list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen);
        list->list[next_free].f.link_to_dir = link_to_dir;
        list->list[next_free].f.stale_link = stale_link;
        list->list[next_free].f.dir_size_computed = 0;
        list->list[next_free].st = st;
        list->list[next_free].sort_key = NULL;
        list->list[next_free].second_sort_key = NULL;
        next_free++;
        if (!(next_free % 16))
            rotate_dash ();
    }
    mc_closedir (dirp);
    tree_store_end_check ();
    g_hash_table_destroy (marked_files);
    if (next_free)
    {
        do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
    }
    clean_dir (&dir_copy, count);
    return next_free;
}
示例#10
0
TEST(QiOs, tmpdir_prefix_zero)
{
  std::string temp = qi::os::mktmpdir(0);
  test_writable_and_empty(temp);
  clean_dir(temp);
}
示例#11
0
文件: pool.c 项目: GDXN/snapraid
/**
 * Clean a directory tree removing all the symlinks and empty directories.
 * Return == 0 if the directory is empty, and it can be removed
 */
static int clean_dir(struct snapraid_state* state, const char* dir)
{
	DIR* d;
	int ignored = 0;

	d = opendir(dir);
	if (!d) {
		/* LCOV_EXCL_START */
		fprintf(stderr, "Error opening pool directory '%s'. %s.\n", dir, strerror(errno));
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}
   
	while (1) { 
		char path_next[PATH_MAX];
		struct stat st;
		const char* name;
		struct dirent* dd;

		/* clear errno to detect erroneous conditions */
		errno = 0;
		dd = readdir(d);
		if (dd == 0 && errno != 0) {
			/* LCOV_EXCL_START */
			fprintf(stderr, "Error reading pool directory '%s'. %s.\n", dir, strerror(errno));
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
		if (dd == 0 && errno == 0) {
			break; /* finished */
		}

		/* skip "." and ".." files */
		name = dd->d_name;
		if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
			continue;

		pathprint(path_next, sizeof(path_next), "%s%s", dir, name);

#if HAVE_STRUCT_DIRENT_D_STAT
		/* convert dirent to lstat result */
		dirent_lstat(dd, &st);
#else
		/* get lstat info about the file */
		if (lstat(path_next, &st) != 0) {
			/* LCOV_EXCL_START */
			fprintf(stderr, "Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
#endif

		if (S_ISLNK(st.st_mode)) {
			int ret;

			/* delete the link */
			ret = remove(path_next);
			if (ret < 0) {
				/* LCOV_EXCL_START */
				fprintf(stderr, "Error removing symlink '%s'. %s.\n", path_next, strerror(errno));
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
		} else if (S_ISDIR(st.st_mode)) {
			/* recurse */
			pathslash(path_next, sizeof(path_next));
			if (clean_dir(state, path_next) == 0) {
				int ret;

				/* directory is empty, try to remove it */
				ret = rmdir(path_next);
				if (ret < 0) {
#ifdef _WIN32
					if (errno == EACCES) {
						/* in Windows just ignore EACCES errors removing directories */
						/* because it could happen that the directory is in use */
						/* and it cannot be removed */
						fprintf(stderr, "Directory '%s' not removed because it's in use.\n", path_next);
						ignored = 1;
					} else
#endif
					{
						/* LCOV_EXCL_START */
						fprintf(stderr, "Error removing pool directory '%s'. %s.\n", path_next, strerror(errno));
						exit(EXIT_FAILURE);
						/* LCOV_EXCL_STOP */
					}
				}
			} else {
				/* something was ignored inside the subdir */
				ignored = 1;
			}
		} else {
			ignored = 1;
			if (state->opt.verbose) {
				printf("Ignoring pool file '%s'\n", path_next);
			}
		}
	}

	if (closedir(d) != 0) {
		/* LCOV_EXCL_START */
		fprintf(stderr, "Error closing pool directory '%s'. %s.\n", dir, strerror(errno));
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	return ignored;
}
示例#12
0
文件: pool.c 项目: GDXN/snapraid
void state_pool(struct snapraid_state* state)
{
	tommy_node* i;
	char pool_dir[PATH_MAX];
	char share_dir[PATH_MAX];
	unsigned count;

	if (state->pool[0] == 0) {
		/* LCOV_EXCL_START */
		fprintf(stderr, "To use the 'pool' command you must set the pool directory in the configuration file\n");
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	printf("Cleaning...\n");

	/* pool directory with final slash */
	pathprint(pool_dir, sizeof(pool_dir), "%s", state->pool);
	pathslash(pool_dir, sizeof(pool_dir));

	/* share directory with final slash */
	pathprint(share_dir, sizeof(share_dir), "%s", state->share);
	pathslash(share_dir, sizeof(share_dir));

	/* first clear the previous pool tree */
	clean_dir(state, pool_dir);

	printf("Pooling...\n");

	/* for each disk */
	count = 0;
	for(i=state->disklist;i!=0;i=i->next) {
		tommy_node* j;
		struct snapraid_disk* disk = i->data;

		/* for each file */
		for(j=disk->filelist;j!=0;j=j->next) {
			struct snapraid_file* file = j->data;
			make_link(pool_dir, share_dir, disk, file->sub);
			++count;
		}

		/* for each link */
		for(j=disk->linklist;j!=0;j=j->next) {
			struct snapraid_link* link = j->data;
			make_link(pool_dir, share_dir, disk, link->sub);
			++count;
		}

		/* we ignore empty dirs in disk->dir */
	}

	if (count)
		printf("%u links created\n", count);
	else
		printf("No link created\n");

	fprintf(stdlog, "summary:link_count::%u\n", count);
	fprintf(stdlog, "summary:exit:ok\n");
	fflush(stdlog);
}