Wednesday, July 31, 2024

Comparing differences between objects in two IFS directories

While it has been possible to compare the contents of two directories in the IFS it has not been, well, simple. A new addition to Db2 for i, SQL, in the latest round of Technology Refreshes, IBM i 7.5 TR4 and IBM i 7.4 TR10. Makes it a whole lot, well, easier.

The new SQL Table function, COMPARE_IFS uses the output from the Table functions IFS_OBJECT_STATISTICS and IFS_OBJECT_PRIVILEGES to compare the objects in two IFS directories to one another. It will only check the following object types:

  • Character special files, *CHRSF
  • Directories, *DIR
  • Name pipes, *FIFO
  • Local sockets, *SOCKET
  • Stream files, *STMF
  • Symbolic links, *SYMLNK

I created two subfolders within my home directory, /home/MyDir, called subdir1 and subdir2. I uploaded some files into each one. Some were the same files, others were different files, and some files had the same names but different contents.

I can show what is in subdir1 using the IFS_OBJECT_STATISTICS Table function:

01  SELECT PATH_NAME,OBJECT_TYPE 
02  FROM TABLE (QSYS2.IFS_OBJECT_STATISTICS(
03        START_PATH_NAME => '/home/MyDir/subdir1',
04        SUBTREE_DIRECTORIES => 'NO',
05        OBJECT_TYPE_LIST => '*ALLSTMF'))

Lines 3 – 5: I have used the parameter names to make it easier what value I am passing to each parameter.

Line 3: I only want to list the contents of the directory subdir1.

Line 4: I do not want to list any objects that are in any subdirectories within subdir1.

Line 5: I want to return the details for all stream files in this directory.

The results are:

                                      OBJECT
PATH_NAME                             _TYPE   CREATE_TIMESTAMP
-----------------------------------   ------  --------------------
/home/MyDir/subdir1/text_file_2.txt   *STMF   2024-07-01 08:30:01
/home/MyDir/subdir1/csv_file_1.csv    *STMF   2024-07-01 08:30:47
/home/MyDir/subdir1/pdf_file_1.pdf    *STMF   2024-07-01 08:31:41
/home/MyDir/subdir1/text_file_1.txt   *STMF   2024-07-01 08:33:15

I can use a similar statement to show the contents of subdir2.

01  SELECT PATH_NAME,OBJECT_TYPE 
02  FROM TABLE (QSYS2.IFS_OBJECT_STATISTICS(
03        '/home/MyDir/subdir2','NO','*ALLSTMF'))

Line 3: This time I decided just to list the parameter values.

The results are as follows:

                                     OBJECT
PATH_NAME                            _TYPE   CREATE_TIMESTAMP
-----------------------------------  ------  -------------------
/home/MyDir/subdir2/csv_file_1.csv   *STMF   2024-07-01 08:34:30
/home/MyDir/subdir2/pdf_file_1.pdf   *STMF   2024-07-01 08:34:57
/home/MyDir/subdir2/text_file_1.txt  *STMF   2024-07-01 08:35:44
/home/MyDir/subdir2/xls_file_1.xls   *STMF   2024-07-01 08:36:40

The COMPARE_IFS Table function is found in the library QSYS2. It has six parameters:

  1. START_PATH_NAME1:  Path name of the first directory
  2. START_PATH_NAME2:  Path name of the second directory that is to be compared to the first directory
  3. RDB2:  If I want to compare directories in different databases, or partitions as we call them in IBM i "speak", this is where I would enter the name of the other database/partition
  4. SUBTREE_DIRECTORIES:  Whether to include the subdirectories. The default is "YES", or I can enter "NO" not to.
  5. OBJECT_TYPE_LIST:  I can enter "*ALL" or enter the object types I mentioned above.
  6. COMPARE_ATTRIBUTES:  I can enter wither "NAMES" which compares just the names in the two directories, this is the default. Or I can enter "YES" where the attributes of the contents of the folders is compared

Results are only returned if differences are found. If everything is the same then no results are returned.

There are five columns returned in the results:

  1. PATH_NAME1:  Path name of the first object
  2. PATH_NAME2:  Path name of the second object
  3. ATTRIBUTE_NAME:  What is not the same
  4. VALUE1:  Attribute value for the first object
  5. VALUE2:  Attribute value for the second object

In my first example I am comparing if the objects in both directories match by name.

01  SELECT * 
02  FROM TABLE(QSYS2.COMPARE_IFS(
03    START_PATH_NAME1 => '/home/MyDir/subdir1',
04    START_PATH_NAME2 => '/home/MyDir/subdir2',
05    SUBTREE_DIRECTORIES => 'NO',
06    OBJECT_TYPE_LIST => '*ALL',
07    COMPARE_ATTRIBUTES => 'NAMES')) 

Line 5: I don't want to check in any subdirectories either of these directories may have.

Line 6: I want to compare all of the eligible object types.

Line 7: I just want to compare names only.

There are two results:

PATH_NAME_1                           PATH_NAME_2
-----------------------------------   ---------------------------------------
/home/MyDir/subdir1/text_file_2.txt   <NULL>
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls

ATTRIBUTE_NAME                                      VALUE1          VALUE_2
--------------------------------------------------  --------------- --------------
Unmatched PATH_NAME in QSYS2.IFS_OBJECT_STATISTICS  text_file_2.txt <NULL>
Unmatched PATH_NAME in QSYS2.IFS_OBJECT_STATISTICS  <NULL>          xls_file_1.xls

