public inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH v17 02/10] pg_stat_file and pg_ls_dir_* to use lstat()..
2+ messages / 2 participants
[nested] [flat]

* [PATCH v17 02/10] pg_stat_file and pg_ls_dir_* to use lstat()..
@ 2020-03-30 23:59 Justin Pryzby <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Justin Pryzby @ 2020-03-30 23:59 UTC (permalink / raw)

pg_ls_dir_* will now skip (no longer show) symbolic links, same as other
non-regular file types, as we advertize we do since 8b6d94cf6.  That seems to
be the intented behavior, since irregular file types are 1) less portable; and,
2) we don't currently show a file's type except for "bool is_dir".

pg_stat_file will now 1) show metadata of links themselves, rather than their
target; and, 2) specifically, show links to directories with "is_dir=false";
and, 3) not error on broken symlinks.
---
 doc/src/sgml/func.sgml          | 2 +-
 src/backend/utils/adt/genfile.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index e0d1eff6b5..d9b3598977 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -25861,7 +25861,7 @@ SELECT convert_from(pg_read_binary_file('file_in_utf8.txt'), 'UTF8');
         Returns a record containing the file's size, last access time stamp,
         last modification time stamp, last file status change time stamp (Unix
         platforms only), file creation time stamp (Windows only), and a flag
-        indicating if it is a directory (or a symbolic link to a directory).
+        indicating if it is a directory.
        </para>
        <para>
         This function is restricted to superusers by default, but other users
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index ceaa6180da..219ac160f8 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -370,7 +370,7 @@ pg_stat_file(PG_FUNCTION_ARGS)
 
 	filename = convert_and_check_filename(filename_t);
 
