Remove dead code
[openafs.git] / src / butc / afsxbsa.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifdef xbsa
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14 #include <afs/stds.h>
15
16 #include <roken.h>
17
18 #include <ctype.h>
19
20 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX26_ENV)
21 #include <dlfcn.h>
22 #endif
23
24 #include <afs/tcdata.h>
25 #include "butc_xbsa.h"
26 #include <afs/butx.h>
27 #include "butc_internal.h"
28
29 /* Global Definations */
30 #define APPLTYPE "afs-butc"
31 #define TDP_LIBNAME "libApiDS.a"
32 #define FIRST_HANDLE 1
33 #define NOHANDLE 0
34 #define XAPI_FSINFO "FS for XOpen API"
35 #define DIR_DELIMITER '/'
36 #define STR_DIR_DELIMITER "/"
37
38 xGlobal xopenGbl;
39 char traceStr[DSM_MAX_RC_MSG_LENGTH+1];
40 char traceStr2[(DSM_MAX_RC_MSG_LENGTH+1) - 30];
41 char ourMsg[DSM_MAX_RC_MSG_LENGTH + 1];
42 static int dsm_init = 0 ;
43
44 /* >>>  TSM function pointers. */
45 dsInt16_t (* AFSdsmBeginQuery)( dsUint32_t dsmHandle, dsmQueryType queryType, dsmQueryBuff *queryBuffer);
46 dsInt16_t (* AFSdsmGetNextQObj)( dsUint32_t dsmHandle, DataBlk *dataBlkPtr) ;
47 dsInt16_t (* AFSdsmEndQuery)( dsUint32_t dsmHandle);
48 dsInt16_t (* AFSdsmRCMsg)( dsUint32_t dsmHandle, dsInt16_t dsmRC, char *msg);
49 dsInt16_t (* AFSdsmLogEventEx)( dsUint32_t dsmHandle, dsmLogExIn_t *dsmLogExInP, dsmLogExOut_t *dsmLogExOutP);
50 dsInt16_t (* AFSdsmTrace)( dsUint32_t dsmHandle, char * string );
51 dsInt16_t (* AFSdsmTerminate)( dsUint32_t dsmHandle);
52 dsInt16_t (* AFSdsmSendData)( dsUint32_t dsmHandle, DataBlk *dataBlkPtri);
53 dsInt16_t (* AFSdsmBeginTxn)( dsUint32_t dsmHandle);
54 dsInt16_t (* AFSdsmDeleteObj)( dsUint32_t dsmHandle, dsmDelType delType, dsmDelInfo delInfo);
55 dsInt16_t (* AFSdsmEndTxn)( dsUint32_t dsmHandle, dsUint8_t vote, dsUint16_t *reason);
56 void (* AFSdsmQueryApiVersion)( dsmApiVersion *apiVersionP);
57 dsInt16_t (* AFSdsmInit)( dsUint32_t *dsmHandle, dsmApiVersion *dsmApiVersionP, char *clientNodeNameP, char *clientOwnerNameP, char *clientPasswordP, char *applicationType, char *configfile, char *options);
58 dsInt16_t (* AFSdsmQuerySessInfo)( dsUint32_t dsmHandle, ApiSessInfo *SessInfoP);
59 dsInt16_t (* AFSdsmBeginGetData)(dsUint32_t dsmHandle, dsBool_t mountWait, dsmGetType getType, dsmGetList *dsmGetObjListP);
60 dsInt16_t (* AFSdsmGetObj)( dsUint32_t dsmHandle, ObjID *objIdP, DataBlk *dataBlkPtr);
61 dsInt16_t (* AFSdsmEndGetObj)( dsUint32_t dsmHandle);
62 dsInt16_t (* AFSdsmEndGetData)( dsUint32_t dsmHandle);
63 dsInt16_t (* AFSdsmGetData)( dsUint32_t dsmHandle, DataBlk *dataBlkPtr);
64 dsInt16_t (* AFSdsmEndSendObj)( dsUint32_t dsmHandle);
65 dsInt16_t (* AFSdsmRegisterFS)( dsUint32_t dsmHandle, regFSData *regFilespaceP);
66 dsInt16_t (* AFSdsmBindMC)( dsUint32_t dsmHandle, dsmObjName *objNameP, dsmSendType sendType, mcBindKey *mcBindKeyP);
67 dsInt16_t (* AFSdsmSendObj)( dsUint32_t dsmHandle, dsmSendType sendType, void *sendBuff, dsmObjName *objNameP, ObjAttr *objAttrPtr, DataBlk *dataBlkPtr);
68 dsInt16_t (* AFSdsmChangePW)( dsUint32_t dsmHandle, char *oldPW, char *newPW);
69 /* <<< TSM function pointers. */
70
71 typedef struct s_delList {
72   struct s_delList *next;
73   ObjID           objId;
74 } delList;
75
76 static dsInt16_t buildList(
77     dsUint32_t     dsmHandle,
78     dsmObjName     *objNameP,
79     delList        **llHeadPP,
80     delList        **llTailPP)
81 {
82    dsInt16_t            rc;
83    qryArchiveData       queryBuffer;       /* for query Archive*/
84    qryRespArchiveData   qaDataArea;
85    DataBlk              qDataBlkArea;
86    char                 descrStar[] = "*";
87    delList              *lnew = NULL, *ll = NULL;
88
89    queryBuffer.stVersion = qryArchiveDataVersion ;
90    queryBuffer.objName = objNameP;
91    queryBuffer.owner = xopenGbl.dsmSessInfo.owner;
92    queryBuffer.insDateLowerBound.year = DATE_MINUS_INFINITE;
93    queryBuffer.insDateUpperBound.year = DATE_PLUS_INFINITE;
94    queryBuffer.expDateLowerBound.year = DATE_MINUS_INFINITE;
95    queryBuffer.expDateUpperBound.year = DATE_PLUS_INFINITE;
96    queryBuffer.descr = descrStar;
97
98    if ((rc=AFSdsmBeginQuery(dsmHandle, qtArchive,
99                         (void *)&queryBuffer )) != DSM_RC_OK)
100    {
101       XOPENRETURN(dsmHandle,"buildList(AFSdsmBeginQuery)",rc,__FILE__,__LINE__);
102    }
103    qaDataArea.stVersion = qryRespArchiveDataVersion;
104    qDataBlkArea.stVersion = DataBlkVersion ;
105    qDataBlkArea.bufferPtr = (char *)&qaDataArea;
106    qDataBlkArea.bufferLen = sizeof(qryRespArchiveData);
107    while ((rc = AFSdsmGetNextQObj(dsmHandle, &qDataBlkArea)) == DSM_RC_MORE_DATA)
108    {
109      if (!(lnew = (delList *)dsMalloc(sizeof(delList))))
110         XOPENRETURN(dsmHandle,"buildList(AFSdsmGetNextQObj)",
111                     DSM_RC_NO_MEMORY,__FILE__,__LINE__);
112
113      if (!*llHeadPP)
114      {
115         *llHeadPP = lnew;
116         *llTailPP = lnew;
117      }
118      else
119      {
120         ll = *llTailPP;
121         ll->next = lnew;
122         *llTailPP = lnew;
123      }
124
125      lnew->next = NULL;
126      lnew->objId.hi = qaDataArea.objId.hi;
127      lnew->objId.lo = qaDataArea.objId.lo;
128    }
129
130    if (rc != DSM_RC_FINISHED)
131    {
132       AFSdsmEndQuery(dsmHandle);
133       XOPENRETURN(dsmHandle,"buildList(AFSdsmGetNextQObj)",
134                   rc,__FILE__,__LINE__);
135    }
136
137    if ((rc = AFSdsmEndQuery(dsmHandle)) != DSM_RC_OK)
138    {
139       sprintf(traceStr2, "buildList: AFSdsmEndQuery rc = %d", rc);
140       ourTrace(dsmHandle,TrFL,traceStr2);
141    }
142    XOPENRETURN(dsmHandle,"buildList",rc,__FILE__,__LINE__);
143 }
144
145 static dsInt16_t  freeList(
146     delList    **llHeadPP,
147     delList    **llTailPP)
148 {
149
150    delList *ll;
151    ll = *llHeadPP;
152    while (ll)
153    {
154       *llHeadPP = ll->next;
155       dsFree(ll);
156       ll = *llHeadPP;
157    }
158    *llHeadPP = NULL;
159    *llTailPP = NULL;
160    XOPENRETURN(0,"freeList",0,__FILE__,__LINE__);
161 }
162
163 void ourTrace(long           BSAHandle,
164               char          *fileName,
165               int            lineNumber,
166               char          *traceStr2)
167 {
168
169    sprintf(traceStr,"%s (%d) ",fileName, lineNumber);
170
171    if (traceStr2 != NULL && *traceStr2 != '\0')
172       strcat(traceStr, traceStr2);
173
174    AFSdsmTrace(BSAHandle, traceStr);
175    return;
176 }
177
178 void ourLogEvent_Ex(dsUint32_t dsmHandle, dsmLogType type, char *message,
179                     char *appMsg, dsmLogSeverity severity)
180 {
181    dsmLogExIn_t dsmLogIn;
182    dsmLogExOut_t dsmLogOut;
183    memset(&dsmLogOut, '\0', sizeof(dsmLogExOut_t));
184
185    if (dsmHandle)
186    {
187       dsmLogIn.stVersion = dsmLogExInVersion;
188       dsmLogIn.severity = severity;
189       dsmLogIn.logType = type;
190
191       strcpy(dsmLogIn.appMsgID, appMsg);
192       dsmLogIn.message = message;
193       AFSdsmLogEventEx(dsmHandle, &dsmLogIn, &dsmLogOut);
194    }
195 }
196
197 char* ourRCMsg(dsInt16_t ourRC, char *msgPrefix)
198 {
199    char         appStr[BSA_MAX_DESC];
200    char         *chP;
201    int          bytesToCp;
202
203    memset(&ourMsg, 0x00, DSM_MAX_RC_MSG_LENGTH + 1);
204    if(msgPrefix != NULL)
205    {
206        /*================================================================
207         call stdXOpenMsgMap if return code values are within the TSM
208         return code range (96 - 104). Reformat the message prefix
209         (ANS0660 - ANS0668).
210         ================================================================*/
211         if ((ourRC >= custMin_retcode) && (ourRC <= custMax_retcode))
212         {
213              stdXOpenMsgMap(ourRC, ourMsg);
214              /* set standard XOpen message prefix range: 660 - 668 */
215              ourRC = ((ourRC * 10) / 16) + (ourRC % 16) + 600;
216              sprintf(msgPrefix, "ANSO%d", ourRC);
217         }
218         else
219           /*===============================================================
220            call dsmRCMsg if return code values other then the above ranges.
221            Get message prefix from the return message of dsmRCMsg.
222            ===============================================================*/
223         {
224               AFSdsmRCMsg(NOHANDLE, ourRC, ourMsg);
225              /*=========================================================
226               search for the first ' ' and copy from beginning of string
227               until before the two charecter before ' '.
228               e.g. To get the code value from the messages of
229                    "ANS1038S Invalid option specified", we only want to
230                    get strings "ANS1038".
231               ========================================================*/
232               chP = (char *) strchr(ourMsg, ' ');
233               bytesToCp = strlen(ourMsg) - (strlen(chP) + 1);
234               strncpy(appStr, ourMsg, bytesToCp);
235               sprintf(msgPrefix, "%s", appStr);
236         }
237    }
238 return ourMsg;
239 } /* ourRCMsg() */
240
241 void StrUpper(char *s)
242 {
243    while (*s != '\0')
244    {
245       if (isalpha((int)(unsigned char)*s))
246          *s = (char)toupper((int)(unsigned char)*s);
247       s++;
248
249    } /* end while */
250 } /* StrUpper() */
251
252 BSA_Int16 xparsePath(
253     long           BSAHandle,
254     char          *pathname,
255     char          *hl,
256     char          *ll)
257 {
258    /*=== code taken from dscparse.c ParseDestOperand function ===*/
259
260    dsInt16_t    opLen;
261    dsInt16_t    x;
262
263    *hl = *ll = '\0';
264
265    strcpy(hl, pathname);   /* use hl as working area */
266    if ((opLen = strlen(hl)) > 0)
267    {
268       /*=== Find the ll portion of the name ===*/
269       #ifdef MBCS
270       {
271          char *llstart;
272          llstart = strrchr(hl,DIR_DELIMITER);
273          if (llstart == NULL)
274             x = 0;
275          else
276             x = llstart - hl;
277       }
278       #else
279          for (x = opLen-1; x>0 && hl[x]!=DIR_DELIMITER; x--);
280       #endif
281
282          /*=== If there is no leading delimiter then add one ===*/
283          if (hl[x] != DIR_DELIMITER)
284             strcpy(ll, STR_DIR_DELIMITER);
285
286          strncat(ll, hl+x, opLen-x);
287
288          hl[x] = '\0';          /* Shorten hl by length of ll */
289    }
290    return 0;
291 }
292
293 BSA_Int16 xlateRC(
294     long           BSAHandle,
295     BSA_Int16      dsmRC,
296     BSA_Int16      *bsaRCP)
297
298 {
299    switch (dsmRC)
300    {
301       case DSM_RC_OK:
302          *bsaRCP=BSA_RC_OK;                              break;
303
304       case DSM_RC_ABORT_ACTIVE_NOT_FOUND:
305          *bsaRCP=BSA_RC_ABORT_ACTIVE_NOT_FOUND;          break;
306
307       case DSM_RC_ABORT_SYSTEM_ERROR:
308          *bsaRCP=BSA_RC_ABORT_SYSTEM_ERROR;              break;
309
310       case DSM_RC_ABORT_BAD_VERIFIER:
311       case DSM_RC_AUTH_FAILURE:
312       case DSM_RC_REJECT_ID_UNKNOWN:
313       case DSM_RC_REJECT_DUPLICATE_ID:
314          *bsaRCP=BSA_RC_AUTHENTICATION_FAILURE;          break;
315
316       case DSM_RC_BAD_CALL_SEQUENCE:
317          *bsaRCP=BSA_RC_BAD_CALL_SEQUENCE;               break;
318
319       case DSM_RC_INVALID_DS_HANDLE:
320          *bsaRCP=BSA_RC_BAD_HANDLE;                      break;
321
322       case DSM_RC_BUFF_TOO_SMALL:
323          *bsaRCP=BSA_RC_BUFFER_TOO_SMALL;                break;
324
325       case DSM_RC_DESC_TOOLONG:
326          *bsaRCP=BSA_RC_DESC_TOO_LONG;                   break;
327
328       case DSM_RC_FILESPACE_TOOLONG:
329          *bsaRCP=BSA_RC_OBJECTSPACE_TOO_LONG;            break;
330
331       /* some other condition here ? */
332       case DSM_RC_PASSWD_TOOLONG:
333          *bsaRCP=BSA_RC_INVALID_TOKEN;                   break;
334
335       case DSM_RC_INVALID_VOTE:
336          *bsaRCP=BSA_RC_INVALID_VOTE;                    break;
337
338       case DSM_RC_INVALID_OPT:
339          *bsaRCP=BSA_RC_INVALID_KEYWORD;                 break;
340
341       /*  ? what conditions cause this - object already exists ?
342       case DSM_RC_?:
343          *bsaRCP=BSA_RC_MATCH_EXISTS;                    break;
344       */
345
346       case DSM_RC_MORE_DATA:
347          *bsaRCP=BSA_RC_MORE_DATA;                       break;
348
349       /* not supported - for QueryAccessRule
350       case :
351          *bsaRCP=BSA_RC_MORE_RULES;                      break;
352       */
353
354       case DSM_RC_NEWPW_REQD:
355          *bsaRCP=BSA_RC_NEWTOKEN_REQD;                   break;
356
357       case DSM_RC_ABORT_NO_MATCH:
358       case DSM_RC_FILE_SPACE_NOT_FOUND:
359       /*=== rc for query ===*/
360          *bsaRCP=BSA_RC_NO_MATCH;                        break;
361       /*=== if this return code comes on Delete, use OBJECT_NOT_FOUND
362             - see xopndel.c ===*/
363
364       case DSM_RC_FINISHED:
365          *bsaRCP=BSA_RC_NO_MORE_DATA;                    break;
366
367       case DSM_RC_ABORT_NO_LOG_SPACE:
368       case DSM_RC_ABORT_NO_DB_SPACE:
369       case DSM_RC_ABORT_NO_MEMORY:
370       case DSM_RC_ABORT_NO_REPOSIT_SPACE:
371       case DSM_RC_REJECT_NO_RESOURCES:
372       case DSM_RC_REJECT_NO_MEMORY:
373       case DSM_RC_REJECT_NO_DB_SPACE:
374       case DSM_RC_REJECT_NO_LOG_SPACE:
375          *bsaRCP=BSA_RC_NO_RESOURCES;                    break;
376
377       case DSM_RC_NULL_DATABLKPTR:
378          *bsaRCP=BSA_RC_NULL_DATABLKPTR;                 break;
379
380       case DSM_RC_NULL_OBJNAME:
381          *bsaRCP=BSA_RC_NULL_OBJNAME;                    break;
382
383       case DSM_RC_NULL_BUFPTR:
384          *bsaRCP=BSA_RC_NULL_POINTER;                    break;
385
386       /* not supported - for DeleteAccessRule
387       case :
388          *bsaRCP=BSA_RC_NULL_RULEID;                     break;
389       */
390
391       case DSM_RC_HL_TOOLONG:
392       case DSM_RC_LL_TOOLONG:
393          *bsaRCP=BSA_RC_OBJECT_NAME_TOO_LONG;            break;
394
395       /* not supported - for DeletePolicyDomain
396       case :
397          *bsaRCP=BSA_RC_OBJECT_NOT_EMPTY;                break;
398       */
399
400       /* same as NO_MATCH for DeleteObject
401       case :
402          *bsaRCP=BSA_RC_OBJECT_NOT_FOUND;                break;
403       */
404
405       case DSM_RC_OBJINFO_TOOLONG:
406          *bsaRCP=BSA_RC_OBJINFO_TOO_LONG;                break;
407
408       /* same as BSA_RC_OBJECT_NAME_TOO_LONG
409       case :
410          *bsaRCP=BSA_RC_OBJNAME_TOO_LONG;                break;
411       */
412
413       /* not supported - for CreatePolicySet, etc
414       case :
415          *bsaRCP=BSA_RC_OPERATION_NOT_AUTHORIZED;        break;
416       */
417
418       case DSM_RC_OLDPW_REQD:
419          *bsaRCP=BSA_RC_OLDTOKEN_REQD;                   break;
420
421       case DSM_RC_REJECT_VERIFIER_EXPIRED:
422          *bsaRCP=BSA_RC_TOKEN_EXPIRED;                   break;
423
424       case DSM_RC_WILL_ABORT:
425       case DSM_RC_CHECK_REASON_CODE:
426          *bsaRCP=BSA_RC_TXN_ABORTED;                     break;
427
428       case DSM_RC_UNMATCHED_QUOTE:
429          *bsaRCP=BSA_RC_UNMATCHED_QUOTE;                 break;
430
431       /* not supported - for DeleteUser
432       case :
433          *bsaRCP=BSA_RC_USER_OWNS_OBJECT;                break;
434       */
435
436       default:
437       {
438          /*=========================================================
439           No suitable match, so print message in log, if we still
440           have a handle.  AFSdsmInit calls ApiCleanUp in certain error
441           situations.  We lose the handle in those cases so check
442           for it.
443           =========================================================*/
444
445          char         rcMsg[DSM_MAX_RC_MSG_LENGTH] ;
446          char         errPrefix[DSM_MAX_RC_MSG_LENGTH + 1];
447          char         ourMessage[DSM_MAX_RC_MSG_LENGTH + 1];
448          dsUint32_t   dsmHandle;
449
450          memset(errPrefix,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
451          memset(ourMessage,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
452
453          if (BSAHandle)
454          {
455             dsmHandle = BSAHandle;
456             AFSdsmRCMsg(dsmHandle,dsmRC,rcMsg) ;
457             strcat(rcMsg, "\n");
458             sprintf(traceStr2,
459                    "xlateRC - %s", rcMsg);
460             ourTrace(BSAHandle,TrFL, traceStr2);
461             strcpy(ourMessage, ourRCMsg(dsmRC, errPrefix));
462             ourLogEvent_Ex(BSAHandle, logLocal, ourMessage, errPrefix, logSevError);
463          }
464
465          *bsaRCP = ADSM_RC_ERROR;
466       }
467    }
468
469    /*=== trace only if we have a valid handle ===*/
470    if (dsmRC && BSAHandle)
471    {
472       sprintf(traceStr2, "xlateRC: TSM rc >%d< , BSA rc >%d<", dsmRC, *bsaRCP);
473       ourTrace(BSAHandle, TrFL, traceStr2);
474    }
475    return DSM_RC_OK ;
476 }
477
478 void stdXOpenMsgMap(dsInt16_t  rc,  char *msg)
479 {
480    switch(rc)
481    {
482       case ADSM_RC_ERROR:
483            strcpy(msg, "TSM rc error, see ADSM error log.");
484       case ADSM_RC_INVALID_NODE:
485            strcpy(msg, "BSAObjectOwner doesn't match value session Init.");
486            break;
487
488       case ADSM_RC_INVALID_COPYTYPE:
489            strcpy(msg, "Invalid copy type.");
490            break;
491
492       case ADSM_RC_INVALID_OBJTYPE:
493            strcpy(msg, "Invalid object type.");
494            break;
495
496       case ADSM_RC_INVALID_STATUS:
497            strcpy(msg, "Invalid Object Status.");
498            break;
499
500       case ADSM_RC_INVALID_ST_VER:
501            strcpy(msg, "Invalid object descriptor structure version.");
502            break;
503
504       case ADSM_RC_OWNER_TOO_LONG:
505            strcpy(msg, "Object owner name too long.");
506            break;
507
508       case ADSM_RC_PSWD_TOO_LONG:
509            strcpy(msg, "Client password too long.");
510            break;
511
512      case ADSM_RC_PSWD_GEN:
513           strcpy(msg, "Password input required .");
514           break;
515
516      default:
517            strcpy(msg, "No message available");
518            break;
519    }
520 }
521
522 BSA_Int16 fillArchiveResp(
523 long                BSAHandle,
524 ObjectDescriptor   *BSAobjDescP,
525 qryRespArchiveData *respArchiveP
526 )
527 {
528    XAPIObjInfo   *xapiObjInfoP;
529
530    strcpy(BSAobjDescP->Owner.appObjectOwner, respArchiveP->owner);
531    strcpy(BSAobjDescP->objName.objectSpaceName, respArchiveP->objName.fs);
532
533    /*=== concatenate hl and ll for pathName ===*/
534    strcpy(BSAobjDescP->objName.pathName, respArchiveP->objName.hl);
535    strcat(BSAobjDescP->objName.pathName, respArchiveP->objName.ll);
536
537    BSAobjDescP->createTime.tm_year = respArchiveP->insDate.year-1900;
538    BSAobjDescP->createTime.tm_mon  = respArchiveP->insDate.month-1;
539    BSAobjDescP->createTime.tm_mday = respArchiveP->insDate.day  ;
540    BSAobjDescP->createTime.tm_hour = respArchiveP->insDate.hour ;
541    BSAobjDescP->createTime.tm_min  = respArchiveP->insDate.minute ;
542    BSAobjDescP->createTime.tm_sec  = respArchiveP->insDate.second ;
543
544    BSAobjDescP->copyId.left  = respArchiveP->objId.hi;
545    BSAobjDescP->copyId.right = respArchiveP->objId.lo;
546
547    BSAobjDescP->restoreOrder.left  = respArchiveP->restoreOrderExt.lo_hi;
548    BSAobjDescP->restoreOrder.right = respArchiveP->restoreOrderExt.lo_lo;
549
550    strcpy(BSAobjDescP->lGName, respArchiveP->mcName) ;
551
552    xapiObjInfoP = (XAPIObjInfo *)&(respArchiveP->objInfo);
553    BSAobjDescP->size.left = xapiObjInfoP->size.left;
554    BSAobjDescP->size.right= xapiObjInfoP->size.right;
555    strcpy(BSAobjDescP->resourceType, xapiObjInfoP->resourceType);
556    strcpy(BSAobjDescP->desc, xapiObjInfoP->partDesc);
557    strcpy(BSAobjDescP->objectInfo, xapiObjInfoP->partObjInfo);
558
559    if (respArchiveP->objName.objType == DSM_OBJ_DIRECTORY)
560       BSAobjDescP->objectType = BSAObjectType_DIRECTORY;
561    else
562       BSAobjDescP->objectType = BSAObjectType_FILE;
563       /*=== future - type DATABASE ?? ===*/
564
565       BSAobjDescP->status = BSAObjectStatus_ACTIVE;
566       XOPENRETURN(BSAHandle, "fillArchiveResp",
567                   BSA_RC_SUCCESS,__FILE__,__LINE__);
568 }
569
570 BSA_Int16 fillBackupResp(
571 long               BSAHandle,
572 ObjectDescriptor  *BSAobjDescP,
573 qryRespBackupData *respBackupP
574 )
575 {
576    XAPIObjInfo   *xapiObjInfoP;
577
578    strcpy(BSAobjDescP->Owner.appObjectOwner, respBackupP->owner);
579    strcpy(BSAobjDescP->objName.objectSpaceName, respBackupP->objName.fs);
580
581    /*=== concatenate hl and ll for pathName ===*/
582    strcpy(BSAobjDescP->objName.pathName, respBackupP->objName.hl);
583    strcat(BSAobjDescP->objName.pathName, respBackupP->objName.ll);
584
585    BSAobjDescP->createTime.tm_year = respBackupP->insDate.year-1900;
586    BSAobjDescP->createTime.tm_mon  = respBackupP->insDate.month-1;
587    BSAobjDescP->createTime.tm_mday = respBackupP->insDate.day  ;
588    BSAobjDescP->createTime.tm_hour = respBackupP->insDate.hour ;
589    BSAobjDescP->createTime.tm_min  = respBackupP->insDate.minute ;
590    BSAobjDescP->createTime.tm_sec  = respBackupP->insDate.second ;
591
592    BSAobjDescP->copyId.left  = respBackupP->objId.hi;
593    BSAobjDescP->copyId.right = respBackupP->objId.lo;
594
595    BSAobjDescP->restoreOrder.left  = respBackupP->restoreOrderExt.lo_hi;
596    BSAobjDescP->restoreOrder.right = respBackupP->restoreOrderExt.lo_lo;
597
598    strcpy(BSAobjDescP->lGName, respBackupP->mcName) ;
599
600    xapiObjInfoP = (XAPIObjInfo *)&(respBackupP->objInfo);
601    BSAobjDescP->size.left = xapiObjInfoP->size.left;
602    BSAobjDescP->size.right= xapiObjInfoP->size.right;
603    strcpy(BSAobjDescP->resourceType, xapiObjInfoP->resourceType);
604    strcpy(BSAobjDescP->desc, xapiObjInfoP->partDesc);
605    strcpy(BSAobjDescP->objectInfo, xapiObjInfoP->partObjInfo);
606
607    if (respBackupP->objName.objType == DSM_OBJ_DIRECTORY)
608       BSAobjDescP->objectType = BSAObjectType_DIRECTORY;
609    else
610       BSAobjDescP->objectType = BSAObjectType_FILE;
611       /*=== future - type DATABASE ?? ===*/
612
613    if (respBackupP->objState == DSM_ACTIVE)
614       BSAobjDescP->status = BSAObjectStatus_ACTIVE;
615    else
616       BSAobjDescP->status = BSAObjectStatus_INACTIVE;
617       /*=== ?? check for any other ===*/
618
619    XOPENRETURN(BSAHandle, "fillRBackupResp",
620                BSA_RC_SUCCESS,__FILE__,__LINE__);
621 }
622
623
624 afs_int32 dsm_MountLibrary(void)
625 {
626 void * dynlib = NULL ;
627
628 #ifdef DEBUG_BUTC
629     printf("dsm_MountLibrary : inside function.  \n");
630 #endif
631 #if defined(AFS_AIX_ENV)
632         dynlib = dlopen("/usr/lib/libApiDS.a(dsmapish.o)", RTLD_NOW | RTLD_LOCAL | RTLD_MEMBER);
633 #elif defined (AFS_AMD64_LINUX26_ENV)
634         dynlib = dlopen("/usr/lib64/libApiTSM64.so", RTLD_NOW | RTLD_LOCAL);
635 #elif defined(AFS_SUN5_ENV) || defined(AFS_LINUX26_ENV)
636         dynlib = dlopen("/usr/lib/libApiDS.so", RTLD_NOW | RTLD_LOCAL);
637 #else
638         dynlib = NULL;
639 #endif
640
641         if (dynlib == NULL) {
642                 ELog(0,"dsm_MountLibrary: The dlopen call to load the libApiDS shared library failed\n");
643                 return(BUTX_NOLIBRARY);
644         }
645
646 #ifdef DEBUG_BUTC
647         printf("dsm_MountLibrary : SUCCESS to Open the libApiDS shared library. \n");
648 #endif
649 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX26_ENV)
650         AFSdsmBeginQuery = (dsInt16_t (*)( dsUint32_t dsmHandle, dsmQueryType queryType, dsmQueryBuff *queryBuffer)) dlsym((void *)dynlib, "dsmBeginQuery");
651         AFSdsmGetNextQObj = (dsInt16_t (*)( dsUint32_t dsmHandle, DataBlk *dataBlkPtr))dlsym((void *)dynlib, "dsmGetNextQObj") ;
652         AFSdsmEndQuery = (dsInt16_t (*)( dsUint32_t dsmHandle))dlsym((void *)dynlib, "dsmEndQuery");
653         AFSdsmRCMsg = (dsInt16_t (*)( dsUint32_t dsmHandle, dsInt16_t dsmRC, char *msg))dlsym((void *)dynlib, "dsmRCMsg");
654         AFSdsmLogEventEx = (dsInt16_t (*)( dsUint32_t dsmHandle, dsmLogExIn_t *dsmLogExInP, dsmLogExOut_t *dsmLogExOutP))dlsym((void *)dynlib, "dsmLogEventEx");
655         AFSdsmTrace = (dsInt16_t (*)( dsUint32_t dsmHandle, char * string ))dlsym((void *)dynlib, "dsmTrace");
656         AFSdsmTerminate = (dsInt16_t (*)( dsUint32_t dsmHandle))dlsym((void *)dynlib, "dsmTerminate");
657         AFSdsmSendData = (dsInt16_t (*)( dsUint32_t dsmHandle, DataBlk *dataBlkPtri))dlsym((void *)dynlib, "dsmSendData");
658         AFSdsmBeginTxn = (dsInt16_t (*)( dsUint32_t dsmHandle))dlsym((void *)dynlib, "dsmBeginTxn");
659         AFSdsmDeleteObj = (dsInt16_t (*)( dsUint32_t dsmHandle, dsmDelType delType, dsmDelInfo delInfo))dlsym((void *)dynlib, "dsmDeleteObj");
660         AFSdsmEndTxn = (dsInt16_t (*)( dsUint32_t dsmHandle, dsUint8_t vote, dsUint16_t *reason))dlsym((void *)dynlib, "dsmEndTxn");
661         AFSdsmQueryApiVersion = (void (*)( dsmApiVersion *apiVersionP))dlsym((void *)dynlib, "dsmQueryApiVersion");
662         AFSdsmInit = (dsInt16_t (*)( dsUint32_t *dsmHandle, dsmApiVersion *dsmApiVersionP, char *clientNodeNameP, char *clientOwnerNameP, char *clientPasswordP, char *applicationType, char *configfile, char *options))dlsym((void *)dynlib, "dsmInit");
663         AFSdsmQuerySessInfo = (dsInt16_t (*)( dsUint32_t dsmHandle, ApiSessInfo *SessInfoP))dlsym((void *)dynlib, "dsmQuerySessInfo");
664         AFSdsmBeginGetData = (dsInt16_t (*)(dsUint32_t dsmHandle, dsBool_t mountWait, dsmGetType getType, dsmGetList *dsmGetObjListP))dlsym((void *)dynlib, "dsmBeginGetData");
665         AFSdsmGetObj = (dsInt16_t (*)( dsUint32_t dsmHandle, ObjID *objIdP, DataBlk *dataBlkPtr))dlsym((void *)dynlib, "dsmGetObj");
666         AFSdsmEndGetObj = (dsInt16_t (*)( dsUint32_t dsmHandle))dlsym((void *)dynlib, "dsmEndGetObj");
667         AFSdsmEndGetData = (dsInt16_t (*)( dsUint32_t dsmHandle))dlsym((void *)dynlib, "dsmEndGetData");
668         AFSdsmGetData = (dsInt16_t (*)( dsUint32_t dsmHandle, DataBlk *dataBlkPtr))dlsym((void *)dynlib, "dsmGetData");
669         AFSdsmEndSendObj = (dsInt16_t (*)( dsUint32_t dsmHandle))dlsym((void *)dynlib, "dsmEndSendObj");
670         AFSdsmRegisterFS = (dsInt16_t (*)( dsUint32_t dsmHandle, regFSData *regFilespaceP))dlsym((void *)dynlib, "dsmRegisterFS");
671         AFSdsmBindMC = (dsInt16_t (*)( dsUint32_t dsmHandle, dsmObjName *objNameP, dsmSendType sendType, mcBindKey *mcBindKeyP))dlsym((void *)dynlib, "dsmBindMC");
672         AFSdsmSendObj = (dsInt16_t (*)( dsUint32_t dsmHandle, dsmSendType sendType, void *sendBuff, dsmObjName *objNameP, ObjAttr *objAttrPtr, DataBlk *dataBlkPtr))dlsym((void *)dynlib, "dsmSendObj");
673         AFSdsmChangePW = (dsInt16_t (*)( dsUint32_t dsmHandle, char *oldPW, char *newPW))dlsym((void *)dynlib, "dsmChangePW");
674
675
676         if (    !AFSdsmBeginQuery || !AFSdsmGetNextQObj || !AFSdsmEndQuery ||
677                 !AFSdsmRCMsg || !AFSdsmLogEventEx || !AFSdsmTrace ||
678                 !AFSdsmTerminate || !AFSdsmEndGetObj || !AFSdsmSendData ||
679                 !AFSdsmBeginTxn || !AFSdsmDeleteObj || !AFSdsmEndGetData ||
680                 !AFSdsmEndTxn || !AFSdsmQueryApiVersion || !AFSdsmInit ||
681                 !AFSdsmGetData || !AFSdsmQuerySessInfo || !AFSdsmBeginGetData ||
682                 !AFSdsmGetObj || !AFSdsmEndSendObj || !AFSdsmRegisterFS ||
683                 !AFSdsmBindMC || !AFSdsmSendObj || !AFSdsmChangePW )
684         {
685                 ELog(0,"dsm_MountLibrary: The dlopen call to load the TSM shared library failed\n");
686                 return(BUTX_NOLIBRARY);
687         }
688 #ifdef DEBUG_BUTC
689                 printf("dsm_MountLibrary : All TSM function pointers initialized. \n");
690 #endif
691
692 #endif
693         dsm_init = 1 ;
694 #ifdef DEBUG_BUTC
695     printf("dsm_MountLibrary : leaving function. \n");
696 #endif
697         return 0 ;
698
699 }
700
701 BSA_Int16 BSAInit( long     *BSAHandleP,
702                    SecurityToken  *clientPasswordP,
703                    ObjectOwner    *objectOwnerP,
704                    char          **envP )
705 {
706    dsInt16_t        rc = 0;
707    dsInt16_t        saverc = 0;
708    BSA_Int16        bsaRC = 0;
709    dsUint32_t       dsmHandle;   /* use diff field when talking to base
710                                    code in case either format changes */
711    dsmApiVersion    xapiHdrVer;  /* Xopen API build version           */
712    dsmApiVersion    apiLibVer;  /* Xopen API Libary version          */
713
714    dsUint32_t       xapiVersion, apiVersion;
715    envSetUp         dsmEnvSetUp;
716
717    /*==================================================================
718      Today the BSA constants have the same values as the DSM constants.
719      If this changes, use strncpy.
720      =================================================================*/
721
722    char            rcmsg[DSM_MAX_RC_MSG_LENGTH + 1];
723    char            dsmNode[DSM_MAX_NODE_LENGTH];
724    char            dsmOwner[DSM_MAX_OWNER_LENGTH];
725    char            dsmPswd[DSM_MAX_VERIFIER_LENGTH];
726    char           *dsmNodeP;
727    char           *dsmOwnerP;
728    char           *dsmPswdP;
729    char            options[DSM_MAX_RC_MSG_LENGTH];
730    char           *optionP = NULL;
731    char            TDPLibName[BSA_MAX_DESC];
732    char            TDPILibName[BSA_MAX_DESC];
733    int             i=0;
734    char            errPrefix[DSM_MAX_RC_MSG_LENGTH + 1];
735    char            ourMessage[DSM_MAX_RC_MSG_LENGTH + 1];
736    char            rcMsg[DSM_MAX_RC_MSG_LENGTH + 1];
737
738    dsmHandle = 0;
739   *BSAHandleP = 0;
740
741 #ifdef DEBUG_BUTC
742    printf("BSAInit : inside function. \n");
743 #endif
744    memset(errPrefix,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
745    memset(ourMessage,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
746    memset(rcMsg,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
747
748    if(!dsm_init)
749    {
750 #ifdef DEBUG_BUTC
751         printf("TSM library not mounted. \n");
752 #endif
753         if (dsm_MountLibrary())
754         {
755                 printf("TSM Library initialisation failed. \n");
756                 return 1 ;
757         }
758 #ifdef DEBUG_BUTC
759         printf("TSM Library initialisation SUCCESS. \n");
760 #endif
761    }
762
763    /*==================================================================
764     Trace won't work before AFSdsmInit, moved to after call to AFSdsmInit
765     The XOpen library is built by statically linking with the API and
766     client common modules.  So, the version checking is unnecessary.
767     We'll fill in the value anyway for consistency.
768     ==================================================================*/
769    memset(&xapiHdrVer,0x00,sizeof(dsmApiVersion));
770    memset(&apiLibVer,0x00,sizeof(dsmApiVersion));
771    memset(&dsmEnvSetUp,0x00,sizeof(envSetUp));
772
773    AFSdsmQueryApiVersion(&apiLibVer);
774
775    xapiHdrVer.version = DSM_API_VERSION;  /* Set the applications    */
776    xapiHdrVer.release = DSM_API_RELEASE;  /* compile time version.   */
777    xapiHdrVer.level   = DSM_API_LEVEL;    /* version                 */
778
779    xapiVersion = (1000 * DSM_API_VERSION) + (100 * DSM_API_RELEASE)
780                   + DSM_API_LEVEL;
781
782    apiVersion  = (1000 * apiLibVer.version) + (100 * apiLibVer.release)
783                   + apiLibVer.level;
784
785    /* check for compatibility problems */
786    if (apiVersion < xapiVersion)
787         return ADSM_RC_ERROR;
788
789    /*==== Init global area ===*/
790
791    memset(&xopenGbl,0x00, sizeof(xGlobal));  /* Zero out block.        */
792
793    memset(dsmNode,0x00,DSM_MAX_NODE_LENGTH);
794    memset(dsmOwner,0x00,DSM_MAX_OWNER_LENGTH);
795    memset(dsmPswd,0x00,DSM_MAX_VERIFIER_LENGTH);
796    memset(options,0x00,DSM_MAX_RC_MSG_LENGTH);
797    memset(TDPLibName,0x00,BSA_MAX_DESC);
798    memset(TDPILibName,0x00,BSA_MAX_DESC);
799
800    memset(rcmsg,         '\0', DSM_MAX_RC_MSG_LENGTH + 1);
801
802
803    /*=== Validate the inputs ===*/
804
805    if (objectOwnerP)
806    {
807       if (strlen(objectOwnerP->bsaObjectOwner) > BSA_MAX_BSAOBJECT_OWNER)
808       {
809          bsaRC = ADSM_RC_OWNER_TOO_LONG;
810          return(bsaRC);
811       }
812
813       if (strlen(objectOwnerP->appObjectOwner) > BSA_MAX_APPOBJECT_OWNER)
814       {
815          bsaRC = ADSM_RC_OWNER_TOO_LONG;
816          return(bsaRC);
817       }
818
819       if (objectOwnerP->bsaObjectOwner[0] == '\0') /* empty string */
820          dsmNodeP = NULL;
821       else
822       {
823          strcpy(dsmNode, objectOwnerP->bsaObjectOwner);
824          dsmNodeP = dsmNode;
825       }
826
827       if (objectOwnerP->appObjectOwner[0] == '\0') /* empty string  */
828          dsmOwnerP = NULL;
829       else
830       {
831          strcpy(dsmOwner, objectOwnerP->appObjectOwner);
832          dsmOwnerP = dsmOwner;
833       }
834    }
835    else
836    {
837       dsmNodeP = NULL;
838       dsmOwnerP = NULL;
839    }
840
841    if (clientPasswordP != NULL)
842    {
843       if (strlen((const char *)clientPasswordP) > BSA_MAX_TOKEN_SIZE)
844       {
845          bsaRC = ADSM_RC_PSWD_TOO_LONG;
846          return(bsaRC);
847       }
848
849       strcpy(dsmPswd, (const char *)clientPasswordP);
850       dsmPswdP = dsmPswd;
851    }
852    else
853       dsmPswdP = NULL;
854       {
855           while ((envP[i] != NULL) && (envP[i] != '\0'))
856           {
857              strcat(options, "-");
858              strcat(options, envP[i]);
859              if ((envP[i+1] != NULL) && (envP[i+1] != '\0'))
860                 strcat(options, " ");
861                 i++;
862           }
863
864           if (options[0] == '\0')   /* empty string */
865              optionP = NULL;
866           else
867              optionP = options;
868
869           rc = AFSdsmInit(&dsmHandle,   /* On return contains session handle.   */
870                        &xapiHdrVer,   /* Version of the API we are using.     */
871                        dsmNodeP,      /* node name                            */
872                        dsmOwnerP,     /* owner name                           */
873                        dsmPswdP,      /* password                             */
874                        APPLTYPE,      /* Name of our node type.               */
875                        NULL,          /* no API config file name              */
876                        optionP);      /* option string                        */
877      }
878
879      if (rc == DSM_RC_OK)
880      {
881         /*=== now that AFSdsmInit issued, we can trace ===*/
882         sprintf(traceStr2,
883                 "BSAInit: original bsaOwner=%s, appOwner=%s, token=%s, Appl Type=%s, options=%s.",
884                  dsmNode, dsmOwner, dsmPswd, APPLTYPE, options);
885         ourTrace(dsmHandle, TrFL, traceStr2);
886         {
887               strcpy(TDPLibName, TDP_LIBNAME);
888               sprintf(traceStr2,
889                      "BSAInit: Using TSM Native API library : %s", TDPLibName);
890               ourTrace(dsmHandle, TrFL, traceStr2);
891         }
892      }
893
894
895      /*=================================================================
896      If the password expired, continue initialization so the session
897      can continue.
898
899      Save the 'password expired' rc away, so we can return it to the
900      caller.
901
902      The application will be responsible for issuing a BSAChangeToken()
903      to update the password.
904      =================================================================*/
905      if (rc == DSM_RC_REJECT_VERIFIER_EXPIRED)
906      {
907         /*=== don't return yet - init global so session can continue ===*/
908
909          saverc = rc;
910          rc = DSM_RC_OK;
911      }
912      else
913         if ((rc == DSM_RC_NO_OWNER_REQD) ||
914             (rc == DSM_RC_NO_NODE_REQD))       /* pswd=generate */
915         {
916             AFSdsmTerminate(dsmHandle);
917             bsaRC = ADSM_RC_PSWD_GEN;
918             return (bsaRC);
919         }
920
921
922    /*==================================================================
923      If we ran into any problems so far, rc will be non-zero or 'true'.
924
925      This is not true for password expired, since we reset rc to zero
926      in that case.  We'll have to check for that later so we return
927      the 'password expired' indication to the caller.
928      ==================================================================*/
929
930    if (rc)
931    {
932       AFSdsmRCMsg(dsmHandle, rc, rcmsg);
933       sprintf(traceStr2, "BSAInit(AFSdsmInit): Messages - %s ", rcmsg);
934       ourTrace(dsmHandle, TrFL, traceStr2);
935       strcpy(ourMessage, ourRCMsg(rc, errPrefix));
936       ourLogEvent_Ex(dsmHandle, logLocal, ourMessage, errPrefix, logSevError);
937       xlateRC(*BSAHandleP, rc, &bsaRC); /* BSAHandle still 0, but not used */
938       AFSdsmTerminate(dsmHandle);          /* clean up memory blocks          */
939       return (bsaRC);
940    }
941
942
943    {
944       char msg[256];                    /* format string and watch for NULLs */
945
946       strcpy(msg, "BSAInit for node=");
947
948       if (objectOwnerP)
949       {
950          if (dsmNodeP)
951             strcat(msg, dsmNodeP);
952          else
953             strcat(msg, "<empty>");
954       }
955       else
956          strcat(msg, "<NULL>");
957
958       strcat(msg, ", owner=");
959
960       if (objectOwnerP)
961       {
962          if (dsmOwnerP)
963             strcat(msg, dsmOwnerP);
964          else
965             strcat(msg, "<empty>");
966       }
967       else
968          strcat(msg, "<NULL>");
969
970       strcat(msg, ".\n");
971
972       strcpy(traceStr, msg);
973    }
974
975    *BSAHandleP = (long)dsmHandle;
976
977
978    /*=================================================================
979     For password expired processing the application is responsible for
980     issuing BSAChangeToken() to update the password, when they see the
981     'expired password' return code.
982
983     The BSAChangeToken() command will issue the AFSdsmQuerySessInfo()
984     call, so don't issue it here.
985     =================================================================*/
986
987    if (!saverc)               /* don't call this if we got pswd expire */
988    {
989       /*=== now query session info to populate the global structure ===*/
990
991       xopenGbl.dsmSessInfo.stVersion = ApiSessInfoVersion;
992       rc = AFSdsmQuerySessInfo(dsmHandle,                /* Session handle */
993                             &xopenGbl.dsmSessInfo);   /* Output struct  */
994
995       if (rc)
996       {
997          sprintf(traceStr2, "BSAInit(AFSdsmQuerySessInfo): error rc = %d", rc);
998          ourTrace(dsmHandle, TrFL, traceStr2);
999          AFSdsmTerminate(dsmHandle);
1000          *BSAHandleP = 0;
1001       }
1002       else
1003       {
1004          sprintf(traceStr2, "BSAInit: Actual node=%s, actual DSM owner=%s, servername=%s.",
1005                  xopenGbl.dsmSessInfo.id, xopenGbl.dsmSessInfo.owner,
1006                  xopenGbl.dsmSessInfo.adsmServerName);
1007          ourTrace(dsmHandle, TrFL, traceStr2);
1008       }
1009    }
1010    else
1011    {
1012       /*============================================================
1013         Indicate in the session flags that we encountered a password
1014         expiration condition.  The application will know that this
1015         happened via the return code.
1016         ===========================================================*/
1017
1018       xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_PSWD_EXPIRE);
1019    }
1020
1021    /*=== Save bsaObjectOwner (node name) value passed on Init ===*/
1022
1023    if (dsmNodeP)
1024       strcpy(xopenGbl.bsaObjectOwner, dsmNodeP);
1025
1026      /*================================================================
1027       Check to see if we saved away the 'expired password' return code.
1028       if so, return it to the caller.
1029       ================================================================*/
1030
1031    if (saverc)
1032       rc = saverc;
1033
1034    xlateRC(*BSAHandleP, rc, &bsaRC);
1035 #ifdef DEBUG_BUTC
1036         printf("BSAInit : Leaving Function. \n");
1037 #endif
1038    return (bsaRC);
1039 }
1040
1041 BSA_Int16 BSATerminate(
1042     long              BSAHandle
1043 )
1044 {
1045    dsInt16_t      rc = 0;
1046    BSA_Int16      bsaRC = 0;
1047    dsUint32_t     dsmHandle;
1048
1049    if(!dsm_init)
1050    {
1051 #ifdef DEBUG_BUTC
1052         printf("TSM library not mounted. \n");
1053 #endif
1054         if (dsm_MountLibrary())
1055         {
1056                 printf("TSM Library initialisation failed. \n");
1057                 return 1 ;
1058         }
1059 #ifdef DEBUG_BUTC
1060         printf("TSM Library initialisation SUCCESS. \n");
1061 #endif
1062    }
1063
1064    dsmHandle = BSAHandle;
1065    sprintf(traceStr2, "BSATerminate ENTRY: BSAHandle is %ld.",
1066                       BSAHandle);
1067    ourTrace(dsmHandle, TrFL, traceStr2);
1068
1069    rc = AFSdsmTerminate(dsmHandle);
1070    dsmHandle = 0;
1071    xlateRC(dsmHandle, rc, &bsaRC);
1072
1073    return (bsaRC);
1074
1075 }
1076
1077 BSA_Int16 BSAChangeToken(
1078    long           BSAHandle,
1079    SecurityToken *oldTokenP,
1080    SecurityToken *newTokenP
1081 )
1082 {
1083    dsInt16_t      rc = 0;
1084    BSA_Int16      bsaRC = 0;
1085    dsUint32_t     dsmHandle;
1086
1087    if(!dsm_init)
1088    {
1089 #ifdef DEBUG_BUTC
1090         printf("TSM library not mounted. \n");
1091 #endif
1092         if (dsm_MountLibrary())
1093         {
1094                 printf("TSM Library initialisation failed. \n");
1095                 return 1 ;
1096         }
1097 #ifdef DEBUG_BUTC
1098         printf("TSM Library initialisation SUCCESS. \n");
1099 #endif
1100    }
1101
1102    dsmHandle = BSAHandle;
1103
1104    sprintf(traceStr2, "BSAChangeToken ENTRY: BSAHandle:%ld old:>%s< new:>%s<",
1105            BSAHandle,*oldTokenP,*newTokenP);
1106    ourTrace(dsmHandle, TrFL, traceStr2);
1107
1108    rc = AFSdsmChangePW(dsmHandle, (char *)oldTokenP, (char *)newTokenP);
1109
1110    if (rc)
1111    {
1112       xlateRC(BSAHandle, rc, &bsaRC);
1113       XOPENRETURN(BSAHandle,"BSAChangeToken(AFSdsmChangePW)",
1114                   bsaRC,__FILE__,__LINE__);
1115    }
1116
1117    xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_PSWD_EXPIRE); /* set off */
1118
1119     /*=== now query session info to populate the global structure ===*/
1120
1121    xopenGbl.dsmSessInfo.stVersion = ApiSessInfoVersion;
1122    rc = AFSdsmQuerySessInfo(dsmHandle,                /* Our session handle  */
1123                          &xopenGbl.dsmSessInfo);   /* Output structure.   */
1124    if (rc)
1125    {
1126       /*=== appl should call Terminate ===*/
1127       sprintf(traceStr2, "BSAChangeToken(AFSdsmQuerySessInfo): error rc = %d", rc);
1128       ourTrace(BSAHandle, TrFL, traceStr2);
1129    }
1130    xlateRC(BSAHandle, rc, &bsaRC);
1131    XOPENRETURN(BSAHandle,"BSAChangeToken",
1132                bsaRC,__FILE__,__LINE__);
1133 }
1134
1135 BSA_Int16 BSASetEnvironment(
1136     long     BSAHandle,
1137     char           **envP
1138 )
1139 {
1140    if(!dsm_init)
1141    {
1142 #ifdef DEBUG_BUTC
1143         printf("TSM library not mounted. \n");
1144 #endif
1145         if (dsm_MountLibrary())
1146         {
1147                 printf("TSM Library initialisation failed. \n");
1148                 return 1 ;
1149         }
1150 #ifdef DEBUG_BUTC
1151         printf("TSM Library initialisation SUCCESS. \n");
1152 #endif
1153    }
1154
1155    sprintf(traceStr2, "BSASetEnvironment ENTRY: BSAHandle:%ld envP:>%p< ",
1156            BSAHandle,envP);
1157    ourTrace(BSAHandle, TrFL, traceStr2);
1158    XOPENRETURN(BSAHandle,"BSASetEnvironment",
1159                BSA_RC_BAD_CALL_SEQUENCE,__FILE__,__LINE__);
1160 }
1161
1162 BSA_Int16 BSAGetEnvironment(
1163    long           BSAHandle,
1164    ObjectOwner   *objOwnerP,
1165    char         **envP
1166 )
1167 {
1168    dsInt16_t   rc = 0;
1169    BSA_Int16   bsaRC = 0;
1170    dsUint32_t  dsmHandle;
1171    char        envString[ADSM_ENV_STRS][BSA_MAX_DESC];
1172    char        maxObjStr[6];  /* conversion field. value range is 16-256 */
1173    dsUint32_t  maxObjNum;
1174    dsInt16_t   i;
1175
1176    if(!dsm_init)
1177    {
1178 #ifdef DEBUG_BUTC
1179         printf("TSM library not mounted. \n");
1180 #endif
1181         if (dsm_MountLibrary())
1182         {
1183                 printf("TSM Library initialisation failed. \n");
1184                 return 1 ;
1185         }
1186 #ifdef DEBUG_BUTC
1187         printf("TSM Library initialisation SUCCESS. \n");
1188 #endif
1189    }
1190
1191    for (i=0; i<ADSM_ENV_STRS; i++)
1192    {
1193       memset(envString[i], 0x00, BSA_MAX_DESC);
1194    }
1195    sprintf(traceStr2, "BSAGetEnvironment ENTRY: BSAHandle:%ld ObjOwner:'%s' appOwner:'%s' envP:>%p<.",
1196            BSAHandle,
1197            objOwnerP->bsaObjectOwner,
1198            objOwnerP->appObjectOwner,
1199            envP);
1200    ourTrace(BSAHandle, TrFL, traceStr2);
1201
1202    dsmHandle = BSAHandle;
1203
1204    /*===============================================================
1205     check if BSAInit has been done for now we have only one possible
1206     handle value - change in future
1207     ==============================================================*/
1208    if ((dsmHandle != FIRST_HANDLE) ||
1209       (xopenGbl.dsmSessInfo.stVersion != ApiSessInfoVersion))
1210        XOPENRETURN(BSAHandle,"BSAGetEnvironment",
1211                    BSA_RC_BAD_CALL_SEQUENCE, __FILE__,__LINE__);
1212
1213    if (objOwnerP)
1214    {
1215       strcpy(objOwnerP->bsaObjectOwner, xopenGbl.dsmSessInfo.id);
1216       strcpy(objOwnerP->appObjectOwner, xopenGbl.dsmSessInfo.owner);
1217    }
1218    else
1219       XOPENRETURN(BSAHandle,"BSAGetEnvironment",
1220                   BSA_RC_NULL_POINTER, __FILE__,__LINE__);
1221
1222    rc = AFSdsmQuerySessInfo(dsmHandle,                   /* Session Handle */
1223                          &xopenGbl.dsmSessInfo);       /* Output struct  */
1224
1225    if (rc)
1226       {
1227          sprintf(traceStr2, "BSAGetEnvironment(AFSdsmQuerySessInfo): error rc = %d", rc);
1228          ourTrace(dsmHandle, TrFL, traceStr2);
1229          AFSdsmTerminate(dsmHandle);
1230         /* *BSAHandleP = 0; */
1231       }
1232       else
1233       {
1234          sprintf(traceStr2, "BSAGetEnvironment: Actual node=%s, actual DSM owner=%s, ServerName=%s.",
1235                  xopenGbl.dsmSessInfo.id, xopenGbl.dsmSessInfo.owner, xopenGbl.dsmSessInfo.adsmServerName);
1236          ourTrace(dsmHandle, TrFL, traceStr2);
1237       }
1238
1239    strcpy(envString[0],"TSMSRVR=");
1240    strcat(envString[0],xopenGbl.dsmSessInfo.serverHost);
1241
1242    strcpy(envString[1],"TSMMAXOBJ=");
1243    maxObjNum = xopenGbl.dsmSessInfo.maxObjPerTxn;  /* convert to 32 bit */
1244    sprintf(maxObjStr,"%u", maxObjNum );
1245    strcat(envString[1], maxObjStr);
1246
1247    strcpy(envString[2], "TSMSRVRSTANZA=");
1248    strcat(envString[2], xopenGbl.dsmSessInfo.adsmServerName);
1249
1250    sprintf(traceStr2, "BSAGetEnvironment Value : TSM Server '%s', TSM Max Object '%s',TSM Server STANZA '%s'",
1251          envString[0], envString[1], envString[2]);
1252    ourTrace(BSAHandle, TrFL, traceStr2);
1253
1254    for (i=0; i< ADSM_ENV_STRS; i++) {
1255      if ( *envP == NULL ) {  /* watch for NULL pointers */
1256        /* Allocating memory for *envP.*/
1257        *envP = malloc(sizeof(char) * BSA_MAX_DESC +1);
1258
1259        /* copy the content of envString[i] to *envP. */
1260         strcpy(*envP,envString[i]);
1261         envP++;
1262      }
1263    }
1264
1265    xlateRC(BSAHandle, rc, &bsaRC);
1266    XOPENRETURN(BSAHandle,"BSAGetEnvironment",
1267                bsaRC,__FILE__,__LINE__)
1268 }
1269
1270 BSA_Int16 BSABeginTxn(
1271     long             BSAHandle
1272 )
1273 {
1274    if(!dsm_init)
1275    {
1276 #ifdef DEBUG_BUTC
1277         printf("TSM library not mounted. \n");
1278 #endif
1279         if (dsm_MountLibrary())
1280         {
1281                 printf("TSM Library initialisation failed. \n");
1282                 return 1 ;
1283         }
1284 #ifdef DEBUG_BUTC
1285         printf("TSM Library initialisation SUCCESS. \n");
1286 #endif
1287    }
1288
1289    sprintf(traceStr2, "BSABeginTxn ENTRY: BSAHandle:%ld", BSAHandle);
1290    ourTrace(BSAHandle, TrFL, traceStr2);
1291   /*========================================================
1292    don't actually issue BeginTxn yet, because we will do our
1293    own for Get and Query
1294    ========================================================*/
1295
1296    xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_IN_BSA_TXN); /* set on */
1297    XOPENRETURN(BSAHandle, "BSABeginTxn",
1298                BSA_RC_SUCCESS,__FILE__,__LINE__);
1299 }
1300
1301 BSA_Int16 BSAEndTxn(
1302     long           BSAHandle,
1303     Vote           vote
1304 )
1305 {
1306    dsInt16_t      rc = 0;
1307    BSA_Int16      bsaRC = 0;
1308    dsUint32_t     dsmHandle;
1309    dsUint8_t      dsmVote;
1310    dsUint16_t     reason ;
1311    char           rsMsg[DSM_MAX_RC_MSG_LENGTH + 1];
1312    char           errPrefix[DSM_MAX_RC_MSG_LENGTH + 1];
1313    char           ourMessage[DSM_MAX_RC_MSG_LENGTH + 1];
1314
1315    if(!dsm_init)
1316    {
1317 #ifdef DEBUG_BUTC
1318         printf("TSM library not mounted. \n");
1319 #endif
1320         if (dsm_MountLibrary())
1321         {
1322                 printf("TSM Library initialisation failed. \n");
1323                 return 1 ;
1324         }
1325 #ifdef DEBUG_BUTC
1326         printf("TSM Library initialisation SUCCESS. \n");
1327 #endif
1328    }
1329
1330    memset(errPrefix,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1331    memset(rsMsg,        '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1332    memset(ourMessage,    '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1333
1334    sprintf(traceStr2, "BSAEndTxn ENTRY: BSAHandle:%ld Vote:>%d<", BSAHandle, vote);
1335    ourTrace(BSAHandle, TrFL, traceStr2);
1336
1337    dsmHandle = BSAHandle;
1338
1339    if (xopenGbl.sessFlags & FL_IN_DSM_TXN)
1340    {
1341       if (vote == BSAVote_COMMIT)
1342          dsmVote = DSM_VOTE_COMMIT;
1343       else
1344          if (vote == BSAVote_ABORT)
1345             dsmVote = DSM_VOTE_ABORT;
1346          else
1347          {
1348             sprintf(traceStr2, "BSAEndTxn: invalid vote (%d)", vote);
1349             ourTrace(BSAHandle,TrFL, traceStr2);
1350             bsaRC = BSA_RC_INVALID_VOTE;
1351             XOPENRETURN(BSAHandle, "BSAEndTxn",bsaRC,__FILE__,__LINE__);
1352          }
1353
1354
1355       sprintf(traceStr2, "BSAEndTxn: issue AFSdsmEndTxn, vote=%s",
1356                   dsmVote == DSM_VOTE_COMMIT ? "COMMIT" : "ABORT");
1357       ourTrace(BSAHandle,TrFL, traceStr2);
1358       /*===========================================================
1359         check if this EndTxn call was proceeded by a Send call which
1360         returned WILL_ABORT. If so, make sure the vote is COMMIT
1361         so we will get the server's reason code.
1362         ===========================================================*/
1363       if (xopenGbl.sessFlags & FL_RC_WILL_ABORT)
1364       {
1365          dsmVote = DSM_VOTE_COMMIT;
1366
1367          sprintf(traceStr2, "BSAEndTxn: sproceeded by RC_WILL_ABORT, so use vote=COMMIT");
1368          ourTrace(BSAHandle,TrFL, traceStr2);
1369          xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_RC_WILL_ABORT); /* set off*/
1370       }
1371
1372       rc = AFSdsmEndTxn(dsmHandle, dsmVote, &reason);
1373       xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_DSM_TXN); /* set off */
1374
1375       /*====================================================
1376        if the caller wanted to Abort, then return rc=OK, not
1377        our usual 2302 and reason 3
1378        =====================================================*/
1379       if ((dsmVote ==  DSM_VOTE_ABORT)     &&
1380           (rc == DSM_RC_CHECK_REASON_CODE) &&
1381           (reason == DSM_RC_ABORT_BY_CLIENT))
1382
1383          rc = DSM_RC_OK;
1384
1385       if (rc)
1386       {
1387          sprintf(traceStr2, "BSAEndTxn(AFSdsmEndTxn) error rc = %d", rc);
1388          ourTrace(BSAHandle,TrFL, traceStr2);
1389
1390          AFSdsmRCMsg(BSAHandle, reason, rsMsg);
1391          sprintf(traceStr2, "BSAEndTxn: reason code = %d, Message='%s'", reason, rsMsg);
1392          ourTrace(BSAHandle,TrFL, traceStr2);
1393
1394          strcpy(ourMessage, ourRCMsg(reason, errPrefix));
1395          ourLogEvent_Ex(BSAHandle,logBoth, ourMessage, errPrefix, logSevError);
1396       }
1397       xlateRC(BSAHandle, rc, &bsaRC);
1398
1399       if (rc == DSM_RC_CHECK_REASON_CODE) /* return reason code instead */
1400       {
1401          xlateRC(BSAHandle, reason, &bsaRC);
1402       }
1403    }
1404
1405    /*=== check if Query was terminated ===*/
1406    if (xopenGbl.sessFlags & FL_IN_BSA_QRY)
1407    {
1408       AFSdsmEndQuery(dsmHandle);
1409       xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_BSA_QRY); /* set off */
1410    }
1411
1412    xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_BSA_TXN); /* set off */
1413    XOPENRETURN(BSAHandle, "BSAEndTxn",
1414                bsaRC,__FILE__,__LINE__);
1415 }
1416
1417 void  BSAQueryApiVersion(
1418 ApiVersion       *BSAapiVerP
1419 )
1420
1421 {
1422    /*===============================================================
1423     Don't actually call the base QueryApiVersion call.
1424     Use the defines from custom.h since this is what the application
1425     sees in the header file. The constants should be the same,
1426     but may get out of sync occasionally.
1427     ===============================================================*/
1428    BSAapiVerP->version = BSA_API_VERSION;
1429    BSAapiVerP->release = BSA_API_RELEASE;
1430    BSAapiVerP->level   = BSA_API_LEVEL;
1431    return;
1432 }
1433
1434 BSA_Int16 BSAQueryObject(
1435 long              BSAHandle,
1436 QueryDescriptor  *BSAqryDescP,
1437 ObjectDescriptor *BSAobjDescP
1438 )
1439 {
1440    dsInt16_t      rc = 0;
1441    BSA_Int16      bsaRC = 0;
1442    dsUint32_t     dsmHandle;
1443    dsmObjName     objName;
1444    dsUint8_t      dsmObjType;
1445    dsmQueryType   query_type;
1446    dsmQueryBuff   *query_buff;
1447    DataBlk        qData;
1448    qryArchiveData archiveData;
1449    qryRespArchiveData respArchive;
1450    qryBackupData  backupData;
1451    qryRespBackupData  respBackup;
1452    BSAObjectOwner upperNode;  /* upper cased node name */
1453
1454    char           errPrefix[DSM_MAX_RC_MSG_LENGTH + 1];
1455    char           ourMessage[DSM_MAX_RC_MSG_LENGTH + 1];
1456
1457    if(!dsm_init)
1458    {
1459 #ifdef DEBUG_BUTC
1460         printf("TSM library not mounted. \n");
1461 #endif
1462         if (dsm_MountLibrary())
1463         {
1464                 printf("TSM Library initialisation failed. \n");
1465                 return 1 ;
1466         }
1467 #ifdef DEBUG_BUTC
1468         printf("TSM Library initialisation SUCCESS. \n");
1469 #endif
1470    }
1471
1472    memset(errPrefix,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1473    memset(ourMessage,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1474
1475    dsmHandle = BSAHandle;
1476
1477    memset(&backupData, 0x00, sizeof(qryBackupData));
1478
1479    sprintf(traceStr2, "BSAQueryObject ENTRY: BSAHandle:%ld ObjOwner(qryDesc):'%s' appOwner(qryDesc):'%s' \n ObjName(qryDesc):'%.*s%.*s' \n copyType:%d ObjectType:%d status:%d ",
1480           BSAHandle,
1481           BSAqryDescP->owner.bsaObjectOwner,
1482           BSAqryDescP->owner.appObjectOwner,
1483           100,BSAqryDescP->objName.objectSpaceName,
1484           100,BSAqryDescP->objName.pathName,
1485           BSAqryDescP->copyType,
1486           BSAqryDescP->objectType,
1487           BSAqryDescP->status);
1488    ourTrace(BSAHandle, TrFL, traceStr2);
1489
1490    /*=====================================================
1491     init objDesc area to 0's before we start to erase any
1492     garbage that might be there.
1493     =====================================================*/
1494    memset(BSAobjDescP, 0x00, sizeof(ObjectDescriptor));
1495
1496    /*=== init global look ahead pointer to NULL ===*/
1497    if (xopenGbl.nextQryP != NULL)
1498    {
1499       dsFree(xopenGbl.nextQryP);
1500       xopenGbl.nextQryP = NULL;
1501    }
1502
1503    /*=========================================================
1504     if the node name is different - we won't query it compare
1505     both the value passed on the BSAInit and the final value
1506     used for the session (which may be different for Generate).
1507     =========================================================*/
1508    strcpy(upperNode, BSAqryDescP->owner.bsaObjectOwner);
1509    StrUpper(upperNode);
1510    if ((strcmp(upperNode, xopenGbl.dsmSessInfo.id)) &&
1511       (strcmp(BSAqryDescP->owner.bsaObjectOwner, xopenGbl.bsaObjectOwner)))
1512    {
1513       sprintf(traceStr2,
1514       "BSAQueryObject: BSAObjectOwner(%s) doesn't match value for session(%s).", upperNode, xopenGbl.dsmSessInfo.id);
1515       ourTrace(BSAHandle,TrFL, traceStr2);
1516       bsaRC = ADSM_RC_INVALID_NODE;
1517       strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
1518       ourLogEvent_Ex(BSAHandle, logLocal, ourMessage, errPrefix, logSevError);
1519       XOPENRETURN(BSAHandle, "BSAQueryObject",
1520                   bsaRC,__FILE__,__LINE__);
1521    }
1522
1523    /*=====================================================
1524      currently BSA_MAX_OSNAME = DSM_MAX_FSNAME_LENGTH
1525      if this changes, use strncpy
1526      ====================================================*/
1527    if (strlen(BSAqryDescP->objName.objectSpaceName) > BSA_MAX_OSNAME)
1528    {
1529       sprintf(traceStr2, "BSAQueryObject: objectSpaceName too long (%" AFS_SIZET_FMT ").",
1530               strlen(BSAqryDescP->objName.objectSpaceName));
1531       ourTrace(BSAHandle,TrFL, traceStr2);
1532       bsaRC = BSA_RC_OBJNAME_TOO_LONG;
1533       XOPENRETURN(BSAHandle, "BSAQueryObject",
1534                   bsaRC,__FILE__,__LINE__);
1535    }
1536    /*=================================================================
1537      the entire pathname gets copied into hl during parsing, so check
1538      for that max len as well. For now these are the same value.
1539      ================================================================*/
1540    if (strlen(BSAqryDescP->objName.pathName) >
1541        min(DSM_MAX_HL_LENGTH, BSA_MAX_PATHNAME))
1542    {
1543       sprintf(traceStr2, "BSAQueryObject: pathName too long (%" AFS_SIZET_FMT ")",
1544               strlen(BSAqryDescP->objName.pathName));
1545       ourTrace(BSAHandle, TrFL, traceStr2);
1546       bsaRC = BSA_RC_OBJNAME_TOO_LONG;
1547       XOPENRETURN(BSAHandle, "BSAQueryObject",
1548                   bsaRC,__FILE__,__LINE__);
1549    }
1550    if ((BSAqryDescP->objectType == BSAObjectType_FILE) ||
1551        (BSAqryDescP->objectType == BSAObjectType_DATABASE))
1552       dsmObjType = DSM_OBJ_FILE;
1553    else
1554       if (BSAqryDescP->objectType == BSAObjectType_DIRECTORY)
1555          dsmObjType = DSM_OBJ_DIRECTORY;
1556       else
1557          if (BSAqryDescP->objectType == BSAObjectType_ANY)
1558             dsmObjType = DSM_OBJ_ANY_TYPE;
1559          else
1560          {
1561              sprintf(traceStr2,
1562                      "BSAQueryObject: invalid objectType (%d)", BSAqryDescP->objectType);
1563              ourTrace(BSAHandle,TrFL, traceStr2);
1564              bsaRC = ADSM_RC_INVALID_OBJTYPE;
1565              strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
1566              ourLogEvent_Ex(BSAHandle, logLocal, ourMessage, errPrefix, logSevError);
1567              XOPENRETURN(BSAHandle, "BSAQueryObject",
1568                          bsaRC,__FILE__,__LINE__);
1569          }
1570
1571    if (!(xopenGbl.sessFlags & FL_IN_BSA_TXN))
1572    {
1573       sprintf(traceStr2, "BSAQueryObject: issued without BSABeginTxn");
1574       ourTrace(BSAHandle,TrFL, traceStr2);
1575       bsaRC = BSA_RC_BAD_CALL_SEQUENCE;
1576       XOPENRETURN(BSAHandle, "BSAQueryObject",
1577                   bsaRC,__FILE__,__LINE__);
1578    }
1579
1580    /* store object name in ADSM structure */
1581    strcpy(objName.fs,BSAqryDescP->objName.objectSpaceName) ;
1582
1583    if (!strcmp(BSAqryDescP->objName.pathName, "/*")) /* if they use a wildcard */
1584    {  /* we need to use a wild card on both hl and ll since the path gets split */
1585       strcpy(objName.hl, "*");  /* don't include delimiter in case there is
1586                              no hl */
1587       strcpy(objName.ll, "/*");
1588    }
1589    else
1590       xparsePath(BSAHandle, BSAqryDescP->objName.pathName, objName.hl, objName.ll);
1591
1592       objName.objType = dsmObjType ;
1593
1594       /*=== save copy type for GetNextQuery call later ===*/
1595       xopenGbl.copyType = BSAqryDescP->copyType;
1596
1597       if (BSAqryDescP->copyType == BSACopyType_ARCHIVE)
1598       {
1599          if (strlen(BSAqryDescP->desc) > ADSM_MAX_DESC)
1600          {
1601
1602             sprintf(traceStr2, "BSAQueryObject: description longer than ADSM max (%" AFS_SIZET_FMT "). ", strlen(BSAqryDescP->desc));
1603             ourTrace(BSAHandle,TrFL, traceStr2);
1604             bsaRC = BSA_RC_DESC_TOO_LONG;
1605             XOPENRETURN(BSAHandle, "BSAQueryObject",
1606                         bsaRC,__FILE__,__LINE__);
1607          }
1608
1609          archiveData.stVersion = qryArchiveDataVersion;
1610          archiveData.objName   = &objName;
1611          archiveData.owner     = (char *)&BSAqryDescP->owner.appObjectOwner;
1612
1613          /*=================================================================
1614            in the tm structure the month count starts with 0 and year is
1615            the value since 1900.
1616            Adjust the year value only if it isn't one of the boundary values
1617            =================================================================*/
1618
1619          archiveData.insDateLowerBound.year   = BSAqryDescP->createTimeLB.tm_year;
1620          if (archiveData.insDateLowerBound.year != ADSM_LOWEST_BOUND)
1621             archiveData.insDateLowerBound.year  += 1900;
1622          archiveData.insDateLowerBound.month  = BSAqryDescP->createTimeLB.tm_mon+1;
1623          archiveData.insDateLowerBound.day    = BSAqryDescP->createTimeLB.tm_mday;
1624          archiveData.insDateLowerBound.hour   = BSAqryDescP->createTimeLB.tm_hour;
1625          archiveData.insDateLowerBound.minute = BSAqryDescP->createTimeLB.tm_min;
1626          archiveData.insDateLowerBound.second = BSAqryDescP->createTimeLB.tm_sec;
1627
1628          archiveData.insDateUpperBound.year   = BSAqryDescP->createTimeUB.tm_year;
1629          if (archiveData.insDateUpperBound.year != ADSM_HIGHEST_BOUND)
1630              archiveData.insDateUpperBound.year  += 1900;
1631          archiveData.insDateUpperBound.month  = BSAqryDescP->createTimeUB.tm_mon+1;
1632          archiveData.insDateUpperBound.day    = BSAqryDescP->createTimeUB.tm_mday;
1633          archiveData.insDateUpperBound.hour   = BSAqryDescP->createTimeUB.tm_hour;
1634          archiveData.insDateUpperBound.minute = BSAqryDescP->createTimeUB.tm_min;
1635          archiveData.insDateUpperBound.second = BSAqryDescP->createTimeUB.tm_sec;
1636
1637          archiveData.expDateLowerBound.year   = BSAqryDescP->expireTimeLB.tm_year;
1638          if (archiveData.expDateLowerBound.year != ADSM_LOWEST_BOUND)
1639              archiveData.expDateLowerBound.year  += 1900;
1640          archiveData.expDateLowerBound.month  = BSAqryDescP->expireTimeLB.tm_mon+1;
1641          archiveData.expDateLowerBound.day    = BSAqryDescP->expireTimeLB.tm_mday;
1642          archiveData.expDateLowerBound.hour   = BSAqryDescP->expireTimeLB.tm_hour;
1643          archiveData.expDateLowerBound.minute = BSAqryDescP->expireTimeLB.tm_min;
1644          archiveData.expDateLowerBound.second = BSAqryDescP->expireTimeLB.tm_sec;
1645
1646          archiveData.expDateUpperBound.year   = BSAqryDescP->expireTimeUB.tm_year;
1647          if (archiveData.expDateUpperBound.year != ADSM_HIGHEST_BOUND)
1648             archiveData.expDateUpperBound.year  += 1900;
1649          archiveData.expDateUpperBound.month  = BSAqryDescP->expireTimeUB.tm_mon+1;
1650          archiveData.expDateUpperBound.day    = BSAqryDescP->expireTimeUB.tm_mday;
1651          archiveData.expDateUpperBound.hour   = BSAqryDescP->expireTimeUB.tm_hour;
1652          archiveData.expDateUpperBound.minute = BSAqryDescP->expireTimeUB.tm_min;
1653          archiveData.expDateUpperBound.second = BSAqryDescP->expireTimeUB.tm_sec;
1654
1655          archiveData.descr = (char *)&BSAqryDescP->desc;
1656          query_type = qtArchive;
1657          query_buff = (dsmQueryBuff *)&archiveData;
1658
1659          qData.stVersion = DataBlkVersion;
1660          qData.bufferLen = sizeof(qryRespArchiveData);
1661          qData.bufferPtr = (char *)&respArchive;
1662          respArchive.stVersion = qryRespArchiveDataVersion;
1663    }
1664    else
1665       if (BSAqryDescP->copyType == BSACopyType_BACKUP)
1666       {
1667          if (BSAqryDescP->status == BSAObjectStatus_ACTIVE)
1668             backupData.objState  = DSM_ACTIVE;
1669          else
1670             if (BSAqryDescP->status == BSAObjectStatus_INACTIVE)
1671                backupData.objState  = DSM_INACTIVE;
1672             else
1673                if (BSAqryDescP->status == BSAObjectStatus_ANY)
1674                   backupData.objState  = DSM_ANY_MATCH;
1675                else
1676                {
1677                    sprintf(traceStr2,
1678                            "BSAQueryObject: invalid status (%d)",
1679                             BSAqryDescP->status);
1680                    ourTrace(BSAHandle,TrFL, traceStr2);
1681                    bsaRC = ADSM_RC_INVALID_STATUS;
1682                    strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
1683                    ourLogEvent_Ex(BSAHandle, logLocal, ourMessage, errPrefix, logSevError);
1684                    XOPENRETURN(BSAHandle, "BSAQueryObject",
1685                                bsaRC,__FILE__,__LINE__);
1686                }
1687
1688             backupData.stVersion = qryBackupDataVersion;
1689             backupData.objName   = &objName;
1690             backupData.owner     = (char *)&BSAqryDescP->owner.appObjectOwner;
1691
1692             query_type = qtBackup;
1693             query_buff = (dsmQueryBuff *)&backupData;
1694
1695             qData.stVersion = DataBlkVersion;
1696             qData.bufferLen = sizeof(qryRespBackupData);
1697             qData.bufferPtr = (char *)&respBackup;
1698             respBackup.stVersion = qryRespBackupDataVersion;;
1699       }
1700       else
1701       {
1702          sprintf(traceStr2,
1703                            "BSAQueryObject: invalid copyType (%d)",
1704                             BSAqryDescP->copyType);
1705          ourTrace(BSAHandle,TrFL, traceStr2);
1706          bsaRC = ADSM_RC_INVALID_COPYTYPE;
1707          strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
1708          ourLogEvent_Ex(BSAHandle, logLocal, ourMessage, errPrefix, logSevError);
1709          XOPENRETURN(BSAHandle, "BSAQueryObject",
1710                      bsaRC,__FILE__,__LINE__);
1711       }
1712
1713       /*=== now set flag for query ===*/
1714       xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_IN_BSA_QRY); /* set on */
1715
1716       if ((rc = AFSdsmBeginQuery(dsmHandle,query_type,query_buff)))
1717       {
1718
1719          sprintf(traceStr2, "BSAQueryObject(AFSdsmBeginQuery) error rc = %d", rc);
1720          ourTrace(BSAHandle,TrFL, traceStr2);
1721          xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_BSA_QRY); /* set off */
1722          xlateRC(BSAHandle, rc, &bsaRC);
1723          XOPENRETURN(BSAHandle, "BSAQueryObject(AFSdsmBeginQuery)",
1724                                  bsaRC,__FILE__,__LINE__);
1725       }
1726
1727       rc = AFSdsmGetNextQObj(dsmHandle,&qData);
1728       if (((rc == DSM_RC_OK) || (rc == DSM_RC_MORE_DATA)) && qData.numBytes)
1729       {
1730          BSAobjDescP->version = ObjectDescriptorVersion;
1731          strcpy(BSAobjDescP->Owner.bsaObjectOwner, xopenGbl.dsmSessInfo.id);
1732          BSAobjDescP->copyType = BSAqryDescP->copyType;
1733          strcpy(BSAobjDescP->cGName, "\0");   /* no copy group name returned */
1734
1735          if (BSAqryDescP->copyType == BSACopyType_ARCHIVE)
1736          {
1737             fillArchiveResp(dsmHandle, BSAobjDescP, &respArchive);
1738          }
1739          else /* backup */
1740          {
1741             fillBackupResp(dsmHandle, BSAobjDescP, &respBackup);
1742          }
1743          /*=== look ahead and see if there is more or if this is really done ===*/
1744          rc = AFSdsmGetNextQObj(dsmHandle,&qData);
1745          if (( (rc == DSM_RC_OK) || (rc == DSM_RC_MORE_DATA)) && qData.numBytes)
1746          { /*=== save the response we just got for later call ===*/
1747             if (BSAqryDescP->copyType == BSACopyType_ARCHIVE)
1748             {
1749                if (!(xopenGbl.nextQryP = (char *)dsMalloc(sizeof(qryRespArchiveData))))
1750                {
1751
1752                   sprintf(traceStr2, "BSAQueryObject: no memory for look ahead buffer");
1753                   ourTrace(BSAHandle,TrFL, traceStr2);
1754                   xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_BSA_QRY); /* set off */
1755                   bsaRC = BSA_RC_NO_RESOURCES;
1756                   XOPENRETURN(BSAHandle, "BSAQueryObject(AFSdsmGetNextQObj)",
1757                               bsaRC,__FILE__,__LINE__);
1758                }
1759                memcpy(xopenGbl.nextQryP, &respArchive, sizeof(qryRespArchiveData));
1760             }
1761             else
1762             {
1763                if (!(xopenGbl.nextQryP = (char *)dsMalloc(sizeof(qryRespBackupData))))
1764                {
1765
1766                   sprintf(traceStr2, "BSAQueryObject: no memory for look ahead buffer");
1767                   ourTrace(BSAHandle,TrFL, traceStr2);
1768                   xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_BSA_QRY); /* set off */
1769                   bsaRC = BSA_RC_NO_RESOURCES;
1770                   XOPENRETURN(BSAHandle, "BSAQueryObject(AFSdsmGetNextQObj)",
1771                               bsaRC,__FILE__,__LINE__);
1772
1773                }
1774                memcpy(xopenGbl.nextQryP, &respBackup, sizeof(qryRespBackupData));
1775             }
1776          }
1777       else
1778       {
1779
1780          sprintf(traceStr2, "BSAQueryObject(AFSdsmGetNextQObj) rc = %d", rc);
1781          ourTrace(BSAHandle,TrFL, traceStr2);
1782          AFSdsmEndQuery(dsmHandle);
1783          xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_BSA_QRY); /* set off */
1784       }
1785
1786    }
1787    else /* could be FINISHED or an error */
1788    {
1789
1790       sprintf(traceStr2, "BSAQueryObject(AFSdsmGetNextQObj) rc = %d", rc);
1791       ourTrace(BSAHandle,TrFL, traceStr2);
1792       AFSdsmEndQuery(dsmHandle);
1793       xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_BSA_QRY); /* set off */
1794    }
1795
1796    if (rc == DSM_RC_OK)
1797    {
1798       sprintf(traceStr2, "BSAQueryObject(AFSdsmGetNextQObj) rc = %d, ObjOwner(objDesc):'%s' appOwner(objDesc):'%s' \n ObjName(objDesc):'%.*s%.*s' \n copyType:%d copyId:'%lu %lu' cGName:'%s'",
1799        rc,
1800        BSAobjDescP->Owner.bsaObjectOwner,
1801        BSAobjDescP->Owner.appObjectOwner,
1802        100,BSAobjDescP->objName.objectSpaceName,
1803        100,BSAobjDescP->objName.pathName,
1804        BSAobjDescP->copyType,
1805        BSAobjDescP->copyId.left,
1806        BSAobjDescP->copyId.right,
1807        BSAobjDescP->cGName);
1808        ourTrace(BSAHandle,TrFL, traceStr2);
1809    }
1810
1811    xlateRC(BSAHandle, rc, &bsaRC);
1812    XOPENRETURN(BSAHandle, "BSAQueryObject",
1813                bsaRC, __FILE__,__LINE__);
1814 }
1815
1816 BSA_Int16 BSAGetObject(
1817     long              BSAHandle,
1818     ObjectDescriptor *BSAobjDescP,
1819     DataBlock        *BSAdataBlockP
1820 )
1821 {
1822    dsInt16_t      rc = 0;
1823    BSA_Int16      bsaRC = 0;
1824    dsUint32_t     dsmHandle;
1825    DataBlk        getBlk;
1826    dsmGetType     getType;
1827    dsmGetList     dsmObjList ;
1828    char           rcMsg[DSM_MAX_RC_MSG_LENGTH + 1];
1829    char           errPrefix[DSM_MAX_RC_MSG_LENGTH + 1];
1830    char           ourMessage[DSM_MAX_RC_MSG_LENGTH + 1];
1831
1832    if(!dsm_init)
1833    {
1834 #ifdef DEBUG_BUTC
1835         printf("TSM library not mounted. \n");
1836 #endif
1837         if (dsm_MountLibrary())
1838         {
1839                 printf("TSM Library initialisation failed. \n");
1840                 return 1 ;
1841         }
1842 #ifdef DEBUG_BUTC
1843         printf("TSM Library initialisation SUCCESS. \n");
1844 #endif
1845    }
1846
1847    dsmHandle = BSAHandle;
1848
1849    memset(rcMsg,         '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1850    memset(errPrefix,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1851    memset(ourMessage,    '\0', DSM_MAX_RC_MSG_LENGTH + 1);
1852
1853    sprintf(traceStr2, "BSAGetObject ENTRY: BSAHandle:%ld version:%lu copyType:%d copyId:'%lu %lu' \n bufferLen:%d numBytes:%d ",
1854            BSAHandle,
1855            BSAobjDescP->version,
1856            BSAobjDescP->copyType,
1857            BSAobjDescP->copyId.left,
1858            BSAobjDescP->copyId.right,
1859            BSAdataBlockP->bufferLen,
1860            BSAdataBlockP->numBytes);
1861    ourTrace(BSAHandle, TrFL, traceStr2);
1862
1863    xopenGbl.oper = OPER_RECV_START;    /* save state for EndData later */
1864
1865    if (BSAobjDescP->version != ObjectDescriptorVersion)
1866    {
1867       sprintf(traceStr2,"Warning: BSAGetObject: objectDesc.version unexpected %lu.", BSAobjDescP->version);
1868       ourTrace(BSAHandle,TrFL, traceStr2);
1869       /*==================================================================
1870        don't treat this as an error now since it isn't defined in the spec
1871        bsaRC = ADSM_RC_INVALID_ST_VER;
1872        return(bsaRC);
1873        =================================================================*/
1874    }
1875
1876    if (!(xopenGbl.sessFlags & FL_IN_BSA_TXN))
1877    {
1878       sprintf(traceStr2, "BSAGetObject: issued without BSABeginTxn");
1879       ourTrace(BSAHandle,TrFL, traceStr2);
1880       bsaRC = BSA_RC_BAD_CALL_SEQUENCE;
1881       XOPENRETURN(BSAHandle, "BSAGetObject",
1882                   bsaRC,__FILE__,__LINE__);
1883    }
1884
1885    /*=== Get the data ===*/
1886    if (BSAobjDescP->copyType == BSACopyType_ARCHIVE)
1887       getType = gtArchive;
1888    else
1889       if (BSAobjDescP->copyType == BSACopyType_BACKUP)
1890          getType = gtBackup;
1891       else
1892       {
1893          sprintf(traceStr2,
1894                            "BSAGetObject: invalid copyType (%d)",
1895                             BSAobjDescP->copyType);
1896          ourTrace(BSAHandle,TrFL, traceStr2);
1897          bsaRC = ADSM_RC_INVALID_COPYTYPE;
1898          strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
1899          ourLogEvent_Ex(BSAHandle, logLocal, ourMessage, errPrefix, logSevError);
1900          XOPENRETURN(BSAHandle, "BSAGetObject",
1901                      bsaRC,__FILE__,__LINE__);
1902       }
1903
1904    /*=== now update state since we'll issue a base API call ===*/
1905    xopenGbl.oper = OPER_RECV_ISSUED;    /* save state for EndData later */
1906    /*=== setup for a single object get ===*/
1907
1908    dsmObjList.stVersion = dsmGetListVersion ;
1909    dsmObjList.numObjId = 1 ;
1910    dsmObjList.objId = (ObjID *)dsMalloc(sizeof(ObjID) * dsmObjList.numObjId) ;
1911
1912    dsmObjList.objId[0].hi = BSAobjDescP->copyId.left ;
1913    dsmObjList.objId[0].lo = BSAobjDescP->copyId.right ;
1914
1915    if ((rc = AFSdsmBeginGetData(dsmHandle,bTrue,   /* always say MountWait */
1916                              getType,
1917                              &dsmObjList)) != DSM_RC_OK)
1918    {
1919       sprintf(traceStr2, "BSAGetObject: Call to AFSdsmBeginGetData error rc = %d", rc);
1920       ourTrace(BSAHandle,TrFL, traceStr2);
1921       xlateRC(BSAHandle, rc, &bsaRC);
1922       XOPENRETURN(BSAHandle, "BSAGetObject(AFSdsmBeginGetData)",
1923                   bsaRC,__FILE__,__LINE__);
1924    }
1925
1926    /*********************************************************************/
1927    getBlk.stVersion = DataBlkVersion ;
1928    getBlk.bufferPtr = BSAdataBlockP->bufferPtr ;
1929    getBlk.bufferLen = BSAdataBlockP->bufferLen;
1930    getBlk.numBytes  = 0;
1931
1932    rc = AFSdsmGetObj(dsmHandle, &(dsmObjList.objId[0]), &getBlk) ;
1933
1934    if ((rc == DSM_RC_FINISHED) &&
1935        (getBlk.numBytes != 0))    /* actually is data received */
1936       rc = DSM_RC_MORE_DATA;      /* use different rc for consistency */
1937                                   /* with Query. No data returned */
1938                                   /* if rc = FINISHED.     */
1939    if ((rc == DSM_RC_FINISHED) ||
1940        (rc == DSM_RC_MORE_DATA))
1941    {
1942       /*=== data is already in the buffer ===*/
1943       BSAdataBlockP->numBytes = (BSA_UInt16)getBlk.numBytes;
1944    }
1945    else
1946    {
1947       /*=== appl may call BSAEndData to clean up trxn but don't count on it... ===*/
1948       AFSdsmEndGetObj(dsmHandle);
1949       AFSdsmEndGetData(dsmHandle);
1950       xopenGbl.sessFlags =
1951                (xopenGbl.sessFlags | FL_END_DATA_DONE);  /* turn flag on */
1952     }
1953
1954    xlateRC(BSAHandle, rc, &bsaRC);
1955    XOPENRETURN(BSAHandle, "BSAGetObject",
1956                bsaRC,__FILE__,__LINE__);
1957 }
1958
1959 BSA_Int16 BSAGetData(
1960     long              BSAHandle,
1961     DataBlock        *BSAdataBlockP
1962 )
1963 {
1964    dsInt16_t      rc = 0;
1965    BSA_Int16      bsaRC = 0;
1966    dsUint32_t     dsmHandle;
1967    DataBlk        getBlk;
1968
1969    if(!dsm_init)
1970    {
1971 #ifdef DEBUG_BUTC
1972         printf("TSM library not mounted. \n");
1973 #endif
1974         if (dsm_MountLibrary())
1975         {
1976                 printf("TSM Library initialisation failed. \n");
1977                 return 1 ;
1978         }
1979 #ifdef DEBUG_BUTC
1980         printf("TSM Library initialisation SUCCESS. \n");
1981 #endif
1982    }
1983
1984    dsmHandle = BSAHandle;
1985
1986
1987    sprintf(traceStr2, "BSAGetData ENTRY: BSAHandle:%ld, bufferLen:%d, numBytes:%d",
1988            BSAHandle,
1989            BSAdataBlockP->bufferLen,
1990            BSAdataBlockP->numBytes);
1991    ourTrace(BSAHandle,TrFL, traceStr2);
1992
1993    getBlk.stVersion = DataBlkVersion ;
1994    getBlk.bufferPtr = BSAdataBlockP->bufferPtr ;
1995    getBlk.bufferLen = BSAdataBlockP->bufferLen;
1996    getBlk.numBytes  = 0;
1997
1998    rc = AFSdsmGetData(dsmHandle, &getBlk) ;
1999
2000    if ((rc == DSM_RC_FINISHED) &&
2001        (getBlk.numBytes != 0))      /* actually is data received */
2002       rc = DSM_RC_MORE_DATA;        /* use different rc for consistency */
2003                                     /* with Query. No data returned */
2004                                     /* if rc = FINISHED.     */
2005
2006    if ((rc == DSM_RC_FINISHED) ||
2007        (rc == DSM_RC_MORE_DATA))
2008    {
2009       /*=== data is already in the buffer ===*/
2010       BSAdataBlockP->numBytes = (BSA_UInt16)getBlk.numBytes;
2011    }
2012    else
2013    {
2014       sprintf(traceStr2, "BSAGetData: Call to AFSdsmGetData error rc = %d", rc);
2015       ourTrace(BSAHandle, TrFL,traceStr2);
2016
2017       /*=== appl may call BSAEndData to clean up trxn but don't count on it... ===*/
2018       AFSdsmEndGetObj(dsmHandle);
2019       AFSdsmEndGetData(dsmHandle);
2020       xopenGbl.sessFlags =
2021                (xopenGbl.sessFlags | FL_END_DATA_DONE);  /* turn flag on */
2022    }
2023    xlateRC(BSAHandle, rc, &bsaRC);
2024    XOPENRETURN(BSAHandle, "BSAGetData(AFSdsmGetData)",
2025                bsaRC,__FILE__,__LINE__);
2026 }
2027
2028 BSA_Int16 BSASendData(
2029     long           BSAHandle,
2030     DataBlock     *BSAdataBlockP
2031 )
2032 {
2033    dsInt16_t      rc = 0;
2034    BSA_Int16      bsaRC = 0;
2035    dsUint32_t     dsmHandle;
2036    DataBlk        dataBlkArea;
2037
2038    if(!dsm_init)
2039    {
2040 #ifdef DEBUG_BUTC
2041         printf("TSM library not mounted. \n");
2042 #endif
2043         if (dsm_MountLibrary())
2044         {
2045                 printf("TSM Library initialisation failed. \n");
2046                 return 1 ;
2047         }
2048 #ifdef DEBUG_BUTC
2049         printf("TSM Library initialisation SUCCESS. \n");
2050 #endif
2051    }
2052
2053    dsmHandle = BSAHandle;
2054
2055
2056    sprintf(traceStr2, "BSASendData ENTRY: BSAHandle:%ld bufferLen: %d numBytes: %d ",
2057            BSAHandle,
2058            BSAdataBlockP->bufferLen,
2059            BSAdataBlockP->numBytes);
2060    ourTrace(BSAHandle, TrFL, traceStr2);
2061
2062    /*=== check for NULL dataBlock pointer ===*/
2063    if (BSAdataBlockP == NULL)
2064       XOPENRETURN(BSAHandle, "BSASendData",
2065                   BSA_RC_NULL_POINTER,__FILE__,__LINE__);
2066       dataBlkArea.stVersion = DataBlkVersion ;
2067       dataBlkArea.bufferPtr = BSAdataBlockP->bufferPtr ;
2068       dataBlkArea.bufferLen = BSAdataBlockP->bufferLen;
2069
2070       rc = AFSdsmSendData(dsmHandle, &dataBlkArea);
2071
2072       if (rc)
2073       {
2074
2075          sprintf(traceStr2, "BSASendData(AFSdsmSendData) error rc = %d", rc);
2076          ourTrace(BSAHandle,TrFL, traceStr2);
2077
2078          if (rc == DSM_RC_WILL_ABORT)  /* save flag */
2079             xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_RC_WILL_ABORT);
2080       }
2081       BSAdataBlockP->numBytes = (BSA_UInt16)dataBlkArea.numBytes;
2082       sprintf(traceStr2, "BSASendData(AFSdsmSendData): BSAHandle:%ld bufferLen: %d numBytes sent: %d ",
2083            BSAHandle,
2084            BSAdataBlockP->bufferLen,
2085            BSAdataBlockP->numBytes);
2086       ourTrace(BSAHandle, TrFL, traceStr2);
2087
2088       xlateRC(BSAHandle, rc, &bsaRC);
2089       XOPENRETURN(BSAHandle, "BSASendData",
2090                   BSA_RC_SUCCESS,__FILE__,__LINE__);
2091 }
2092
2093 BSA_Int16 BSAEndData(
2094     long          BSAHandle
2095 )
2096 {
2097    dsInt16_t      rc = 0;
2098    BSA_Int16      bsaRC = 0;
2099    dsUint32_t     dsmHandle;
2100
2101    if(!dsm_init)
2102    {
2103 #ifdef DEBUG_BUTC
2104         printf("TSM library not mounted. \n");
2105 #endif
2106         if (dsm_MountLibrary())
2107         {
2108                 printf("TSM Library initialisation failed. \n");
2109                 return 1 ;
2110         }
2111 #ifdef DEBUG_BUTC
2112         printf("TSM Library initialisation SUCCESS. \n");
2113 #endif
2114    }
2115
2116    dsmHandle = BSAHandle;
2117
2118
2119    sprintf(traceStr2, "BSAEndData ENTRY: BSAHandle:%ld", BSAHandle);
2120    ourTrace(BSAHandle,TrFL, traceStr2);
2121
2122    /*=======================================================
2123      check the state - don't issue base API call unless one
2124      was called previously, or else we get a sequence error.
2125      ======================================================*/
2126
2127    if (xopenGbl.oper == OPER_SEND_ISSUED)
2128    {
2129       rc = AFSdsmEndSendObj(dsmHandle);
2130       if (rc)
2131       {
2132
2133          sprintf(traceStr2, "BSAEndData(AFSdsmEndSendObj) error rc = %d", rc);
2134          ourTrace(BSAHandle,TrFL, traceStr2);
2135          xlateRC(BSAHandle, rc, &bsaRC);
2136       }
2137    }
2138    else
2139       if (xopenGbl.oper == OPER_RECV_ISSUED)
2140       {
2141           if (xopenGbl.sessFlags & FL_END_DATA_DONE)   /* EndData processing */
2142              xopenGbl.sessFlags =                     /*  already done */
2143                    (xopenGbl.sessFlags ^ FL_END_DATA_DONE);  /* turn flag off */
2144          else
2145          {
2146             rc = AFSdsmEndGetObj(dsmHandle);
2147             if (rc)
2148             {
2149                sprintf(traceStr2, "BSAEndData(AFSdsmEndGetObj) error rc = %d", rc);
2150                ourTrace(BSAHandle, TrFL, traceStr2);
2151
2152                /*==============================================================
2153                 this may get 'Out of sequence error' if previous GetObject
2154                 had rc=2 (no match). Don't return this to caller - too confusing
2155                 ===============================================================*/
2156                if (rc != DSM_RC_BAD_CALL_SEQUENCE)
2157                xlateRC(BSAHandle, rc, &bsaRC);
2158             }
2159
2160             rc = AFSdsmEndGetData(dsmHandle);
2161             if (rc)
2162             {
2163                sprintf(traceStr2, "BSAEndData(AFSdsmEndGetData) error rc = %d", rc);
2164                ourTrace(BSAHandle, TrFL, traceStr2);
2165                xlateRC(BSAHandle, rc, &bsaRC);
2166             }
2167          }
2168       }
2169    else  /* invalid state */
2170       if ((xopenGbl.oper != OPER_RECV_START) &&
2171           (xopenGbl.oper != OPER_SEND_START))
2172       {
2173          sprintf(traceStr2, "BSAEndData: BSAEndData but not Send or Recv");
2174          ourTrace(BSAHandle,TrFL, traceStr2);
2175          bsaRC = BSA_RC_BAD_CALL_SEQUENCE;
2176       }
2177
2178    xopenGbl.oper = OPER_NONE;
2179    XOPENRETURN(BSAHandle, "BSAEndData",bsaRC,__FILE__,__LINE__);
2180 }
2181
2182 BSA_Int16 BSACreateObject(
2183     long              BSAHandle,
2184     ObjectDescriptor *BSAobjDescP,
2185     DataBlock        *BSAdataBlockP
2186 )
2187 {
2188    dsInt16_t          rc = 0;
2189    BSA_Int16          bsaRC = 0;
2190    dsUint32_t         dsmHandle;
2191    regFSData          regFilespace ;
2192    dsmObjName         objName ;
2193    ObjAttr            objAttrArea;
2194    sndArchiveData     archData;
2195    DataBlk            dataBlkArea;
2196    dsmSendType        sendType;
2197    dsUint8_t          dsmObjType;
2198    XAPIObjInfo        xapiObjInfo;
2199    mcBindKey          mcBindKey;
2200    BSAObjectOwner     upperNode;  /* upper cased node name */
2201    char               errPrefix[DSM_MAX_RC_MSG_LENGTH + 1];
2202    char               ourMessage[DSM_MAX_RC_MSG_LENGTH + 1];
2203
2204    if(!dsm_init)
2205    {
2206 #ifdef DEBUG_BUTC
2207         printf("TSM library not mounted. \n");
2208 #endif
2209         if (dsm_MountLibrary())
2210         {
2211                 printf("TSM Library initialisation failed. \n");
2212                 return 1 ;
2213         }
2214 #ifdef DEBUG_BUTC
2215         printf("TSM Library initialisation SUCCESS. \n");
2216 #endif
2217    }
2218
2219    memset(errPrefix,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
2220    memset(ourMessage,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
2221
2222    dsmHandle = BSAHandle;
2223
2224    if (BSAobjDescP != NULL && BSAdataBlockP != NULL)
2225    {
2226    sprintf(traceStr2, "BSACreateObject ENTRY: BSAHandle:%ld ObjOwner:'%s' appOwner:'%s' \n ObjName:'%.*s%.*s' \n objType:%d  size:'%lu %lu' resourceType:'%s'  \n bufferLen:%d numBytes:%d ",
2227
2228            BSAHandle,
2229            BSAobjDescP->Owner.bsaObjectOwner[0] != '\0' ? BSAobjDescP->Owner.bsaObjectOwner : "",
2230            BSAobjDescP->Owner.appObjectOwner[0] != '\0' ?  BSAobjDescP->Owner.appObjectOwner : "",
2231            100,BSAobjDescP->objName.objectSpaceName[0] != '\0' ? BSAobjDescP->objName.objectSpaceName
2232            : "",
2233            100,BSAobjDescP->objName.pathName[0] != '\0' ? BSAobjDescP->objName.pathName : "",
2234            BSAobjDescP->objectType,
2235            BSAobjDescP->size.left,
2236            BSAobjDescP->size.right,
2237            BSAobjDescP->resourceType[0] != '\0' ?  BSAobjDescP->resourceType : "" ,
2238            BSAdataBlockP->bufferLen,
2239            BSAdataBlockP->numBytes);
2240   }
2241   else
2242   {
2243     if (BSAobjDescP == NULL)
2244       {
2245          bsaRC = BSA_RC_NULL_POINTER;
2246          XOPENRETURN(BSAHandle, "BSACreateObject",
2247                   bsaRC,__FILE__,__LINE__);
2248       }
2249     sprintf(traceStr2, "BSACreateObject ENTRY: BSAHandle:%ld", BSAHandle);
2250   }
2251
2252   ourTrace(BSAHandle, TrFL, traceStr2);
2253
2254    xopenGbl.oper = OPER_SEND_START;    /* save state for EndData later */
2255    /*=================================================================
2256    if (BSAobjDescP->version != ObjectDescriptorVersion)
2257        BSA spec doesn't require this currently..
2258    =================================================================*/
2259
2260    /*===============================================================
2261     if the node name is different - we won't create it compare both
2262     the value passed on the BSAInit and the final value used for the
2263     session (which may be different for Generate)
2264     ===============================================================*/
2265    strcpy(upperNode, BSAobjDescP->Owner.bsaObjectOwner);
2266    StrUpper(upperNode);
2267    if ((strcmp(upperNode, xopenGbl.dsmSessInfo.id)) &&
2268       (strcmp(BSAobjDescP->Owner.bsaObjectOwner, xopenGbl.bsaObjectOwner)))
2269    {
2270       sprintf(traceStr2,
2271              "BSACreateObject: BSAObjectOwner(%s) doesn't match value for session(%s).",
2272               upperNode, xopenGbl.dsmSessInfo.id);
2273       ourTrace(BSAHandle,TrFL, traceStr2);
2274       bsaRC = ADSM_RC_INVALID_NODE;
2275       strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
2276       ourLogEvent_Ex(BSAHandle,logLocal, ourMessage, errPrefix, logSevError);
2277       XOPENRETURN(BSAHandle, "BSACreateObject",
2278                   bsaRC,__FILE__,__LINE__);
2279    }
2280
2281    /*=== check string lengths - if this too long, it won't fit in our objInfo ===*/
2282    if (strlen(BSAobjDescP->desc) > ADSM_MAX_DESC)
2283    {
2284       sprintf(traceStr2,"BSACreateObject: description longer than TSM max (%" AFS_SIZET_FMT "). ",
2285               strlen(BSAobjDescP->desc));
2286       ourTrace(BSAHandle, TrFL, traceStr2);
2287       bsaRC = BSA_RC_DESC_TOO_LONG;
2288       XOPENRETURN(BSAHandle, "BSACreateObject",
2289                   bsaRC,__FILE__,__LINE__);
2290    }
2291    if (strlen(BSAobjDescP->objectInfo) > ADSM_MAX_OBJINFO)
2292    {
2293       sprintf(traceStr2,"BSACreateObject: objInfo longer than TSM max (%" AFS_SIZET_FMT ").",
2294       strlen(BSAobjDescP->objectInfo));
2295       ourTrace(BSAHandle,TrFL, traceStr2);
2296       bsaRC = BSA_RC_OBJINFO_TOO_LONG;
2297       XOPENRETURN(BSAHandle, "BSACreateObject",
2298                   bsaRC,__FILE__,__LINE__);
2299    }
2300
2301    if (!(xopenGbl.sessFlags & FL_IN_BSA_TXN))
2302    {
2303       sprintf(traceStr2, "BSACreateObject issued without BSABeginTxn");
2304       ourTrace(BSAHandle,TrFL, traceStr2);
2305       bsaRC = BSA_RC_BAD_CALL_SEQUENCE;
2306       XOPENRETURN(BSAHandle, "BSACreateObject",
2307                   bsaRC,__FILE__,__LINE__);
2308    }
2309
2310    if (strlen(BSAobjDescP->objName.objectSpaceName) > BSA_MAX_OSNAME)
2311    {
2312       sprintf(traceStr2, "BSACreateObject: objectSpaceName too long (%" AFS_SIZET_FMT ")",
2313                        strlen(BSAobjDescP->objName.objectSpaceName));
2314       ourTrace(BSAHandle, TrFL, traceStr2);
2315       bsaRC = BSA_RC_OBJNAME_TOO_LONG;
2316       XOPENRETURN(BSAHandle, "BSACreateObject:",
2317                   bsaRC,__FILE__,__LINE__);
2318    }
2319
2320    if (!(xopenGbl.sessFlags & FL_IN_DSM_TXN))        /* first CreateObj */
2321    { /* can't issue RegisterFS if already in Txn */
2322       /*=== Register the file space ===*/
2323       regFilespace.stVersion = regFSDataVersion ;
2324       regFilespace.fsName = BSAobjDescP->objName.objectSpaceName ;
2325
2326       /*===  use resource type for fsType (as it was intended)  ===*/
2327
2328       regFilespace.fsType = BSAobjDescP->resourceType ;
2329       regFilespace.capacity.lo = 0;
2330       regFilespace.capacity.hi = 0;
2331       regFilespace.occupancy.lo = 0;
2332       regFilespace.occupancy.hi = 0;
2333       #if _OPSYS_TYPE == DS_AIX
2334          regFilespace.fsAttr.unixFSAttr.fsInfoLength = strlen(XAPI_FSINFO) ;
2335          strcpy(regFilespace.fsAttr.unixFSAttr.fsInfo, XAPI_FSINFO);
2336       #else
2337          regFilespace.fsAttr.dosFSAttr.fsInfoLength = strlen(XAPI_FSINFO) ;
2338          strcpy(regFilespace.fsAttr.dosFSAttr.fsInfo, XAPI_FSINFO);
2339          regFilespace.fsAttr.dosFSAttr.driveLetter = 'X';
2340       #endif
2341       rc = AFSdsmRegisterFS(dsmHandle, &regFilespace) ;
2342       if ((rc != 0) && (rc != DSM_RC_FS_ALREADY_REGED))
2343       {
2344          sprintf(traceStr2, "BSACreateObject(AFSdsmRegisterFS) error rc = %d",rc);
2345          ourTrace(BSAHandle,TrFL, traceStr2);
2346
2347          xlateRC(BSAHandle, rc, &bsaRC);
2348          XOPENRETURN(BSAHandle, "BSACreateObject(AFSdsmRegisterFS)",
2349                      bsaRC,__FILE__,__LINE__);
2350       }
2351    }
2352
2353    /*========================================================
2354     Check for invalid copyType before sending data. Log error
2355     to dsirror.log file.
2356     ========================================================*/
2357     if (BSAobjDescP->copyType == BSACopyType_ARCHIVE)
2358        sendType = stArchiveMountWait;
2359     else
2360        if (BSAobjDescP->copyType == BSACopyType_BACKUP)
2361           sendType = stBackupMountWait;
2362        else
2363        {
2364            sprintf(traceStr2,
2365                   "BSACreateObject: invalid copyType (%d)",
2366                    BSAobjDescP->copyType);
2367            ourTrace(BSAHandle,TrFL, traceStr2);
2368            bsaRC = ADSM_RC_INVALID_COPYTYPE;
2369            strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
2370            ourLogEvent_Ex(BSAHandle,logLocal, ourMessage, errPrefix, logSevError);
2371            XOPENRETURN(BSAHandle, "BSACreateObject",
2372                        bsaRC,__FILE__,__LINE__);
2373        }
2374
2375    if ((BSAobjDescP->objectType == BSAObjectType_FILE) ||
2376       (BSAobjDescP->objectType == BSAObjectType_DATABASE) ||
2377       (BSAobjDescP->objectType == BSAObjectType_ANY))
2378
2379          dsmObjType = DSM_OBJ_FILE;
2380    else
2381       if (BSAobjDescP->objectType == BSAObjectType_DIRECTORY)
2382          dsmObjType = DSM_OBJ_DIRECTORY;
2383       else
2384       {
2385          sprintf(traceStr2,
2386                "BSACreateObject: invalid objectType (%d)",
2387                 BSAobjDescP->objectType);
2388          ourTrace(BSAHandle,TrFL, traceStr2);
2389          bsaRC = ADSM_RC_INVALID_OBJTYPE;
2390          strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
2391          ourLogEvent_Ex(BSAHandle,logLocal, ourMessage, errPrefix, logSevError);
2392          XOPENRETURN(BSAHandle, "BSACreateObject",
2393                     bsaRC,__FILE__,__LINE__);
2394       }
2395
2396    /*==================================================================
2397     put in a check here - count the number of objects per txn
2398     and compare with xopenGbl.sessInfo.maxObjPerTxn
2399     If reach the limit, EndTxn and start a new one
2400     OK to do this without telling the BSA caller?
2401     Or should we exit with an error to tell them the limit is reached ?
2402     ==================================================================*/
2403
2404     if (!(xopenGbl.sessFlags & FL_IN_DSM_TXN))
2405     {
2406       rc = AFSdsmBeginTxn(dsmHandle);
2407
2408       if (rc)
2409       {
2410          sprintf(traceStr2, "BSACreateObject(AFSdsmBeginTxn) error rc = %d", rc);
2411          ourTrace(BSAHandle,TrFL, traceStr2);
2412          xlateRC(BSAHandle, rc, &bsaRC);
2413          XOPENRETURN(BSAHandle, "BSACreateObject(AFSdsmBeginTxn)",
2414                      bsaRC,__FILE__,__LINE__);
2415       }
2416       xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_IN_DSM_TXN); /* set on */
2417     }
2418
2419     /*=== Backup the data  ===*/
2420
2421    /*================================================================
2422      the entire pathname gets copied into hl during parsing, so
2423      check for that max len as well. For now these are the same value.
2424      =================================================================*/
2425    if (strlen(BSAobjDescP->objName.pathName) >
2426             min(DSM_MAX_HL_LENGTH, BSA_MAX_PATHNAME))
2427    {
2428       sprintf(traceStr2, "BSACreateObject: pathName too long (%" AFS_SIZET_FMT ")",
2429               strlen(BSAobjDescP->objName.pathName));
2430       ourTrace(BSAHandle,TrFL, traceStr2);
2431       bsaRC = BSA_RC_OBJNAME_TOO_LONG;
2432       XOPENRETURN(BSAHandle, "BSACreateObject",
2433                   bsaRC,__FILE__,__LINE__);
2434    }
2435
2436    strcpy(objName.fs,BSAobjDescP->objName.objectSpaceName) ;
2437    /*=== previous code to use only ll field ===*/
2438    /*objName.hl[0] = '\0';
2439      strcpy(objName.ll,BSAobjDescP->objName.pathName) ;
2440    */
2441    xparsePath(BSAHandle, BSAobjDescP->objName.pathName, objName.hl, objName.ll);
2442    objName.objType = dsmObjType ;
2443
2444    objAttrArea.stVersion = ObjAttrVersion ;
2445    strcpy(objAttrArea.owner,BSAobjDescP->Owner.appObjectOwner);
2446    objAttrArea.sizeEstimate.hi = BSAobjDescP->size.left;
2447    objAttrArea.sizeEstimate.lo = BSAobjDescP->size.right;
2448    objAttrArea.objCompressed = bFalse ;  /* let COMPRESSION option decide */
2449    /*=== whether there's actually compression ===*/
2450    objAttrArea.objInfoLength = sizeof(XAPIObjInfo);
2451    objAttrArea.objInfo = (char *)&xapiObjInfo ;
2452
2453    memset(&xapiObjInfo,0x00,sizeof(XAPIObjInfo));
2454    strcpy(xapiObjInfo.resourceType, BSAobjDescP->resourceType);
2455    xapiObjInfo.size.left = BSAobjDescP->size.left;
2456    xapiObjInfo.size.right = BSAobjDescP->size.right;
2457    strcpy(xapiObjInfo.partDesc, BSAobjDescP->desc);
2458    strcpy(xapiObjInfo.partObjInfo, BSAobjDescP->objectInfo);
2459
2460    /*=== check if a lifecycle group name was passed to us ===*/
2461    if (strlen(BSAobjDescP->lGName))
2462       objAttrArea.mcNameP = (char *)BSAobjDescP->lGName ;
2463    else
2464       objAttrArea.mcNameP = NULL;
2465
2466       dataBlkArea.stVersion = DataBlkVersion ;
2467       if (BSAdataBlockP == NULL)
2468       {
2469          dataBlkArea.bufferPtr = NULL;
2470          dataBlkArea.bufferLen = 0;
2471       }
2472       else
2473       {
2474          dataBlkArea.bufferPtr = BSAdataBlockP->bufferPtr ;
2475          dataBlkArea.bufferLen = BSAdataBlockP->bufferLen;
2476       }
2477
2478       /*=======================================================
2479        always issue BindMC because we don't expect applications
2480        to call ResolveLifecycleGroup since it isn't in the
2481        Data Movement subset
2482        =======================================================*/
2483        mcBindKey.stVersion = mcBindKeyVersion ;
2484        rc = AFSdsmBindMC(dsmHandle, &objName, sendType, &mcBindKey);
2485        if (rc)
2486        {
2487           sprintf(traceStr2, "BSACreateObject(AFSdsmBindMC): error rc = %d", rc);
2488           ourTrace(BSAHandle, TrFL, traceStr2);
2489           xlateRC(BSAHandle, rc, &bsaRC);
2490           XOPENRETURN(BSAHandle, "BSACreateObject(dsnBindMC)",
2491                       bsaRC,__FILE__,__LINE__);
2492        }
2493
2494        /*=== now update state since we'll issue the base Send call ===*/
2495
2496        xopenGbl.oper = OPER_SEND_ISSUED;    /* save state for EndData later */
2497
2498        switch (sendType)
2499        {
2500           case (stBackupMountWait) :
2501           rc = AFSdsmSendObj(dsmHandle,
2502                           sendType,
2503                           NULL,
2504                           &objName,
2505                           &objAttrArea,
2506                           &dataBlkArea);
2507           break;
2508
2509           case (stArchiveMountWait) :
2510           archData.stVersion = sndArchiveDataVersion;
2511           archData.descr = (char *)(BSAobjDescP->desc);
2512           rc = AFSdsmSendObj(dsmHandle,
2513                           sendType,
2514                           &archData,
2515                           &objName,
2516                           &objAttrArea,
2517                           &dataBlkArea);
2518           break;
2519           default : ;
2520        }
2521
2522        if (rc != DSM_RC_OK)
2523        {
2524           sprintf(traceStr2, "BSACreateObject(AFSdsmSendObj) error rc = %d", rc);
2525           ourTrace(BSAHandle,TrFL, traceStr2);
2526
2527           if (rc == DSM_RC_WILL_ABORT)  /* save flag */
2528              xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_RC_WILL_ABORT);
2529
2530           xlateRC(BSAHandle, rc, &bsaRC);
2531           XOPENRETURN(BSAHandle, "BSACreateObject(AFSdsmSendObj)",
2532                       bsaRC,__FILE__,__LINE__);
2533        }
2534    XOPENRETURN(BSAHandle, "BSACreateObject",
2535                BSA_RC_SUCCESS, __FILE__,__LINE__);
2536 }
2537
2538
2539 BSA_Int16 BSADeleteObject(
2540     long           BSAHandle,
2541     CopyType       copyType,
2542     ObjectName     *BSAobjNameP,
2543     CopyId         *copyidP
2544 )
2545 {
2546    dsInt16_t      rc = 0;
2547    BSA_Int16      bsaRC = 0;
2548    dsUint32_t     dsmHandle;
2549    dsUint16_t     reason ;                   /* for AFSdsmEndTxn */
2550    dsmObjName     dsmobjName;
2551
2552    dsmDelType delType;
2553    dsmDelInfo delInfo;
2554
2555    delList *llHeadP = NULL;
2556    delList *llTailP = NULL;
2557    delList *ll = NULL;
2558
2559    char errPrefix[DSM_MAX_RC_MSG_LENGTH + 1];
2560    char ourMessage[DSM_MAX_RC_MSG_LENGTH + 1];
2561
2562    if(!dsm_init)
2563    {
2564 #ifdef DEBUG_BUTC
2565         printf("TSM library not mounted. \n");
2566 #endif
2567         if (dsm_MountLibrary())
2568         {
2569                 printf("TSM Library initialisation failed. \n");
2570                 return 1 ;
2571         }
2572 #ifdef DEBUG_BUTC
2573         printf("TSM Library initialisation SUCCESS. \n");
2574 #endif
2575    }
2576
2577    memset(errPrefix,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
2578    memset(ourMessage,     '\0', DSM_MAX_RC_MSG_LENGTH + 1);
2579
2580    dsmHandle = BSAHandle;
2581
2582    sprintf(traceStr2, "BSADeleteObject ENTRY: BSAHandle:%ld CopyType:%d \n ObjName:'%.*s%.*s' copyidP:'%lu %lu'.",
2583            BSAHandle,
2584            copyType,
2585            100,BSAobjNameP->objectSpaceName,
2586            100,BSAobjNameP->pathName,
2587            copyidP->left,
2588            copyidP->right);
2589    ourTrace(BSAHandle, TrFL, traceStr2);
2590    if (copyType != BSACopyType_ARCHIVE)
2591    {
2592       sprintf(traceStr2,
2593               "BSADeleteObject: invalid copyType %d",
2594                copyType);
2595       ourTrace(BSAHandle,TrFL, traceStr2);
2596       bsaRC = ADSM_RC_INVALID_COPYTYPE;
2597       strcpy(ourMessage, ourRCMsg(bsaRC, errPrefix));
2598       ourLogEvent_Ex(BSAHandle, logLocal, ourMessage, errPrefix, logSevError);
2599       XOPENRETURN(BSAHandle, "BSADeleteObject",
2600                   bsaRC,__FILE__,__LINE__);
2601    }
2602
2603    if (!(xopenGbl.sessFlags & FL_IN_BSA_TXN))
2604    {
2605       sprintf(traceStr2, "BSADeleteObject issued without BSABeginTxn");
2606       ourTrace(BSAHandle, TrFL, traceStr2);
2607
2608       bsaRC = BSA_RC_BAD_CALL_SEQUENCE;
2609       XOPENRETURN(BSAHandle, "BSADeleteObject:",
2610                   bsaRC,__FILE__,__LINE__);
2611    }
2612
2613    strcpy(dsmobjName.fs, BSAobjNameP->objectSpaceName);
2614    xparsePath(BSAHandle, BSAobjNameP->pathName, dsmobjName.hl, dsmobjName.ll);
2615    dsmobjName.objType = DSM_OBJ_FILE;
2616
2617    if (!copyidP)    /* NULL, so query and delete all with same name */
2618    {
2619       if (xopenGbl.sessFlags & FL_IN_DSM_TXN)
2620       /*=== if a trxn had been started, end it before doing Query ===*/
2621       {
2622          rc = AFSdsmEndTxn(dsmHandle, DSM_VOTE_COMMIT, &reason);
2623          xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_DSM_TXN); /* set off */
2624       }
2625       rc = buildList(dsmHandle, &dsmobjName, &llHeadP, &llTailP);
2626       if (rc)
2627       {
2628          bsaRC = BSA_RC_OBJECT_NOT_FOUND;
2629          XOPENRETURN(BSAHandle, "BSADeleteObject(buildList)",
2630                      bsaRC,__FILE__,__LINE__);
2631       }
2632    }
2633
2634    if (!(xopenGbl.sessFlags & FL_IN_DSM_TXN))        /* first call */
2635    {
2636       rc = AFSdsmBeginTxn(dsmHandle);
2637       if (rc)
2638       {
2639          xlateRC(BSAHandle, rc, &bsaRC);
2640          XOPENRETURN(dsmHandle,"BSADeleteObject(AFSdsmBeginTxn)",
2641                      bsaRC,__FILE__,__LINE__);
2642       }
2643       xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_IN_DSM_TXN); /* set on */
2644    }
2645
2646    delType = dtArchive;
2647    delInfo.archInfo.stVersion = delArchVersion;
2648
2649    if (copyidP)   /* single ID to delete */
2650    {
2651       delInfo.archInfo.objId.hi  = copyidP->left;
2652       delInfo.archInfo.objId.lo  = copyidP->right;
2653
2654       if ((rc = AFSdsmDeleteObj(dsmHandle,delType,delInfo)) != DSM_RC_OK)
2655       {
2656          sprintf(traceStr2, "BSADeleteObject: AFSdsmDeleteObj error rc = %d", rc);
2657          ourTrace(dsmHandle,TrFL, traceStr2);
2658       }
2659    }
2660    else   /* multiple IDs to delete */
2661    {
2662       ll = llHeadP;
2663       while (ll)
2664       {
2665          delInfo.archInfo.objId.hi  = ll->objId.hi;
2666          delInfo.archInfo.objId.lo  = ll->objId.lo;
2667          if ((rc = AFSdsmDeleteObj(dsmHandle, delType, delInfo)) != DSM_RC_OK)
2668          {
2669             sprintf(traceStr2, "BSADeleteObject: AFSdsmDeleteObj error rc = %d", rc);
2670             ourTrace(dsmHandle, TrFL, traceStr2);
2671             /*=== break and get out of loop, or keep going? ===*/
2672          }
2673          /*=== incr to next list entry ===*/
2674          ll = ll->next;
2675       }
2676       /*=== free list now that done ===*/
2677       rc =  freeList(&llHeadP, &llTailP);
2678    }
2679
2680    xlateRC(BSAHandle, rc, &bsaRC);
2681    XOPENRETURN(BSAHandle,"BSADeleteObject",
2682                bsaRC,__FILE__,__LINE__)
2683 }
2684
2685 BSA_Int16 BSAMarkObjectInactive(
2686     long            BSAHandle,
2687     ObjectName     *BSAobjNameP
2688 )
2689 {
2690    dsInt16_t      rc = 0;
2691    BSA_Int16      bsaRC = 0;
2692    dsUint32_t     dsmHandle;
2693
2694    dsmObjName           dsmobjName;
2695
2696    qryBackupData        queryBuffer;       /* for query Backup */
2697    qryRespBackupData    qbDataArea;
2698    DataBlk              qDataBlkArea;
2699
2700    dsmDelType           delType;
2701    dsmDelInfo           delInfo;
2702
2703    dsUint16_t           reason ;                   /* for AFSdsmEndTxn */
2704    /*=== build list of all objTypes we find for this name ===*/
2705    dsInt16_t            i;
2706    dsInt16_t            numTypes;
2707    dsUint8_t            listObjType[5];    /* only 2 objTypes defined today */
2708    dsUint32_t           listCopyGroup[5];
2709
2710    if(!dsm_init)
2711    {
2712 #ifdef DEBUG_BUTC
2713         printf("TSM library not mounted. \n");
2714 #endif
2715         if (dsm_MountLibrary())
2716         {
2717                 printf("TSM Library initialisation failed. \n");
2718                 return 1 ;
2719         }
2720 #ifdef DEBUG_BUTC
2721         printf("TSM Library initialisation SUCCESS. \n");
2722 #endif
2723    }
2724
2725    dsmHandle = BSAHandle;
2726    memset(&delInfo, 0x00, sizeof(dsmDelInfo));
2727    memset(&queryBuffer, 0x00, sizeof(qryBackupData));
2728
2729    sprintf(traceStr2, "BSAMarkObjectInactive ENTRY: BSAHandle:%ld \n ObjName:'%.*s%.*s'.",
2730            BSAHandle,
2731            100, BSAobjNameP->objectSpaceName,
2732            100, BSAobjNameP->pathName);
2733    ourTrace(dsmHandle, TrFL, traceStr2);
2734
2735    if (!(xopenGbl.sessFlags & FL_IN_BSA_TXN))
2736    {
2737       sprintf(traceStr2, "BSAMarkObjectInactive: issued without BSABeginTxn.");
2738       ourTrace(BSAHandle, TrFL, traceStr2);
2739       bsaRC = BSA_RC_BAD_CALL_SEQUENCE;
2740       XOPENRETURN(BSAHandle, "BSAMarkObjectInactive",
2741                   bsaRC,__FILE__,__LINE__);
2742    }
2743
2744    if (strlen(BSAobjNameP->objectSpaceName) > DSM_MAX_FSNAME_LENGTH)
2745    {
2746       sprintf(traceStr2, "BSAMarkObjectInactive: objectSpaceName too long (%" AFS_SIZET_FMT ")", strlen(BSAobjNameP->objectSpaceName));
2747       ourTrace(BSAHandle,TrFL, traceStr2);
2748
2749       bsaRC = BSA_RC_OBJNAME_TOO_LONG;
2750       XOPENRETURN(BSAHandle, "BSAMarkObjectInactive",
2751                   bsaRC,__FILE__,__LINE__);
2752    }
2753    /*===============================================================
2754     The entire pathname gets copied into hl during parsing, so
2755     check for that max len as well. For now these are the same value.
2756     =============================================================== */
2757    if (strlen(BSAobjNameP->pathName) >
2758        min(DSM_MAX_HL_LENGTH, BSA_MAX_PATHNAME))
2759    {
2760       sprintf(traceStr2, "BSAMarkObjectInactive: pathName too long (%" AFS_SIZET_FMT ")",
2761                          strlen(BSAobjNameP->pathName));
2762       ourTrace(BSAHandle,TrFL, traceStr2);
2763       bsaRC = BSA_RC_OBJNAME_TOO_LONG;
2764       XOPENRETURN(BSAHandle, "BSAMarkObjectInactive",
2765                   bsaRC,__FILE__,__LINE__);
2766    }
2767
2768    /*==============================================================
2769     we don't allow any wildcard in name because that could retrieve
2770     LOTS of objects and make list processing more complicated.
2771     XBSA spec implies a single object.
2772     ==============================================================*/
2773    if ( strchr(BSAobjNameP->objectSpaceName, '*') ||
2774         strchr(BSAobjNameP->objectSpaceName, '?') ||
2775         strchr(BSAobjNameP->pathName,        '*') ||
2776         strchr(BSAobjNameP->pathName,        '?'))
2777    {
2778
2779       sprintf(traceStr2, "BSAMarkObjectInactive: objName contains a wildcard.")
2780 ;
2781       ourTrace(BSAHandle, TrFL, traceStr2);
2782       /*=== could have a more specific rc, use this for now ===*/
2783       bsaRC = BSA_RC_ABORT_ACTIVE_NOT_FOUND;
2784       XOPENRETURN(BSAHandle, "BSAMarkObjectInactive",
2785                   bsaRC,__FILE__,__LINE__);
2786    }
2787
2788    strcpy(dsmobjName.fs, BSAobjNameP->objectSpaceName);
2789    xparsePath(BSAHandle, BSAobjNameP->pathName, dsmobjName.hl, dsmobjName.ll);
2790    dsmobjName.objType = DSM_OBJ_ANY_TYPE;
2791
2792    /*============================================================
2793     A Backup Delete must include the copy Group and objType this
2794     wasn't passed in, so we have to do a query.
2795     ============================================================*/
2796
2797    queryBuffer.stVersion = qryBackupDataVersion ;
2798    queryBuffer.objName = &dsmobjName;
2799    queryBuffer.owner   = xopenGbl.dsmSessInfo.owner;
2800    queryBuffer.objState = DSM_ACTIVE;              /* only get active one */
2801
2802    if ((rc=AFSdsmBeginQuery(dsmHandle, qtBackup,
2803                         (void *)&queryBuffer )) != DSM_RC_OK)
2804    {
2805       sprintf(traceStr2, "BSAMarkObjectInactive: Call to AFSdsmBeginQuery for Backup error rc = %d", rc);
2806       ourTrace(BSAHandle,TrFL, traceStr2);
2807       xlateRC(BSAHandle, rc, &bsaRC);
2808       if ((rc == DSM_RC_ABORT_NO_MATCH) ||    /* special rc for MarkInact */
2809           (rc == DSM_RC_FILE_SPACE_NOT_FOUND))
2810          bsaRC = BSA_RC_ABORT_ACTIVE_NOT_FOUND;
2811
2812       rc = AFSdsmEndQuery(dsmHandle);
2813       XOPENRETURN(BSAHandle, "BSAMarkObjectInactive: AFSdsmBeginQuery",
2814                   bsaRC,__FILE__,__LINE__);
2815    }
2816
2817    qbDataArea.stVersion   = qryRespBackupDataVersion;
2818    qDataBlkArea.stVersion = DataBlkVersion ;
2819    qDataBlkArea.bufferPtr = (char *)&qbDataArea;
2820    qDataBlkArea.bufferLen = sizeof(qryRespBackupData);
2821
2822    numTypes = 0;
2823    rc = AFSdsmGetNextQObj(dsmHandle, &qDataBlkArea);
2824    while (((rc == DSM_RC_MORE_DATA) ||
2825          (rc == DSM_RC_FINISHED)) &&
2826           qDataBlkArea.numBytes)
2827    {  /* save copy Group we got */
2828       listCopyGroup[numTypes] = qbDataArea.copyGroup;
2829       listObjType[numTypes]   = qbDataArea.objName.objType;
2830       numTypes++;
2831       rc = AFSdsmGetNextQObj(dsmHandle, &qDataBlkArea);
2832    }
2833
2834    if (rc != DSM_RC_FINISHED)
2835    {
2836       xlateRC(BSAHandle, rc, &bsaRC);
2837       if ((rc == DSM_RC_ABORT_NO_MATCH) || /* special rc for MarkInact */
2838           (rc == DSM_RC_FILE_SPACE_NOT_FOUND))
2839          bsaRC = BSA_RC_ABORT_ACTIVE_NOT_FOUND;
2840       rc = AFSdsmEndQuery(dsmHandle);
2841       XOPENRETURN(BSAHandle,"BSAMarkObjectInactive: AFSdsmGetNextQObj",
2842                   bsaRC,__FILE__,__LINE__);
2843    }
2844    rc = AFSdsmEndQuery(dsmHandle);
2845
2846    /*=== now we can do the delete ===*/
2847    rc = AFSdsmBeginTxn(dsmHandle);
2848
2849    if (rc)
2850    {
2851      xlateRC(BSAHandle, rc, &bsaRC);
2852      XOPENRETURN(BSAHandle,"BSAMarkObjectInactive: AFSdsmBeginTxn",
2853                  bsaRC,__FILE__,__LINE__);
2854    }
2855    xopenGbl.sessFlags = (xopenGbl.sessFlags | FL_IN_DSM_TXN); /* set on */
2856
2857    delType = dtBackup;     /* this only applies to Backup */
2858
2859    delInfo.backInfo.stVersion = delBackVersion;
2860    delInfo.backInfo.objNameP = &dsmobjName;
2861
2862    for (i=0; i<numTypes; i++)
2863    {
2864       delInfo.backInfo.copyGroup = listCopyGroup[i];
2865       dsmobjName.objType         = listObjType[i];
2866
2867       if ((rc = AFSdsmDeleteObj(dsmHandle, delType, delInfo)) != DSM_RC_OK)
2868       {
2869
2870          sprintf(traceStr2, "BSAMarkObjectInactive: call to AFSdsmDeleteObj error rc = %d", rc);
2871          ourTrace(BSAHandle, TrFL, traceStr2);
2872       }
2873    }
2874
2875    /*=== issue EndTxn since these can't be nested because of Query sequence ===*/
2876    AFSdsmEndTxn(dsmHandle, DSM_VOTE_COMMIT, &reason);
2877    xopenGbl.sessFlags = (xopenGbl.sessFlags ^ FL_IN_DSM_TXN); /* set off */
2878
2879    xlateRC(BSAHandle, rc, &bsaRC);
2880    XOPENRETURN(BSAHandle,"BSAMarkObjectInactive",
2881                bsaRC,__FILE__,__LINE__);
2882 }
2883
2884 #endif /*xbsa*/