Beispiel #1
0
xi_file_re xi_file_stat(const xchar* pathname, xi_file_stat_t *s) {
	WIN32_FILE_ATTRIBUTE_DATA se;
	SYSTEMTIME st;
	LARGE_INTEGER fsize;

	xbool ret;
	xi_time_t xtime;
	xlong utc;

	if (pathname == NULL || s == NULL) {
		return XI_FILE_RV_ERR_ARGS;
	}

	ret = GetFileAttributesEx(pathname, GetFileExInfoStandard, &se);
	if (!ret) {
		return XI_FILE_RV_ERR_ARGS;
	}

	if (se.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
		s->type = XI_FILE_TYPE_DIR;
	} else {
		s->type = XI_FILE_TYPE_REG;
	}

	if (se.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
		s->perm = 00400;
	} else {
		s->perm = 00600;
	}

	fsize.LowPart = se.nFileSizeLow;
	fsize.HighPart = se.nFileSizeHigh;
	s->size = fsize.QuadPart;
	s->blocks = (s->size / 4096L) + 1;

	FileTimeToSystemTime(&se.ftCreationTime, &st);
	xtime.year = st.wYear + 1601;
	xtime.mon = st.wMonth;
	xtime.day = st.wDay;
	xtime.hour = st.wHour;
	xtime.min = st.wMinute;
	xtime.sec = st.wSecond;
	xtime.msec = st.wMilliseconds;
	xi_clock_time2sec(&utc, xtime);
	s->created = (utc * 1000) + xtime.msec;

	FileTimeToSystemTime(&se.ftLastAccessTime, &st);
	xtime.year = st.wYear + 1601;
	xtime.mon = st.wMonth;
	xtime.day = st.wDay;
	xtime.hour = st.wHour;
	xtime.min = st.wMinute;
	xtime.sec = st.wSecond;
	xtime.msec = st.wMilliseconds;
	xi_clock_time2sec(&utc, xtime);
	s->accessed = (utc * 1000) + xtime.msec;

	FileTimeToSystemTime(&se.ftLastWriteTime, &st);
	xtime.year = st.wYear + 1601;
	xtime.mon = st.wMonth;
	xtime.day = st.wDay;
	xtime.hour = st.wHour;
	xtime.min = st.wMinute;
	xtime.sec = st.wSecond;
	xtime.msec = st.wMilliseconds;
	xi_clock_time2sec(&utc, xtime);
	s->modified = (utc * 1000) + xtime.msec;

	xi_strcpy(s->pathname, pathname);
	xi_strcpy(s->filename, xi_pathname_basename(pathname));

	return XI_FILE_RV_OK;
}
Beispiel #2
0
xi_file_re xi_file_fstat(xint32 fd, xi_file_stat_t *s) {
	BY_HANDLE_FILE_INFORMATION se;
	SYSTEMTIME st;
	LARGE_INTEGER fsize;

	xbool ret;
	xg_fd_t *fdesc;
	xi_time_t xtime;
	xlong utc;

	if (fd < 0 || s == NULL) {
		return XI_FILE_RV_ERR_ARGS;
	}

	fdesc = xg_fd_get(fd);

	ret = GetFileInformationByHandle(fdesc->desc.f.fd, &se);
	if (!ret) {
		return XI_FILE_RV_ERR_ARGS;
	}

	if (se.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
		s->type = XI_FILE_TYPE_DIR;
	} else {
		s->type = XI_FILE_TYPE_REG;
	}

	if (se.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
		s->perm = 00400;
	} else {
		s->perm = 00600;
	}

	fsize.LowPart = se.nFileSizeLow;
	fsize.HighPart = se.nFileSizeHigh;
	s->size = fsize.QuadPart;
	s->blocks = (s->size / 4096L) + 1;

	FileTimeToSystemTime(&se.ftCreationTime, &st);
	xtime.year = st.wYear + 1601;
	xtime.mon = st.wMonth;
	xtime.day = st.wDay;
	xtime.hour = st.wHour;
	xtime.min = st.wMinute;
	xtime.sec = st.wSecond;
	xtime.msec = st.wMilliseconds;
	xi_clock_time2sec(&utc, xtime);
	s->created = (utc * 1000) + xtime.msec;

	FileTimeToSystemTime(&se.ftLastAccessTime, &st);
	xtime.year = st.wYear + 1601;
	xtime.mon = st.wMonth;
	xtime.day = st.wDay;
	xtime.hour = st.wHour;
	xtime.min = st.wMinute;
	xtime.sec = st.wSecond;
	xtime.msec = st.wMilliseconds;
	xi_clock_time2sec(&utc, xtime);
	s->accessed = (utc * 1000) + xtime.msec;

	FileTimeToSystemTime(&se.ftLastWriteTime, &st);
	xtime.year = st.wYear + 1601;
	xtime.mon = st.wMonth;
	xtime.day = st.wDay;
	xtime.hour = st.wHour;
	xtime.min = st.wMinute;
	xtime.sec = st.wSecond;
	xtime.msec = st.wMilliseconds;
	xi_clock_time2sec(&utc, xtime);
	s->modified = (utc * 1000) + xtime.msec;

	xi_strcpy(s->pathname, fdesc->desc.f.path);
	xi_strcpy(s->filename, xi_pathname_basename(fdesc->desc.f.path));

	return XI_FILE_RV_OK;
}
Beispiel #3
0
int tc_xi_file_dop() {
	xint32 t = 1;
	xchar *tcname = "xi_file.h";

	xint32 ret;

	xi_dir_t *tdir;
	xi_file_stat_t fs;

	xchar pathbuf[1024];
	xchar pathtmp[1024];
	xchar *pathptr = NULL;

	tc_info();

	log_print(XDLOG, "[%s:%02d] Create a directory ##########\n", tcname, t++);
	ret = xi_dir_make("xx_test", 0755);
	if (ret < 0) {
		log_print(XDLOG, "    - result : failed!!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass.\n\n");

	log_print(XDLOG, "[%s:%02d] Create same directory #######\n", tcname, t++);
	ret = xi_dir_make("xx_test", 0x755);
	if (ret != XI_FILE_RV_ERR_AE) {
		log_print(XDLOG, "    - result : failed!! (must return -4 / ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass. (must return -4 / ret=%d)\n\n", ret);

	log_print(XDLOG, "[%s:%02d] Remove the directory ########\n", tcname, t++);
	ret = xi_dir_remove("xx_test");
	if (ret < 0) {
		log_print(XDLOG, "    - result : failed!!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass.\n\n");

	log_print(XDLOG, "[%s:%02d] Create a nested directory ###\n", tcname, t++);
	ret = xi_dir_make_force("xx_test/depth", 0755);
	if (ret < 0) {
		log_print(XDLOG, "    - result : failed!!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass.\n\n");

	log_print(XDLOG, "[%s:%02d] Remove a top directory ######\n", tcname, t++);
	ret = xi_dir_remove("xx_test");
	if (ret != XI_FILE_RV_ERR_NOTEMPT) {
		log_print(XDLOG, "    - result : failed!! (must return -14 / ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass. (must return -14 / ret=%d)\n\n", ret);

	log_print(XDLOG, "[%s:%02d] Open the directory ##########\n", tcname, t++);
	tdir = xi_dir_open("xx_test");
	if (tdir == NULL) {
		log_print(XDLOG, "    - result : failed!! (dir=%p)\n\n", tdir);
		return -1;
	}
	log_print(XDLOG, "    - result : pass. (dir=%p)\n\n", tdir);

	log_print(XDLOG, "[%s:%02d] Read the directory ##########\n", tcname, t++);
	log_print(XDLOG, "xx_test >\n");
	while ((ret = xi_dir_read(tdir, &fs)) > 0) {
		log_print(XDLOG, "\t%s\n", fs.filename);
	}
	if (ret < 0) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass.\n\n");

	log_print(XDLOG, "[%s:%02d] Close the directory #########\n", tcname, t++);
	ret = xi_dir_close(tdir);
	if (ret < 0) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass.\n\n");

	log_print(XDLOG, "[%s:%02d] Delete test directories #####\n", tcname, t++);
	ret = xi_dir_remove("xx_test/depth");
	if (ret < 0) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	ret = xi_dir_remove("xx_test");
	if (ret < 0) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass.\n\n");

	log_print(XDLOG, "[%s:%02d] xi_pathname_absolute(..) ####\n", tcname, t++);
	ret = xi_pathname_absolute(pathbuf, sizeof(pathbuf), "../");
	if (ret != XI_FILE_RV_OK) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass. (../ = %s)\n\n", pathbuf);

	log_print(XDLOG, "[%s:%02d] xi_pathname_basename(..) ####\n", tcname, t++);
	pathptr = xi_pathname_basename(pathbuf);
	log_print(XDLOG, "    - result : pass. (basename ../ = %s)\n\n", pathptr);

	log_print(XDLOG, "[%s:%02d] xi_pathname_get #############\n", tcname, t++);
	ret = xi_pathname_get(pathbuf, sizeof(pathbuf));
	if (ret != XI_FILE_RV_OK) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	log_print(XDLOG, "    - result : pass.(current dir = %s)\n\n", pathbuf);

	log_print(XDLOG, "[%s:%02d] xi_pathname_set(..) #########\n", tcname, t++);
	ret = xi_pathname_set("..");
	if (ret != XI_FILE_RV_OK) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	xi_pathname_get(pathtmp, sizeof(pathtmp));
	log_print(XDLOG, "    - result : pass.(current dir = %s)\n\n", pathtmp);

	log_print(XDLOG, "[%s:%02d] xi_pathname_set (old) #######\n", tcname, t++);
	ret = xi_pathname_set(pathbuf);
	if (ret != XI_FILE_RV_OK) {
		log_print(XDLOG, "    - result : failed!! (ret=%d)\n\n", ret);
		return -1;
	}
	xi_pathname_get(pathtmp, sizeof(pathtmp));
	log_print(XDLOG, "    - result : pass.(current dir = %s)\n\n", pathtmp);

	log_print(XDLOG, "========= DONE [xi_file.h - dir/path OP] =========\n\n");

	return 0;
}