The results show that there is a file, text_file_2.txt, that is in the first directory, but not in the second. And there is a file, xls_file_1.xls, that is in the second directory, and not in the first.

Now for the more complicated one. I want to compare all the attributes if there are files with the same name in both folders. My statement to do this would be:

01  SELECT * 
02  FROM TABLE(QSYS2.COMPARE_IFS(
03    START_PATH_NAME1 => '/home/MyDir/subdir1',
04    START_PATH_NAME2 => '/home/MyDir/subdir2',
05    SUBTREE_DIRECTORIES => 'NO',
06    OBJECT_TYPE_LIST => '*ALL',
07    COMPARE_ATTRIBUTES => 'YES'))

Line 5: This is the only change, now I want to check all attributes.

There are a lot of results for these few objects:

PATH_NAME_1                           PATH_NAME_2
-----------------------------------   ---------------------------------------
/home/MyDir/subdir1                   /home/MyDir/subdir2
/home/MyDir/subdir1/csv_file_1.csv    /home/MyDir/subdir2/csv_file_1.csv
/home/MyDir/subdir1/pdf_file_1.pdf    /home/MyDir/subdir2/pdf_file_1.pdf
/home/MyDir/subdir1/text_file_1.txt   /home/MyDir/subdir2/text_file_1.txt
/home/MyDir/subdir1/text_file_1.txt   /home/MyDir/subdir2/text_file_1.txt
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
/home/MyDir/subdir1/text_file_2.txt   <NULL>
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls
<NULL>                                /home/MyDir/subdir2/xls_file_1.xls

ATTRIBUTE_NAME
---------------------------------------------------------
Unmatched CREATE_TIMESTAMP in QSYS2.IFS_OBJECT_STATISTICS
Unmatched CREATE_TIMESTAMP in QSYS2.IFS_OBJECT_STATISTICS
Unmatched CREATE_TIMESTAMP in QSYS2.IFS_OBJECT_STATISTICS
Unmatched CREATE_TIMESTAMP in QSYS2.IFS_OBJECT_STATISTICS
Unmatched DATA_SIZE in QSYS2.IFS_OBJECT_STATISTICS
Unmatched PATH_NAME in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_TYPE in QSYS2.IFS_OBJECT_STATISTICS
Unmatched ASP_NUMBER in QSYS2.IFS_OBJECT_STATISTICS
Unmatched CREATE_TIMESTAMP in QSYS2.IFS_OBJECT_STATISTICS
Unmatched DATA_SIZE in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_OWNER in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_AUDIT in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_AUDIT_CREATE in QSYS2.IFS_OBJECT_STATISTICS
Unmatched JOURNALED in QSYS2.IFS_OBJECT_STATISTICS
Unmatched PATH_NAME in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_TYPE in QSYS2.IFS_OBJECT_STATISTICS
Unmatched ASP_NUMBER in QSYS2.IFS_OBJECT_STATISTICS
Unmatched CREATE_TIMESTAMP in QSYS2.IFS_OBJECT_STATISTICS
Unmatched DATA_SIZE in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_OWNER in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_AUDIT in QSYS2.IFS_OBJECT_STATISTICS
Unmatched OBJECT_AUDIT_CREATE in QSYS2.IFS_OBJECT_STATISTICS
Unmatched JOURNALED in QSYS2.IFS_OBJECT_STATISTICS

VALUE1                VALUE2
-------------------   -------------------
2024-07-01-08.26.13   2024-07-01-08.26.23
2024-07-01-08.30.47   2024-07-01-08.34.30
2024-07-01-08.31.41   2024-07-01-08.34.57
2024-07-01-08.33.15   2024-07-01-08.35.44
10                    30
text_file_2.txt       <NULL>
*STMF                 <NULL>
1                     <NULL>
2024-07-01-08.30.01   <NULL>
10                    <NULL>
RPGPGM                <NULL>
*NONE                 <NULL>	
*SYSVAL               <NULL>
NO                    <NULL>
<NULL>                xls_file_1.xls
<NULL>                *STMF
<NULL>                1
<NULL>                2024-07-01-08.36.40
<NULL>                153088
<NULL>                RPGPGM
<NULL>                *NONE
<NULL>                *SYSVAL
<NULL>                NO

The data in the Attribute name column shows which source the information was gathered.

As I mentioned I can compare the contents of two directories on different partitions. This statement will compare the names of the objects in the same folder in two different partitions, the current one I am on, and in another called OTHERSYS:

01  SELECT * 
02  FROM TABLE(QSYS2.COMPARE_IFS(
03    START_PATH_NAME1 => '/home/MyDir/subdir1',
04    START_PATH_NAME2 => '/home/MyDir/subdir1',
05    RDB2 => 'OTHERSYS',
06    SUBTREE_DIRECTORIES => 'NO',
07    OBJECT_TYPE_LIST => '*ALL',
08    COMPARE_ATTRIBUTES => 'NAMES'))

Line 5: Here is where I give the name of the remote database, or partition name.

Line 8: I am comparing the names of the objects in both directories.

There are no results returned as the two directories have the same named objects within them.

 

You can learn more about the changes to Compare IFS SQL Table function from the IBM website here.

 

This article was written for IBM i 7.5 TR4 and 7.4 TR10.

No comments:

Post a Comment

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.