-	if (stat(filename, &fst) < 0)
+	if (lstat(filename, &fst) < 0)
 	{
 		if (missing_ok && errno == ENOENT)
 			PG_RETURN_NULL();
@@ -596,7 +596,7 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
 
 		/* Get the file info */
 		snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
-		if (stat(path, &attrib) < 0)
+		if (lstat(path, &attrib) < 0)
 		{
 			/* Ignore concurrently-deleted files, else complain */
 			if (errno == ENOENT)
-- 
2.17.0


--4LFBTxd4L5NLO6ly
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
 filename="v17-0003-Add-tests-on-pg_ls_dir-before-changing-it.patch"



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* real/float example for testlibpq3
@ 2022-02-24 21:42 Mark Wong <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Mark Wong @ 2022-02-24 21:42 UTC (permalink / raw)
  To: [email protected]

Hi everyone,

Would adding additional examples to testlibpq3.c on handling more data
types be helpful?  I've attached a patch adding how to handle a REAL to
current example.

Regards,
Mark

diff --git a/src/test/examples/testlibpq3.c b/src/test/examples/testlibpq3.c
index 4f7b791388..0c1739fcbb 100644
--- a/src/test/examples/testlibpq3.c
+++ b/src/test/examples/testlibpq3.c
@@ -11,19 +11,21 @@
  * CREATE SCHEMA testlibpq3;
  * SET search_path = testlibpq3;
  * SET standard_conforming_strings = ON;
- * CREATE TABLE test1 (i int4, t text, b bytea);
- * INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004');
- * INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000');
+ * CREATE TABLE test1 (i int4, r real, t text, b bytea);
+ * INSERT INTO test1 values (1, 2.3, 'joe''s place', '\000\001\002\003\004');
+ * INSERT INTO test1 values (2, 4.5 'ho there', '\004\003\002\001\000');
  *
  * The expected output is:
  *
  * tuple 0: got
  *	i = (4 bytes) 1
+ *	r = (4 bytes) 2.3
  *	t = (11 bytes) 'joe's place'
  *	b = (5 bytes) \000\001\002\003\004
  *
  * tuple 0: got
  *	i = (4 bytes) 2
+ *	r = (4 bytes) 4.5
  *	t = (8 bytes) 'ho there'
  *	b = (5 bytes) \004\003\002\001\000
  */
@@ -62,32 +64,41 @@ show_binary_results(PGresult *res)
 	int			i,
 				j;
 	int			i_fnum,
+				r_fnum,
 				t_fnum,
 				b_fnum;
 
 	/* Use PQfnumber to avoid assumptions about field order in result */
 	i_fnum = PQfnumber(res, "i");
+	r_fnum = PQfnumber(res, "r");
 	t_fnum = PQfnumber(res, "t");
 	b_fnum = PQfnumber(res, "b");
 
 	for (i = 0; i < PQntuples(res); i++)
 	{
 		char	   *iptr;
+		char	   *rptr;
 		char	   *tptr;
 		char	   *bptr;
 		int			blen;
 		int			ival;
+		union {
+			int		ival;
+			float	fval;
+		}			rval;
 
 		/* Get the field values (we ignore possibility they are null!) */
 		iptr = PQgetvalue(res, i, i_fnum);
+		rptr = PQgetvalue(res, i, r_fnum);
 		tptr = PQgetvalue(res, i, t_fnum);
 		bptr = PQgetvalue(res, i, b_fnum);
 
 		/*
-		 * The binary representation of INT4 is in network byte order, which
-		 * we'd better coerce to the local byte order.
+		 * The binary representation of INT4 and REAL are in network byte
+		 * order, which we'd better coerce to the local byte order.
 		 */
 		ival = ntohl(*((uint32_t *) iptr));
+		rval.ival = ntohl(*((uint32_t *) rptr));
 
 		/*
 		 * The binary representation of TEXT is, well, text, and since libpq
@@ -102,6 +113,8 @@ show_binary_results(PGresult *res)
 		printf("tuple %d: got\n", i);
 		printf(" i = (%d bytes) %d\n",
 			   PQgetlength(res, i, i_fnum), ival);
+		printf(" r = (%d bytes) %f\n",
+			   PQgetlength(res, i, r_fnum), rval.fval);
 		printf(" t = (%d bytes) '%s'\n",
 			   PQgetlength(res, i, t_fnum), tptr);
 		printf(" b = (%d bytes) ", blen);
diff --git a/src/test/examples/testlibpq3.sql b/src/test/examples/testlibpq3.sql
index 35a95ca347..e3a70cec27 100644
--- a/src/test/examples/testlibpq3.sql
+++ b/src/test/examples/testlibpq3.sql
@@ -1,6 +1,6 @@
 CREATE SCHEMA testlibpq3;
 SET search_path = testlibpq3;
 SET standard_conforming_strings = ON;
-CREATE TABLE test1 (i int4, t text, b bytea);
-INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004');
-INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000');
+CREATE TABLE test1 (i int4, r real, t text, b bytea);
+INSERT INTO test1 values (1, 2.3, 'joe''s place', '\000\001\002\003\004');
+INSERT INTO test1 values (2, 4.5, 'ho there', '\004\003\002\001\000');


Attachments:

  [text/plain] testlibpq3-real-v1.patch (3.2K, ../../Yhf70G9OYIKueYZi@workstation-mark-wong/2-testlibpq3-real-v1.patch)
  download | inline diff:
diff --git a/src/test/examples/testlibpq3.c b/src/test/examples/testlibpq3.c
index 4f7b791388..0c1739fcbb 100644
--- a/src/test/examples/testlibpq3.c
+++ b/src/test/examples/testlibpq3.c
@@ -11,19 +11,21 @@
  * CREATE SCHEMA testlibpq3;
  * SET search_path = testlibpq3;
  * SET standard_conforming_strings = ON;
- * CREATE TABLE test1 (i int4, t text, b bytea);
- * INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004');
- * INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000');
+ * CREATE TABLE test1 (i int4, r real, t text, b bytea);
+ * INSERT INTO test1 values (1, 2.3, 'joe''s place', '\000\001\002\003\004');
+ * INSERT INTO test1 values (2, 4.5 'ho there', '\004\003\002\001\000');
  *
  * The expected output is:
  *
  * tuple 0: got
  *	i = (4 bytes) 1
+ *	r = (4 bytes) 2.3
  *	t = (11 bytes) 'joe's place'
  *	b = (5 bytes) \000\001\002\003\004
  *
  * tuple 0: got
  *	i = (4 bytes) 2
+ *	r = (4 bytes) 4.5
  *	t = (8 bytes) 'ho there'
  *	b = (5 bytes) \004\003\002\001\000
  */
@@ -62,32 +64,41 @@ show_binary_results(PGresult *res)
 	int			i,
 				j;
 	int			i_fnum,
+				r_fnum,
 				t_fnum,
 				b_fnum;
 
 	/* Use PQfnumber to avoid assumptions about field order in result */
 	i_fnum = PQfnumber(res, "i");
+	r_fnum = PQfnumber(res, "r");
 	t_fnum = PQfnumber(res, "t");
 	b_fnum = PQfnumber(res, "b");
 
 	for (i = 0; i < PQntuples(res); i++)
 	{
 		char	   *iptr;
+		char	   *rptr;
 		char	   *tptr;
 		char	   *bptr;
 		int			blen;
 		int			ival;
+		union {
+			int		ival;
+			float	fval;
+		}			rval;
 
 		/* Get the field values (we ignore possibility they are null!) */
 		iptr = PQgetvalue(res, i, i_fnum);
+		rptr = PQgetvalue(res, i, r_fnum);
 		tptr = PQgetvalue(res, i, t_fnum);
 		bptr = PQgetvalue(res, i, b_fnum);
 
 		/*
-		 * The binary representation of INT4 is in network byte order, which
-		 * we'd better coerce to the local byte order.
+		 * The binary representation of INT4 and REAL are in network byte
+		 * order, which we'd better coerce to the local byte order.
 		 */
 		ival = ntohl(*((uint32_t *) iptr));
+		rval.ival = ntohl(*((uint32_t *) rptr));
 
 		/*
 		 * The binary representation of TEXT is, well, text, and since libpq
@@ -102,6 +113,8 @@ show_binary_results(PGresult *res)
 		printf("tuple %d: got\n", i);
 		printf(" i = (%d bytes) %d\n",
 			   PQgetlength(res, i, i_fnum), ival);
+		printf(" r = (%d bytes) %f\n",
+			   PQgetlength(res, i, r_fnum), rval.fval);
 		printf(" t = (%d bytes) '%s'\n",
 			   PQgetlength(res, i, t_fnum), tptr);
 		printf(" b = (%d bytes) ", blen);
diff --git a/src/test/examples/testlibpq3.sql b/src/test/examples/testlibpq3.sql
index 35a95ca347..e3a70cec27 100644
--- a/src/test/examples/testlibpq3.sql
+++ b/src/test/examples/testlibpq3.sql
@@ -1,6 +1,6 @@
 CREATE SCHEMA testlibpq3;
 SET search_path = testlibpq3;
 SET standard_conforming_strings = ON;
-CREATE TABLE test1 (i int4, t text, b bytea);
-INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004');
-INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000');
+CREATE TABLE test1 (i int4, r real, t text, b bytea);
+INSERT INTO test1 values (1, 2.3, 'joe''s place', '\000\001\002\003\004');
+INSERT INTO test1 values (2, 4.5, 'ho there', '\004\003\002\001\000');


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-02-24 21:42 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-30 23:59 [PATCH v17 02/10] pg_stat_file and pg_ls_dir_* to use lstat().. Justin Pryzby <[email protected]>
2022-02-24 21:42 real/float example for testlibpq3 Mark Wong <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox