Subject: [PATCH v9 10/11] pg_ls_*dir to show directories and "isdir" column..

From: Justin Pryzby <[email-masked]>
Date: Mon, 9 Mar 2020 01:00:42 -0500

pg_ls_logdir, pg_ls_waldir, pg_ls_archive_statusdir

..FLAG_ISDIR is collapsed into FLAG_METADATA.

Need catversion bump
---
 doc/src/sgml/func.sgml          | 39 +++++++++++++++++--------------
 src/backend/utils/adt/genfile.c | 41 +++++++++++----------------------
 src/include/catalog/pg_proc.dat | 20 ++++++++--------
 3 files changed, 46 insertions(+), 54 deletions(-)

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 672cbab7b9..d0b782d803 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -21348,8 +21348,9 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
        </entry>
        <entry><type>setof record</type></entry>
        <entry>
-        List the name, size, and last modification time of files in the log
-        directory. Access is granted to members of the <literal>pg_monitor</literal>
+        For each file in the log directory, list the file's name, size, last
+        modification time, and a boolean indicating if it is a directory.
+        Access is granted to members of the <literal>pg_monitor</literal>
         role and may be granted to other non-superuser roles.
        </entry>
       </row>
@@ -21359,8 +21360,9 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
        </entry>
        <entry><type>setof record</type></entry>
        <entry>
-        List the name, size, and last modification time of files in the WAL
-        directory. Access is granted to members of the <literal>pg_monitor</literal>
+        For each file in the WAL directory, list the file's name, size, last
+        modification time, and a boolean indicating if it is a directory.
+        Access is granted to members of the <literal>pg_monitor</literal>
         role and may be granted to other non-superuser roles.
        </entry>
       </row>
@@ -21370,8 +21372,9 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
        </entry>
        <entry><type>setof record</type></entry>
        <entry>
-        List the name, size, and last modification time of files in the WAL
-        archive status directory. Access is granted to members of the
+        For each file in the WAL archive status directory, list the file's
+        name, size, last modification time, and a boolean indicating if it is a
+        directory.  Access is granted to members of the
         <literal>pg_monitor</literal> role and may be granted to other
         non-superuser roles.
        </entry>
@@ -21459,36 +21462,38 @@ SELECT * FROM (SELECT DISTINCT COALESCE(NULLIF(pg_tablespace_location(b.oid),'')
     <primary>pg_ls_logdir</primary>
    </indexterm>
    <para>
-    <function>pg_ls_logdir</function> returns the name, size, and last modified time
-    (mtime) of each file in the log directory. By default, only superusers
+    <function>pg_ls_logdir</function> lists each file in the log directory,
+    along with file's size, last modification time, and a boolean
+    indicating if the file is a directory.  By default, only superusers
     and members of the <literal>pg_monitor</literal> role can use this function.
     Access may be granted to others using <command>GRANT</command>.
-    Filenames beginning with a dot, directories, and other special files are not shown.
+    Filenames beginning with a dot and special file types are not shown.
    </para>
 
    <indexterm>
     <primary>pg_ls_waldir</primary>
    </indexterm>
    <para>
-    <function>pg_ls_waldir</function> returns the name, size, and last modified time
-    (mtime) of each file in the write ahead log (WAL) directory. By
-    default only superusers and members of the <literal>pg_monitor</literal> role
+    <function>pg_ls_waldir</function> lists each file in the WAL directory,
+    along with the file's size, last modification time, and a boolean
+    indicating if the file is a directory.  By default, only superusers
+    and members of the <literal>pg_monitor</literal> role
     can use this function. Access may be granted to others using
     <command>GRANT</command>.
-    Filenames beginning with a dot, directories, and other special files are not shown.
+    Filenames beginning with a dot and special file types are not shown.
    </para>
 
    <indexterm>
     <primary>pg_ls_archive_statusdir</primary>
    </indexterm>
    <para>
-    <function>pg_ls_archive_statusdir</function> returns the name, size, and
-    last modified time (mtime) of each file in the WAL archive status
-    directory <filename>pg_wal/archive_status</filename>. By default only
+    <function>pg_ls_archive_statusdir</function> lists each file in the WAL
+    archive status directory, along with the file's size, last modification
+    time, and a boolean indicating if the file is a directory.  By default, only 
     superusers and members of the <literal>pg_monitor</literal> role can
     use this function. Access may be granted to others using
     <command>GRANT</command>.
-    Filenames beginning with a dot, directories, and other special files are not shown.
+    Filenames beginning with a dot and special file types are not shown.
    </para>
 
    <indexterm>
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index 4ce39516d7..387114d4ee 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -44,12 +44,10 @@ typedef struct
 
 static Datum pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, int flags);
 
-#define	FLAG_ISDIR				(1<<0) /* Show column: isdir */
-#define	FLAG_METADATA			(1<<1) /* Show columns: mtime, size */
-#define	FLAG_MISSING_OK			(1<<2) /* Ignore ENOENT if the toplevel dir is missing */
-#define	FLAG_SKIP_DOT_DIRS		(1<<3) /* Do not show . or .. */
-#define	FLAG_SKIP_HIDDEN		(1<<4) /* Do not show anything begining with . */
-#define	FLAG_SKIP_DIRS			(1<<5) /* Do not show directories */
+#define	FLAG_METADATA			(1<<0) /* Show columns: mtime, size */
+#define	FLAG_MISSING_OK			(1<<1) /* Ignore ENOENT if the toplevel dir is missing */
+#define	FLAG_SKIP_DOT_DIRS		(1<<2) /* Do not show . or .. */
+#define	FLAG_SKIP_HIDDEN		(1<<3) /* Do not show anything begining with . */
 
 /*
  * Convert a "text" filename argument to C string, and check it's allowable.
@@ -482,11 +480,6 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, int flags)
 	struct dirent *de;
 	directory_fctx *fctx;
 
-	/* isdir depends on metadata */
-	Assert(!(flags&FLAG_ISDIR) || (flags&FLAG_METADATA));
-	/* Unreasonable to show isdir and skip dirs */
-	Assert(!(flags&FLAG_ISDIR) || !(flags&FLAG_SKIP_DIRS));
-
 	/* check the optional arguments */
 	if (PG_NARGS() == 3)
 	{
@@ -517,8 +510,7 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, int flags)
 
 		fctx = palloc(sizeof(directory_fctx));
 
-		tupdesc = CreateTemplateTupleDesc((flags&FLAG_ISDIR) ? 4 :
-				(flags&FLAG_METADATA) ? 3 : 1);
+		tupdesc = CreateTemplateTupleDesc((flags&FLAG_METADATA) ? 4 : 1);
 		TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
 						   TEXTOID, -1, 0);
 		if (flags&FLAG_METADATA)
