Commits

Sebastian Freundt committed 4c17551 Merge

Merge branch 'next'

* next:
minor, do not instantiate result upon read_zif() until very late
fix, associate -1 with anonymous map, tfraw.c
minor, check for NULL result after init'ing the prchunk buffer
fix, associate -1 with anonymous map
fix, carry nanosecond slot through zone conversions
test, add regression tests for the %N bug

Comments (0)

Files changed (16)

lib/dt-core-tz-glue.c

 			.s = sexy % 60,
 			.m = (sexy % 3600) / 60,
 			.h = sexy / 3600,
+			.ns = dt.t.hms.ns,
 		};
 #else  /* !__C1X */
 		res.t.hms.s = sexy % 60;
 		res.t.hms.m = (sexy % 3600) / 60;
 		res.t.hms.h = sexy / 3600;
+		res.t.hms.ns = dt.t.hms.ns;
 #endif	/* __C1X */
 	}
 	return res;
 			.s = sexy % 60,
 			.m = (sexy % 3600) / 60,
 			.h = sexy / 3600,
+			.ns = dt.t.hms.ns,
 		};
 #else  /* !__C1X */
 		res.t.hms.s = sexy % 60;
 		res.t.hms.m = (sexy % 3600) / 60;
 		res.t.hms.h = sexy / 3600;
+		res.t.hms.ns = dt.t.hms.ns;
 #endif	/* __C1X */
 	}
 	return res;
 static zif_t
 __read_zif(int fd)
 {
-	zif_t res;
+	struct zif_s tmp;
 	struct stat st;
-	void *map;
+	zif_t res;
 
 	if (fstat(fd, &st) < 0) {
 		return NULL;
 	}
-	res = malloc(sizeof(*res));
-	res->mpsz = st.st_size;
-	res->fd = fd;
-	map = mmap(NULL, res->mpsz, PROT_READ, MAP_SHARED, fd, 0);
-	if (map != MAP_FAILED) {
-		res->hdr = map;
+	tmp.mpsz = st.st_size;
+	tmp.fd = fd;
+	tmp.hdr = mmap(NULL, tmp.mpsz, PROT_READ, MAP_SHARED, fd, 0);
+	if (tmp.hdr == MAP_FAILED) {
+		return NULL;
 	}
+	/* otherwise generate an output structure */
+	res = malloc(sizeof(*res));
+	*res = tmp;
 	return res;
 }
 
 	}
 	sz = z->mpsz + sizeof(*z);
 
-	map = mmap(NULL, sz, PROT_MEMMAP, MAP_MEMMAP, 0, 0);
+	map = mmap(NULL, sz, PROT_MEMMAP, MAP_MEMMAP, -1, 0);
 	if (map != MAP_FAILED) {
 		/* we mmap'ped ourselves a slightly larger struct
 		 * res + 1 points to the header*/
 
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto ndl_free;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t llen;
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
+	ndl_free:
 		if (needle != __nstk) {
 			free(needle);
 		}
 		ndlsoa = build_needle(needle, nneedle, fmt, nfmt);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto ndl_free;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t llen;
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
+	ndl_free:
 		if (needle != __nstk) {
 			free(needle);
 		}
 		__io_setlocking_bycaller(stdout);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto out;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t UNUSED(llen);
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
-		goto out;
 	}
 
 out:
 		ndlsoa = build_needle(needle, nneedle, fmt, nfmt);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto ndl_free;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t llen;
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
+	ndl_free:
 		if (needle != __nstk) {
 			free(needle);
 		}
 		ndlsoa = build_needle(needle, nneedle, fmt, nfmt);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto ndl_free;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t llen;
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
+	ndl_free:
 		if (needle != __nstk) {
 			free(needle);
 		}
 #define MAP_MEM		(MAP_ANONYMOUS | MAP_PRIVATE)
 #define PROT_MEM	(PROT_READ | PROT_WRITE)
 #define MAP_LEN		(MAX_NLINES * MAX_LLEN)
