디렉토리를 체크하고 만들기..

leolo의 이미지

간단하게 디렉토리가 존재하는지를 체크해서..
존재하면 만들지 않고..
존재하지 않으면 디렉토리를 만들려고 합니다..

if(mkdir(WEBDIR, mode) < 0)
{
     fprintf(stderr, "dir creat failed\n");
}

대충 어떠한 것들을 체크해야하는지.. 어떻게 만들어야하는지..
알고 싶습니다..

한가지 더 부탁드리면.. 시그널에 관한 문제입니다..
일반적으로 데몬을 만들때.. SIGKILL을 제외하고..
어떠한 시그널에도 죽지 않게.. 만들려면 어떠한 시그널을
mask시켜야 하는지 알고 싶습니다..
SIGHUP, SIGCHLD.. etc...

gitagy의 이미지

struct t_varchar {
	char	*arr;
	int		len;
};

struct request_info {
	char	s_code[3];
	char	c_path_len[5];
	char	cmd_len[6];
	char	*c_path;
	char	*cmd;
};

int check_path(const char *path)
{
	struct stat sb;
	int p;

	if (stat(path, &sb) != 0) {
		if (errno == ENOENT) {
			p = 0;
		} else {
			p = 9;
		}
	} else {
		if (S_ISDIR(sb.st_mode)) {
			p = 2;
		} else {
			p = 1;
		}
	}

	return p;
}

struct t_varchar *t_varchar_init(void)
{
	struct t_varchar *t;

	t = (struct t_varchar *)malloc(sizeof(struct t_varchar));
	t->len = 0;
	t->arr = NULL;

	return t;
}

void t_varchar_free(struct t_varchar *t)
{
	if (t->arr != NULL)
		free(t->arr);

	if (t)
		free(t);

	return;
}

char *get_homedir(const char *homedir, const struct request_info *rip)
{
	char *t;
	int h_len, cp_len;

	h_len = strlen(homedir);
	cp_len = atoi(rip->c_path_len);

	t = (char *)malloc(h_len+cp_len+2);

	memcpy(t, homedir, h_len);

	if (cp_len > 0) {
		memcpy(t+h_len, "/", 1);
		memcpy(t+h_len+1, rip->c_path, cp_len);
		t[h_len+1+cp_len] = '\0';
	} else {
		t[h_len] = '\0';
	}

	return t;
}

char *get_cmd(const struct request_info *rip)
{
	char *t;

	t = (char *)malloc(atoi(rip->cmd_len)+1);

	memcpy(t, rip->cmd, atoi(rip->cmd_len));
	t[atoi(rip->cmd_len)] = '\0';

	return t;
}

int check_path(const char *path)
{
	struct stat sb;
	int p;

	if (stat(path, &sb) != 0) {
		if (errno == ENOENT) {
			p = 0;
		} else {
			p = 9;
		}
	} else {
		if (S_ISDIR(sb.st_mode)) {
			p = 2;
		} else {
			p = 1;
		}
	}

	return p;
}

char *get_path_info(const char *path, const char *name, const int mark)
{
	struct stat		sb;
	struct tm		*time_ptr, *time_buf;
	unsigned int	size;
	char			time[20];
	char			mark_h[2] = "", mark_f[2] = "";
	char			*buf;

	if (mark == 1) {
		strcpy(mark_h, "*");
	} else if (mark == 2) {
		strcpy(mark_f, "*");
	}

	time_buf = (struct tm*)malloc(sizeof(struct tm));
	buf = (char *)malloc(strlen(name)+50);

	stat(path, &sb);

	time_ptr = localtime_r(&sb.st_mtime, time_buf);
	strftime(time, 20, "%Y-%m-%d %X", time_ptr);

	if (S_ISREG(sb.st_mode)) {
		size = (int)sb.st_size/1024;
		sprintf(buf, "%s1%s|%d|%s%s", mark_h, name, size, time, mark_f);
	} else if (S_ISDIR(sb.st_mode)) {
		sprintf(buf, "%s2%s|0|%s%s", mark_h, name, time, mark_f);
	}

	free(time_buf);

	return buf;
}

/*
** input  : (int)type(str)name  -  "2new folder"
** output : (str)info  -  "2my folder|0|2000/11/21 20:30:21"
*/
void os_make_new(struct t_varchar *snd_buf, const char *homedir, const struct request_info *rip)
{
	char			this_err[ERR_LEN];
	char 			*path, *file, *node, *s_buf;
	FILE			*fp;
	int				i, s, r = 0;
	
	path = get_homedir(homedir, rip);

	file = (char *)malloc(atoi(rip->cmd_len)+6);
	node = (char *)malloc(strlen(path)+7+atoi(rip->cmd_len));

	for (i = 0; i < 100; i++) {
		if (i < 1) {
			sprintf(file, "%.*s", atoi(rip->cmd_len)-1, rip->cmd+1);
		} else {
			sprintf(file, "(%d)%.*s", i, atoi(rip->cmd_len)-1, rip->cmd+1);
		}

		sprintf(node, "%s/%s", path, file);

		if ( (s = check_path(node)) == 9) {
			strcpy(this_err, CT_SYSTEM_ERR);
			break;
		} else if (s == 0) {
			if (rip->cmd[0] == '1') {
				if ( (fp = fopen(node, "w")) == NULL) {
					strcpy(this_err, CT_SYSTEM_ERR);
				} else {
					r = 1;
					fclose(fp);
				}
			} else if (rip->cmd[0] == '2') {
				if (mkdir(node, S_IRWXU) != 0) {
					strcpy(this_err, CT_SYSTEM_ERR);
				} else {
					r = 1;
				}
			}

			break;
		}
	}

	if (r == 0) {
		add_to_t_varchar(snd_buf, "9", 1);
		add_to_t_varchar(snd_buf, this_err, strlen(this_err));
	} else {
		s_buf = get_path_info(node, file, 0);
		add_to_t_varchar(snd_buf, s_buf, strlen(s_buf));
		free(s_buf);
	}

	free(node);
	free(file);
	free(path);

	return;
}

허접한 실력으로 예전에 만든 파일&디렉터리 핸들링 부분의 소스 입니다.
보면 아실만큼 간단한 소스인지라 따로 설명은
쿨럭... :!:

ddt의 이미지

leolo wrote:
간단하게 디렉토리가 존재하는지를 체크해서..
존재하면 만들지 않고..
존재하지 않으면 디렉토리를 만들려고 합니다..

man 2 mkdir에서

EEXIST pathname  already  exists  (not  necessarily  as  a
              directory).  This includes the case where  pathname
              is a symbolic link, dangling or not.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.