@@ -527,9 +519,8 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, int flags)
 							   INT8OID, -1, 0);
 			TupleDescInitEntry(tupdesc, (AttrNumber) 3, "modification",
 							   TIMESTAMPTZOID, -1, 0);
-			if (flags&FLAG_ISDIR)
-				TupleDescInitEntry(tupdesc, (AttrNumber) 4, "isdir",
-						BOOLOID, -1, 0);
+			TupleDescInitEntry(tupdesc, (AttrNumber) 4, "isdir",
+								BOOLOID, -1, 0);
 		}
 
 		funcctx->tuple_desc = BlessTupleDesc(tupdesc);
@@ -584,10 +575,7 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, int flags)
 					 errmsg("could not stat file \"%s\": %m", path)));
 
 		if (S_ISDIR(attrib.st_mode))
-		{
-			if (flags&FLAG_SKIP_DIRS)
-				continue;
-		}
+			; /* Do nothing, fall through */
 		else if (!S_ISREG(attrib.st_mode))
 			continue;
 
@@ -596,8 +584,7 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, int flags)
 			values[0] = CStringGetTextDatum(de->d_name);
 			values[1] = Int64GetDatum((int64) attrib.st_size);
 			values[2] = TimestampTzGetDatum(time_t_to_timestamptz(attrib.st_mtime));
-			if (flags & FLAG_ISDIR)
-				values[3] = BoolGetDatum(S_ISDIR(attrib.st_mode));
+			values[3] = BoolGetDatum(S_ISDIR(attrib.st_mode));
 		} else
 			SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(de->d_name));
 
@@ -616,7 +603,7 @@ Datum
 pg_ls_logdir(PG_FUNCTION_ARGS)
 {
 	return pg_ls_dir_files(fcinfo, Log_directory,
-			FLAG_MISSING_OK|FLAG_SKIP_DIRS|FLAG_SKIP_HIDDEN|FLAG_METADATA);
+			FLAG_MISSING_OK|FLAG_SKIP_HIDDEN|FLAG_METADATA);
 }
 
 /* Function to return the list of files in the WAL directory */