-	__ctx->buf = mmap(NULL, MAP_LEN, PROT_MEM, MAP_MEM, 0, 0);
-	__ctx->fd = fd;
+	__ctx->buf = mmap(NULL, MAP_LEN, PROT_MEM, MAP_MEM, -1, 0);
+	if (__ctx->buf == MAP_FAILED) {
+		return NULL;
+	}
+
+	/* bit of space for the rechunker */
+	__ctx->soff = mmap(NULL, MAP_LEN, PROT_MEM, MAP_MEM, -1, 0);
+	if (__ctx->soff == MAP_FAILED) {
+		return NULL;
+	}
 
+	__ctx->fd = fd;
 #if defined POSIX_FADV_SEQUENTIAL
 	/* give advice about our read pattern */
 	posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
 #endif	/* POSIX_FADV_SEQUENTIAL */
-
-	/* bit of space for the rechunker */
-	__ctx->soff = mmap(NULL,MAP_LEN, PROT_MEM, MAP_MEM, 0, 0);
 	return __ctx;
 }
 
 		return 1;
 	}
 	/* get our prchunk up n running */
-	ctx = init_prchunk(fd);
+	if ((ctx = init_prchunk(fd)) == NULL) {
+		perror("ctx NULL");
+		return 1;
+	}
 	/* fill the buffer */
 	while (!(prchunk_fill(ctx) < 0)) {
 		char *l[1];
 	void *pctx;
 
 	/* using the prchunk reader now */
-	pctx = init_prchunk(STDIN_FILENO);
+	if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+		perror("dtconv: could not open stdin");
+		return;
+	}
 	while (prchunk_fill(pctx) >= 0) {
 		for (char *line; prchunk_haslinep(pctx); lno++) {
 			size_t UNUSED(llen);
 		ndlsoa = build_tneedle(needle, nneedle, fmt, nfmt);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto ndl_free;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				ssize_t llen;
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
+	ndl_free:
 		if (needle != __nstk) {
 			free(needle);
 		}
 		ndlsoa = build_tneedle(needle, nneedle, fmt, nfmt);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto ndl_free;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t llen;
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
+	ndl_free:
 		if (needle != __nstk) {
 			free(needle);
 		}
 		__io_setlocking_bycaller(stdout);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto out;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t UNUSED(llen);
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
-		goto out;
 	}
 
 out:
 		ndlsoa = build_tneedle(needle, nneedle, fmt, nfmt);
 
 		/* using the prchunk reader now */
-		pctx = init_prchunk(STDIN_FILENO);
+		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
+			perror("dtconv: could not open stdin");
+			goto ndl_free;
+		}
 		while (prchunk_fill(pctx) >= 0) {
 			for (char *line; prchunk_haslinep(pctx); lno++) {
 				size_t llen;
 		}
 		/* get rid of resources */
 		free_prchunk(pctx);
+	ndl_free:
 		if (needle != __nstk) {
 			free(needle);
 		}
 TESTS += dtconv.16.dt
 TESTS += dtconv.17.dt
 TESTS += dtconv.18.dt
+TESTS += dtconv.19.dt
+TESTS += dtconv.20.dt
 
 ## Makefile.am ends here

test/dtconv.19.dt

+## -*- shell-script -*-
+
+TOOL=dtconv
+CMDLINE="--from-zone Europe/Berlin -z Europe/Berlin \
+	-i '%F %T.%N' -f '%T.%N' '2012-01-01 00:00:00.123456789'"
+
+## STDOUT
+stdout=$(mktemp "/tmp/tmp.XXXXXXXXXX")
+cat > "${stdout}" <<EOF
+00:00:00.123456789
+EOF
+
+## dtconv.19.dt ends here

test/dtconv.20.dt

+## -*- shell-script -*-
+
+TOOL=dtconv
+CMDLINE="--from-zone Europe/Berlin -z Europe/Berlin -i '%F %T.%N' -f '%T.%N'"
+
+## STDIN
+stdin=$(mktemp "/tmp/tmp.XXXXXXXXXX")
+cat > "${stdin}" <<EOF
+2010-01-01 10:00:00.723000000
+EOF
+
+## STDOUT
+stdout=$(mktemp "/tmp/tmp.XXXXXXXXXX")
+cat > "${stdout}" <<EOF
+10:00:00.723000000
+EOF
+
+## dtconv.20.dt ends here