Use the Save Object List API.
I was recently asked by Vasilis F. if there was a way to save specific user profiles from one system and then restore those profiles to one or more other systems. He had found the Restore User Profile (RSTUSRPRF) command, which allows you to restore from 1 to 300 specific profiles but, strangely enough, no Save User Profile command in order to save those specific profiles. Rather, the help text for RSTUSRPRF points to the Save System (SAVSYS) and Save Security Data (SAVSECDTA) commands, both of which are a bit of overkill when you just want to duplicate three user profiles (out of hundreds) to several other systems. Fortunately, the Save Object List (QSRSAVO) API supports, among many other capabilities, the saving of a specific list of user profiles.
To simplify the saving of the user profiles, we'll first create the command SAVUSRPRFS. Note that I am intentionally adding an "S" to the end of the command name so that, if IBM ever comes out with a SAVUSRPRF command, our command will (hopefully) not have a name conflict. The command definition is:
Cmd Prompt('Save User Profiles')
Parm Kwd(UsrPrf) Type(*Name) +
Min(1) Max(300) +
Prompt('User profiles')
Parm Kwd(SavF) Type(QualObj) +
Prompt('Save file')
QualObj: Qual Type(*Name) Len(10) Dft(SavUsrPrfs)
Qual Type(*Name) Len(10) Dft(*Libl) +
SpcVal((*Libl) (*CurLib)) +
Prompt('Library')
The command requires that you specify at least one user profile (*USRPRF) that is to be saved and allows up to 300 *USRPRFs. An optional second parameter on the command, SAVF, allows you to also specify the qualified name of a save file where the *USRPRF information will be saved. The default for this parameter is a save file named SAVUSRPRF that can be found in the job's current library list. The user of the SAVUSRPRFS command is responsible for creating this save file.
Assuming that you have stored the above command definition in source file QCMDSRC and that the library containing QCMDSRC is in your current library list, then you can create the SAVUSRPRFS command with the following command.
CrtCmd Cmd(SavUsrPrfs) Pgm(SavUsrPrf)
This creates the SAVUSRPRFS command into your current library and specifies that the command processing program (CPP) for the command is the program SAVUSRPRF. The source for the SAVUSRPRF CPP is shown below.
h dftactgrp(*no)
d SavUsrPrf pr
d NbrPrfs_In 5i 0
d SavF_In 20a
d SavUsrPrf pi
d NbrPrfs_In 5i 0
d SavF_In 20a
**********************************************************
d ChgUsrSpcA pr extpgm('QUSCUSAT')
d RtnUsrSpcLib 10a
d QualUsrSpcN 20a const
d UsrSpcAttrs 1a const options(*varsize)
d ErrCde likeds(QUSEC)
d CrtUsrSpc pr extpgm('QUSCRTUS')
d QualUsrSpcN 20a const
d XAttr 10a const
d IntSize 10i 0 const
d IntValue 1a const
d PubAut 10a const
d TxtDesc 50a const
d ReplaceOpt 10a const options(*nopass)
d ErrCde likeds(QUSEC) options(*nopass)
d Domain 10a const options(*nopass)
d TfrSize 10i 0 const options(*nopass)
d OptSpcAlgn 1a const options(*nopass)
d FinishEntry pr
d RtvUsrSpcPtr pr extpgm('QUSPTRUS')
d QualUsrSpcN 20a const
d UsrSpcPtr *
d ErrCde likeds(QUSEC) options(*nopass)
d SavObjLst pr extpgm('QSRSAVO')
d QualUsrSpcN 20a const
d ErrCde likeds(QUSEC)
**********************************************************
d UsrSpc_Ptr s *
d SRUS ds likeds(QSRUS)
d based(UsrSpc_Ptr)
d SRR_Ptr s *
d SRR ds likeds(QSRR)
d based(SRR_Ptr)
d ObjEntry_Ptr s *
d ObjEntryHdr ds qualified
d based(ObjEntry_Ptr)
d Nbr 10i 0
d ObjEntry ds qualified
d based(ObjEntry_Ptr)
d Name 10a
d Type 10a
d LibEntry_Ptr s *
d LibEntry ds qualified
d based(LibEntry_Ptr)
d Nbr 10i 0
d Library 10a
d DevEntry_Ptr s *
d DevEntry ds qualified
d based(DevEntry_Ptr)
d Nbr 10i 0
d Device 10a
d QualUsrSpcN ds qualified
d Name 10a inz('SAVUSRPRFS')
d Library 10a inz('QTEMP')
d UsrSpcAttrs ds qualified
d NbrAttrs 10i 0 inz(1)
d AutoXtndKey 10i 0 inz(3)
d LenKeyVal 10i 0 inz(1)
d AutXtndYes 1a inz('1')
d 3a
d ErrCde ds qualified
d Hdr likeds(QUSEC)
d MsgDta 256a
**********************************************************
d AlignOn s 10i 0 inz(4)
d Profile_Ptr s *
d Profile_In s 10a based(Profile_Ptr)
d RtnUsrSpcLib s 10a
d SavFEntry_Ptr s *
d SavFEntry s 20a based(SavFEntry_Ptr)
d X s 10i 0
**********************************************************
/copy qsysinc/qrpglesrc,qsr
/copy qsysinc/qrpglesrc,qusec
/free
// Set Key 1 values (List of user profiles)
SRR.QSRKNbr = 1;
SRR.QSRDL = %size(ObjEntryHdr) +
(%size(ObjEntry) * NbrPrfs_In);
ObjEntry_Ptr = SRR_Ptr + %size(SRR);
ObjEntryHdr.Nbr = NbrPrfs_In;
for X = 1 to NbrPrfs_In;
if X = 1;
Profile_Ptr = %addr(NbrPrfs_In) +
%size(NbrPrfs_In);
ObjEntry_Ptr += %size(ObjEntryHdr);
else;
Profile_Ptr += %size(Profile_In);
ObjEntry_Ptr += %size(ObjEntry);
endif;
ObjEntry.Name = Profile_In;
ObjEntry.Type = '*USRPRF';
endfor;
FinishEntry();
// Set Key 2 values (Library)
SRR.QSRKNbr = 2;
SRR.QSRDL = %size(LibEntry);
LibEntry_Ptr = SRR_Ptr + %size(SRR);
LibEntry.Nbr = 1;
LibEntry.Library = 'QSYS';
FinishEntry();
// Set Key 3 values (Device)
SRR.QSRKNbr = 3;
SRR.QSRDL = %size(DevEntry);
DevEntry_Ptr = SRR_Ptr + %size(SRR);
DevEntry.Nbr = 1;
DevEntry.Device = '*SAVF';
FinishEntry();
// Set Key 4 values (Save file)
SRR.QSRKNbr = 4;
SRR.QSRDL = %size(SavFEntry);
SavFEntry_Ptr = SRR_Ptr + %size(SRR);
SavFEntry = SavF_In;
FinishEntry();
// Perform the save
SavObjLst(QualUsrSpcN :QUSEC);
*inlr = *on;
return;
begsr *inzsr;
// Set API QUSEC parameter to send exceptions
QUSBPrv = 0;
// Set API ErrCde parameter to not send exceptions
ErrCde.Hdr.QUSBPrv = %size(ErrCde);
RtvUsrSpcPtr(QualUsrSpcN :UsrSpc_Ptr :ErrCde);
select;
when ErrCde.Hdr.QUSBAvl = 0;
// All is OK
when ErrCde.Hdr.QUSEI = 'CPF9801';
// UsrSpc not found, so create it
CrtUsrSpc(QualUsrSpcN :' ' :512
:x'00' :'*ALL' :'QSRSAVO input values'
:'*YES' :QUSEC :'*DEFAULT' :0 :'1');
// Set user space to autoextend yes
ChgUsrSpcA(RtnUsrSpcLib :QualUsrSpcN
:UsrSpcAttrs :QUSEC);
// Get accessibility to user space
RtvUsrSpcPtr(QualUsrSpcN :UsrSpc_Ptr :QUSEC);
other;
// Something seriously wrong. Send Escape
endsl;
// Initialize current number of records
SRUS.QSRNbrR = 0;
// Set SRR_Ptr to first record entry
SRR_Ptr = UsrSpc_Ptr + %size(SRUS);
endsr;
/end-free
**********************************************************
p FinishEntry b
d FinishEntry pi
/free
if %rem(SRR.QSRDL :AlignOn) <> 0;
SRR.QSRRL00 = %size(SRR) + SRR.QSRDL +
(AlignOn - %rem(SRR.QSRDL :AlignOn));
else;
SRR.QSRRL00 = %size(SRR) + SRR.QSRDL;
endif;
SRUS.QSRNbrR += 1;
SRR_Ptr += SRR.QSRRL00;
/end-free
p FinishEntry e
The QSRSAVO API, like many other APIs that support a wide range of options (in this case the various options/keywords found on various SAV* CL commands), uses a keyed approach with variable-length records. Each variable-length record starts with three fixed fields, which represent the length of the current record (used to locate the next variable-length record), the key of the current record, and the length of the data associated with the current key, respectively. A fourth field of the variable-length record then provides the data associated with the key. A key value of 1, for instance, is used to identify the objects and object types to be saved with the fourth field containing the list of objects to be saved. This corresponds (loosely) to the OBJ and OBJTYPE keywords of the SAVOBJ command. A key value of 11 is used to identify the target release for the save and corresponds to the TGTRLS keyword of the SAVOBJ command; a key value of 20 identifies whether or not the storage associated with various object types should be freed, corresponding to the STG keyword of the SAVOBJ command; and so on.
For the purposes of saving user profiles, we will be using the following keys:
- 1—The list of *USRPRFs to save (specified by the USRPRF keyword of the SAVUSRPRFS command)
- 2—The library where the *USRPRFs are found (QSYS)
- 3—The device to be used for the save operation (a *SAVF)
- 4—The qualified name of the *SAVF (specified by the SAVF keyword of the SAVUSRPRFS command)
Many system APIs would defined the parameter where you provide this keyed information as a variable length input parameter—that is, a parameter defined as type Char(*). Though not unique to the QSRSAVO API, QSRSAVO takes the approach of passing this keyed information in a user space (*USRSPC). The qualified name of the *USRSPC is the first parameter passed to QSRSAVO. This use of a *USRSPC (rather than just a parameter being passed by our program) could be handy if we wanted to save the same list of objects every night. In that case, we could format the *USRSPC once and then reference the *USRSPC every time we wanted to save the objects, thereby avoiding the need to reformat the *USRSPC contents every time we wanted to save the objects. In the case of our SAVUSRPRF CPP, we will create the *USRSPC into QTEMP as I don't see any need (with our sample program anyway) to retain the formatted contents of the *USRSPC). Another possible use of this *USRSPC, if you're careful about the key values used, is that the *USRSPC could be used as an input to the Restore Object List (QSRRSTO) API (though this reuse does not apply to our saving of *USRRPFs due to the need to later issue commands such as RSTUSRPRF and RSTAUT). The second parameter passed to the QSRSAVO API is the standard API error-code structure.
Before getting into the specifics of the SAVUSRPRF program, I want to point out that this program uses pointers extensively. If you have a phobia concerning pointers—or worse, your company forbids the use of pointers—this implementation of the SAVUSRPRF program is most likely not for you. In this case, feel free to send me a note and I will provide a few other implementations of the program—approaches that do not require the use of pointers. These non-pointer-based implementations may not run as fast as the version provided above (note that this does not mean that they'll run like molasses, just that they will not run as fast), be as flexible, or, to my way of thinking anyway, be as "clean" to follow, but they will get the job done. The one thing I won't do is provide a fixed-form RPG implementation; that's just asking too much. J
The *INZSR subroutine of the SAVUSRPRF program is used to establish the environment for the mainline of the program. The processing found in the *INZSR is:
- Initialize the QUSEC error-code data structure to return API errors as escape messages.
- Initialize the ErrCde error-code data structure to not return API errors as escape messages but rather as structured feedback information that is available to the SAVUSRPRF program for recovery purposes.
- Attempt, using the ErrCde error-code data structure, to access a space pointer to the *USRSPC QTEMP/SAVUSRPRFS using the Retrieve Pointer to User Space (QUSPTRUS) API. If successful, the pointer value is returned in variable UsrSpc_Ptr. This pointer addresses the first available byte of the *USRSPC.
- If an error is encountered accessing the *USRSPC (that is, ErrCde.Hdr.QUSBAvl is not 0), then determine what the problem is:
- If the error is related to the *USRSPC not being found (ErrCde.Hdr.QUSEI is CPF9801), then create the *USRSPC using the Create User Space (QUSCRTUS) API, use the Change User Space Attributes (QUSCUSAT) API to set the *USRSPC to being auto-extendable as we don't know how big a *USRSPC we need (we could figure it out, but it's so much easier to just let the system automatically grow the *USRSPC to the size we need), and then attempt again to access a pointer to the *USRSPC using QUSPTRUS. In these calls to QUSCRTUS, QUSCUSAT, and QUSPTRUS, we use the QUSEC error-code data structure so that any errors encountered here will be returned as escape messages.
- If the error is not related to the *USRSPC not being found, then, though not shown, the program should send an escape message to the user, reporting what the problem is. This sending of an escape message can be found in many earlier "API Corner" programs (see "More on Sending Messages from an Application Program" for one article that discusses this).
- Initialize the variable SRUS.QSRNbrR to 0. Per the documentation for the QSRSAVO API, the first four bytes of the *USRSPC are used to tell the API how many variable-length records are to be processed within the *USRSPC. The variable SRUS.QSRNbrR is defined as a 4-byte integer based on the pointer UsrSpc_Ptr and, as UsrSpc_Ptr is addressing the first byte of the *USRSPC, we are indicating that there are currently 0 variable-length records.
- Initialize the pointer variable SRR_Ptr to where the next (in the current case, also the first) variable-length record will start. This is done by taking the value of UsrSpc_Ptr and adding to it the size of variable SRUS.QSRNbrR.
Having completed the preparatory work in setting our environment, the main line of SAVUSRPRF builds the various variable-length records, defining what we want to save and where we want to save it. The first record we will format is the list of the user profiles that we want to save. This is done by formatting a variable-length record with a key value of 1.
As mentioned previously, all of the variable-length records have a common header defining three fields: the length of the current record, the key of the current record, and the length of the data associated with the current record's key. This common definition can be found in QSYSINC/QRPGLESRC include member QSR as data structure QSRR. The SAVUSRPRF program defines the data structure SRR as being LikeDS(QSRR) and based on the pointer variable SRR_Ptr. As SRR_Ptr was previously set, in the *INZSR subroutine, to the location of the "next" variable-length record, SAVUSRPRF can simply start formatting this record by setting the key number (SRR.QSrKNbr) to 1 and the data length associated with the key (SRR.QSRDL) to the appropriate length. In reviewing the API documentation, we see that the data associated with key 1 is comprised of two items. The first is a 4-byte integer indicating how many objects are provided in the list of objects to be saved, and the second is the actual list of objects to be saved. Each object entry is 20 bytes in length with the first 10 bytes being an object name, a generic object name, or the special value *ALL; and the second 10 bytes being an object type or a special value. With this information, we can set the data length for key 1 by summing the length of the 4-byte integer representing the number of objects being saved and the size of the object list in bytes. The byte size of the object list can be determined by multiplying the number of user profiles specified by the user (NbrPrfs_In) by the size of each user profile entry (20 bytes or, as shown in the program to avoid hard coding the value 20, %size(ObjEntry)).
In case you are wondering how NbrPrfs_In was set to the number of user profiles specified by the user, it's a function of how we defined the SAVUSRPRFS command. The USRPRF keyword is defined as being a list parameter of Type(*Name) so the system, by default, will pass the list to the CPP as a 2-byte integer count of the number of list entries specified, immediately followed by that number of 10-byte list entries. Reviewing the prototype and procedure interface of SAVUSRPRF, you'll see that NbrPrfs_In is defined as a 2-byte integer being passed as the first parameter to the program and represents the start of the SAVUSRPRFS's USRPRF list.
Having set two of the three variables defined in the SRR header (the third element we'll set in just a bit during procedure FinishEntry() processing), SAVUSRPRF now formats the actual data associated with key 1. We previously saw that the first item associated with key 1 is the number of variable-length entries. As this item immediately follows the SRR header, SAVUSRPRF sets variable ObjEntry_Ptr to the start of the current SRR structure (SRR_Ptr) plus the size of the SRR structure. Variable ObjEntryHdr.Nbr, which is based on ObjEntry_Ptr, is then set to the value of NbrPrfs_In. Following this, the list of user profiles is loaded by way of a FOR loop that is run NbrPrfs_In times.
On the first iteration of the FOR group (X is equal to 1):
- Profile_Ptr is set to address the first *USRPRF name specified by the user of the SAVUSRPRFS command. This is done by taking the address of NbrPrfs_In and adding the size of NbrPrfs_In.
- ObjEntry_Ptr is set to address the location of the first object entry to be passed to the QSRSAVO API. This is done by taking the current value of ObjEntry_Ptr (which is pointing to the number of objects to be saved, ObjEntryHdr.Nbr) and adding the size of ObjEntryHdr.
- The user profile name addressed by Profile_Ptr is then copied to the location addressed by ObjEntry_Ptr, the object type is set to the special value '*USRPRF', and the next iteration of the FOR group is run.
In these subsequent iterations (X is not equal to 1):
- Profile_Ptr is set to address the next *USRPRF name specified by the user of the SAVUSRPRFS command. This is done by taking the current value of Profile_Ptr and adding the size of the profile just processed (%size(Profile_In)).
- ObjEntry_Ptr is set to address the location of the next object entry to be passed to the QSRSAVO API. This is done by taking the current value of ObjEntry_Ptr and adding the size of the ObjEntry just processed (%size(ObjEntry)).
- The user profile name addressed by Profile_Ptr is then copied to the location addressed by ObjEntry_Ptr, the object type is set to the special value '*USRPRF', and the next iteration of the FOR group is run.
When all *USRPRF names have been copied to the list of objects to be saved. then procedure FinishEntry() is run.
The FinishEntry procedure accomplishes a few common tasks. The first task is related to performance.
In reviewing the documentation for the QSRSAVO API. you may have noticed the following note related to the variable-length records passed to the API:
It is recommended, but not required, to align each variable length record on a 4-byte boundary. That is, you should make the length of each variable length record a multiple of 4, even if the data length is not a multiple of 4.
The first addressable byte of a *USRSPC is always aligned on a 4-byte boundary (for that matter, it's also on a 16-byte boundary in case you ever need to align pointers in a *USRSPC), so the first operation done in FinishEntry() is to determine if the length of the current variable-length record is a multiple of 4 (variable AlignOn). If not currently a multiple of 4 (that is, %rem(SRR.QSRDL :AlignOn) is not equal to 0), FinishEntry() determines how many bytes are needed in order to make the current record length a multiple of 4 so that subsequent records will also start on a 4-byte boundary. This padded length is what is then written to the length of the current record (SRR.QSRRL00). If the current length is already a multiple of 4, then the current length is written to SRR.QSRRL00.
The other two tasks of FinishEntry() are similar to the last two tasks done in the *INZSR subroutine in order to prepare the environment for the adding of variable-length records. FinishEntry() records that another variable-length record has been written to the *USRSPC (SRUS.QSRNbrR += 1) and then sets SRR_Ptr to the starting location of the next (if any) variable-length record. We're now ready to start formatting another variable-length record to pass to the QSRSAVO API.
Using the same general approach as was done for key 1, SAVUSRPRF now formats the variable-length records for keys 2, 3, and 4 (where you might notice a certain pattern being repeated for each record):
Key 2:
-
Set SRR.QSRKNbr to the value of 2, indicating a library entry.
-
Set SRR.QSRDL to the size of a library entry (%size(LibEntry)).
-
Set LibEntry_Ptr to the first byte following the SRR structure (SRR_Ptr + %size(SRR)).
-
Record the number of libraries (LibEntry.Nbr = 1).
-
Record the name of the library (LibEntry.Library = 'QSYS').
-
Run the FinishEntry() procedure to prepare for the next variable-length entry.
Key 3:
-
Set SRR.QSRKNbr to the value of 3, indicating a device entry.
-
Set SRR.QSRDL to the size of a device entry (%size(DevEntry)).
-
Set DevEntry_Ptr to the first byte following the SRR structure (SRR_Ptr + %size(SRR)).
-
Record the number of devices (DevEntry.Nbr = 1).
-
Record the type of device (DevEntry.Device = '*SAVF').
-
Run the FinishEntry() procedure to prepare for the next variable-length entry.
Key 4:
-
Set SRR.QSRKNbr to the value of 4, indicating a *SAVF entry.
-
Set SRR.QSRDL to the size of a *SAVF entry (%size(SavFEntry)).
-
Set SavFEntry_Ptr to the first byte following the SRR structure (SRR_Ptr + %size(SRR)).
-
Record the *SAVF qualified name that was passed to the program as the second parameter (SavF_In).
-
Run the FinishEntry() procedure to prepare for the next variable-length entry.
Having successfully formatted the four variable-length records defining what profiles to save and where to save them, the QSRSAVO API is then called using the QUSEC error-code structure so that any error encountered while saving the *USRPRFs will be returned as an escape message. Assuming no error was found, the SAVUSRPRF program then ends and you can move the *SAVF to the system(s) where you want to restore the *USRPRFs.
Assuming that you have stored the above program source in source file QRPGLESRC and that the library containing QRPGLESRC is in your current library list, then you can create the SAVUSRPRF program with the following command.
CrtBndRPG Pgm(SavUsrPrf)
To save *USRPRFs One, Two, and Three, you can now use the following commands to create a *SAVF and save those user profiles.
CrtSavF File(SavUsrPrfs)
SavUsrPrfs UsrPrf(One Two Three)
After moving the SAVUSRPRFS *SAVF to a target system where you want to restore the *USRPRFs, you can run a command such as this:
RstUsrPrf Dev(*SavF) UsrPrf(One Two Three) SavF(SavUsrPrfs)
When running the RSTUSRPF, keep in mind that you may, or may not, want to override the defaults for RSTUSRPRF parameters, such as Security data (SECDTA) and Allow object differences (ALWOBJDIF).
To restore the private authorities associated with one of the *USRPRFs—for instance, profile THREE—you will also want to run the RSTAUT command as in:
RstAut UsrPrf(Three)
Today we've seen how to save specific user profiles and then restore them to a system. More importantly, we've also seen how to work with an API that uses variable-length records as inputs to its processing. There are many such APIs, with QSRSAVO being just one example, so it's my hope that you will find this article to be of assistance in other endeavors involving system APIs.
As usual, if you have any API questions, send them to me at
LATEST COMMENTS
MC Press Online