@@ -624,7 +611,7 @@ Datum
 pg_ls_waldir(PG_FUNCTION_ARGS)
 {
 	return pg_ls_dir_files(fcinfo, XLOGDIR,
-			FLAG_SKIP_DIRS|FLAG_SKIP_HIDDEN|FLAG_METADATA);
+			FLAG_SKIP_HIDDEN|FLAG_METADATA);
 }
 
 /*
@@ -643,7 +630,7 @@ pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc)
 
 	TempTablespacePath(path, tblspc);
 	return pg_ls_dir_files(fcinfo, path,
-			FLAG_MISSING_OK|FLAG_SKIP_HIDDEN|FLAG_METADATA|FLAG_ISDIR);
+			FLAG_MISSING_OK|FLAG_SKIP_HIDDEN|FLAG_METADATA);
 }
 
 /*
@@ -673,7 +660,7 @@ Datum
 pg_ls_archive_statusdir(PG_FUNCTION_ARGS)
 {
 	return pg_ls_dir_files(fcinfo, XLOGDIR "/archive_status",
-			FLAG_MISSING_OK|FLAG_SKIP_DIRS|FLAG_SKIP_HIDDEN|FLAG_METADATA);
+			FLAG_MISSING_OK|FLAG_SKIP_HIDDEN|FLAG_METADATA);
 }
 
 /*
@@ -684,5 +671,5 @@ pg_ls_dir_metadata(PG_FUNCTION_ARGS)
 {
 	char	*dirname = convert_and_check_filename(PG_GETARG_TEXT_PP(0));
 
-	return pg_ls_dir_files(fcinfo, dirname, FLAG_METADATA|FLAG_ISDIR);
+	return pg_ls_dir_files(fcinfo, dirname, FLAG_METADATA);
 }
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index cc2c6f6571..0e5a570285 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6149,7 +6149,7 @@
   provolatile => 'v', prorettype => 'record', proargtypes => 'text',
   proallargtypes => '{text,text,int8,timestamptz,bool}',
   proargnames => '{dirname,name,size,modification,isdir}', proargmodes => '{i,o,o,o,o}',
-  prolang => 'sql', prosrc => "WITH RECURSIVE x AS (SELECT * FROM pg_ls_dir_metadata(dirname, true, false, true) UNION ALL SELECT x.name||'/'||a.name, a.size, a.modification, a.isdir FROM x, pg_ls_dir_metadata(dirname||'/'||x.name, true, false, true)a WHERE x.isdir) SELECT * FROM x" },
+  prolang => 'sql', prosrc => "WITH RECURSIVE x AS (SELECT * FROM pg_ls_dir_metadata(dirname, true, false) UNION ALL SELECT x.name||'/'||a.name, a.size, a.modification, a.isdir FROM x, pg_ls_dir_metadata(dirname||'/'||x.name, true, false)a WHERE x.isdir) SELECT * FROM x" },
 
 { oid => '2626', descr => 'sleep for the specified time in seconds',
   proname => 'pg_sleep', provolatile => 'v', prorettype => 'void',
@@ -10725,18 +10725,18 @@
 { oid => '3353', descr => 'list files in the log directory',
   proname => 'pg_ls_logdir', procost => '10', prorows => '20', proretset => 't',
   provolatile => 'v', prorettype => 'record', proargtypes => '',
-  proallargtypes => '{text,int8,timestamptz}', proargmodes => '{o,o,o}',
-  proargnames => '{name,size,modification}', prosrc => 'pg_ls_logdir' },
+  proallargtypes => '{text,int8,timestamptz,bool}', proargmodes => '{o,o,o,o}',
+  proargnames => '{name,size,modification,isdir}', prosrc => 'pg_ls_logdir' },
 { oid => '3354', descr => 'list of files in the WAL directory',
   proname => 'pg_ls_waldir', procost => '10', prorows => '20', proretset => 't',
   provolatile => 'v', prorettype => 'record', proargtypes => '',
-  proallargtypes => '{text,int8,timestamptz}', proargmodes => '{o,o,o}',
-  proargnames => '{name,size,modification}', prosrc => 'pg_ls_waldir' },
+  proallargtypes => '{text,int8,timestamptz,bool}', proargmodes => '{o,o,o,o}',
+  proargnames => '{name,size,modification,isdir}', prosrc => 'pg_ls_waldir' },
 { oid => '5031', descr => 'list of files in the archive_status directory',
   proname => 'pg_ls_archive_statusdir', procost => '10', prorows => '20',
   proretset => 't', provolatile => 'v', prorettype => 'record',
-  proargtypes => '', proallargtypes => '{text,int8,timestamptz}',
-  proargmodes => '{o,o,o}', proargnames => '{name,size,modification}',
+  proargtypes => '', proallargtypes => '{text,int8,timestamptz,bool}',
+  proargmodes => '{o,o,o,o}', proargnames => '{name,size,modification,isdir}',
   prosrc => 'pg_ls_archive_statusdir' },
 { oid => '5029', descr => 'list files in the pgsql_tmp directory',
   proname => 'pg_ls_tmpdir', procost => '10', prorows => '20', proretset => 't',
@@ -10751,9 +10751,9 @@
   prosrc => 'pg_ls_tmpdir_1arg' },
 { oid => '5032', descr => 'list directory with metadata',
   proname => 'pg_ls_dir_metadata', procost => '10', prorows => '20', proretset => 't',
-  provolatile => 'v', prorettype => 'record', proargtypes => 'text bool bool bool',
-  proallargtypes => '{text,bool,bool,bool,text,int8,timestamptz,bool}', proargmodes => '{i,i,i,i,o,o,o,o}',
-  proargnames => '{dirname,missing_ok,include_dot_dirs,dir_ok,name,size,modification,isdir}',
+  provolatile => 'v', prorettype => 'record', proargtypes => 'text bool bool',
+  proallargtypes => '{text,bool,bool,text,int8,timestamptz,bool}', proargmodes => '{i,i,i,o,o,o,o}',
+  proargnames => '{dirname,missing_ok,include_dot_dirs,name,size,modification,isdir}',
   prosrc => 'pg_ls_dir_metadata' },
 
 # hash partitioning constraint function
-- 
2.17.0

--32u276st3Jlj2kUU
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
 filename="v9-0011-pg_ls_-dir-to-return-all-the-metadata-from-pg_sta.patch"