-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathIBMiContent.ts
942 lines (825 loc) Β· 33.6 KB
/
IBMiContent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
import { parse } from 'csv-parse/sync';
import fs from 'fs';
import path from 'path';
import tmp from 'tmp';
import util from 'util';
import { window } from 'vscode';
import { ObjectTypes } from '../filesystems/qsys/Objects';
import { CommandResult, IBMiError, IBMiMember, IBMiObject, IFSFile, QsysPath } from '../typings';
import { ConnectionConfiguration } from './Configuration';
import { FilterType, parseFilter, singleGenericName } from './Filter';
import { default as IBMi } from './IBMi';
import { Tools } from './Tools';
const tmpFile = util.promisify(tmp.file);
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);
const UTF8_CCSIDS = [`819`, `1208`, `1252`];
type Authority = "*ADD" | "*DLT" | "*EXECUTE" | "*READ" | "*UPD" | "*NONE" | "*ALL" | "*CHANGE" | "*USE" | "*EXCLUDE" | "*AUTLMGT";
export type SortOrder = `name` | `type`;
export type SortOptions = {
order: "name" | "date"
ascending?: boolean
}
export default class IBMiContent {
private chgJobCCSID: string | undefined = undefined;
constructor(readonly ibmi: IBMi) { }
private get config(): ConnectionConfiguration.Parameters {
if (!this.ibmi.config) {
throw new Error("Please connect to an IBM i");
}
else {
return this.ibmi.config;
}
}
private getTempRemote(path: string) {
const tempRemote = this.ibmi.getTempRemote(path);
if (!tempRemote) {
throw new Error(`Could not compute temporary remote location for ${path}`);
}
return tempRemote;
}
private async getNotUTF8CCSID(attr: string, remotePath: string): Promise<string> {
const result = await this.ibmi.sendCommand({ command: `${attr} "${remotePath}" CCSID` });
if (result.code === 0) {
//What's the point of converting 1208?
let ccsid = result.stdout.trim();
if (!UTF8_CCSIDS.includes(ccsid)) {
return ccsid.padStart(3, `0`);
}
}
return "";
}
private async convertToUTF8(iconv: string, from: string, to: string, ccsid: string) {
const result = await this.ibmi.sendCommand({ command: `${iconv} -f IBM-${ccsid} -t UTF-8 "${from}" > ${to}` });
if (result.code === 0) {
return result.stdout;
}
else {
throw new Error(`Failed to convert ${from} to UTF-8: ${result.stderr}`);
}
}
/**
*
* @param remotePath Remote IFS path
* @param localPath Local path to download file to
*/
async downloadStreamfileRaw(remotePath: string, localPath?: string) {
const features = this.ibmi.remoteFeatures;
if (this.config.autoConvertIFSccsid && features.attr && features.iconv) {
// If it's not 1208, generate a temp file with the converted content
const ccsid = await this.getNotUTF8CCSID(features.attr, remotePath);
if (ccsid) {
const newTempFile = this.getTempRemote(remotePath);
await this.convertToUTF8(features.iconv, remotePath, newTempFile, ccsid);
remotePath = newTempFile;
}
}
if (!localPath) {
localPath = await tmpFile();
}
await this.ibmi.downloadFile(localPath, remotePath);
const raw = await readFileAsync(localPath);
return raw;
}
/**
* @deprecated Use downloadStreamfileRaw instead
*/
async downloadStreamfile(remotePath: string, localPath?: string) {
const raw = await this.downloadStreamfileRaw(remotePath, localPath);
return raw.toString(`utf8`);
}
/**
* @param originalPath
* @param content Raw content
* @param encoding Optional encoding to write.
*/
async writeStreamfileRaw(originalPath: string, content: Uint8Array, encoding?: string) {
const client = this.ibmi.client;
const features = this.ibmi.remoteFeatures;
const tmpobj = await tmpFile();
let ccsid;
if (this.config.autoConvertIFSccsid && features.attr) {
// First, find the CCSID of the original file if not UTF-8
ccsid = await this.getNotUTF8CCSID(features.attr, originalPath);
}
await writeFileAsync(tmpobj, content, encoding);
if (ccsid && features.iconv) {
// Upload our file to the same temp file, then write convert it back to the original ccsid
const tempFile = this.getTempRemote(originalPath);
await client.putFile(tmpobj, tempFile); //TODO: replace with uploadFiles
return await this.convertToUTF8(features.iconv, tempFile, originalPath, ccsid);
} else {
return client.putFile(tmpobj, originalPath);
}
}
/**
* Write utf8 content to a streamfile
* @deprecated Use writeStreamfileRaw instead
*/
async writeStreamfile(originalPath: string, content: string) {
const buffer = Buffer.from(content, `utf8`);
return this.writeStreamfileRaw(originalPath, buffer);
}
/**
* Download the contents of a source member
*/
async downloadMemberContent(asp: string | undefined, library: string, sourceFile: string, member: string, localPath?: string) {
asp = asp || this.config.sourceASP;
library = this.ibmi.upperCaseName(library);
sourceFile = this.ibmi.upperCaseName(sourceFile);
member = this.ibmi.upperCaseName(member);
let retry = false;
let path = Tools.qualifyPath(library, sourceFile, member, asp);
const tempRmt = this.getTempRemote(path);
while (true) {
let copyResult: CommandResult;
if (this.ibmi.dangerousVariants && new RegExp(`[${this.ibmi.variantChars.local}]`).test(path)) {
copyResult = { code: 0, stdout: '', stderr: '' };
try {
await this.runSQL([
`@QSYS/CPYF FROMFILE(${library}/${sourceFile}) TOFILE(QTEMP/QTEMPSRC) FROMMBR(${member}) TOMBR(TEMPMEMBER) MBROPT(*REPLACE) CRTFILE(*YES);`,
`@QSYS/CPYTOSTMF FROMMBR('${Tools.qualifyPath("QTEMP", "QTEMPSRC", "TEMPMEMBER", undefined)}') TOSTMF('${tempRmt}') STMFOPT(*REPLACE) STMFCCSID(1208) DBFCCSID(${this.config.sourceFileCCSID});`
].join("\n"));
} catch (error: any) {
copyResult.code = -1;
copyResult.stderr = String(error);
}
}
else {
copyResult = await this.ibmi.runCommand({
command: `QSYS/CPYTOSTMF FROMMBR('${path}') TOSTMF('${tempRmt}') STMFOPT(*REPLACE) STMFCCSID(1208) DBFCCSID(${this.config.sourceFileCCSID})`,
noLibList: true
});
}
if (copyResult.code === 0) {
if (!localPath) {
localPath = await tmpFile();
}
await this.ibmi.downloadFile(localPath, tempRmt);
return await readFileAsync(localPath, `utf8`);
} else {
if (!retry) {
const messageID = String(copyResult.stdout).substring(0, 7);
switch (messageID) {
case "CPDA08A":
//We need to try again after we delete the temp remote
const result = await this.ibmi.sendCommand({ command: `rm -rf ${tempRmt}`, directory: `.` });
retry = !result.code || result.code === 0;
break;
case "CPFA0A9":
//The member may be located on SYSBAS
if (asp) {
path = Tools.qualifyPath(library, sourceFile, member);
retry = true;
}
break;
default:
retry = false;
break;
}
}
if (!retry) {
throw new Error(`Failed downloading member: ${copyResult.stderr}`);
}
}
}
}
/**
* Upload to a member
*/
async uploadMemberContent(asp: string | undefined, library: string, sourceFile: string, member: string, content: string | Uint8Array) {
asp = asp || this.config.sourceASP;
library = this.ibmi.upperCaseName(library);
sourceFile = this.ibmi.upperCaseName(sourceFile);
member = this.ibmi.upperCaseName(member);
const client = this.ibmi.client;
const tmpobj = await tmpFile();
let retry = false;
try {
await writeFileAsync(tmpobj, content, `utf8`);
let path = Tools.qualifyPath(library, sourceFile, member, asp);
const tempRmt = this.getTempRemote(path);
await client.putFile(tmpobj, tempRmt);
while (true) {
let copyResult: CommandResult;
if (this.ibmi.dangerousVariants && new RegExp(`[${this.ibmi.variantChars.local}]`).test(path)) {
copyResult = { code: 0, stdout: '', stderr: '' };
try {
await this.runSQL([
`@QSYS/CPYF FROMFILE(${library}/${sourceFile}) FROMMBR(${member}) TOFILE(QTEMP/QTEMPSRC) TOMBR(TEMPMEMBER) MBROPT(*REPLACE) CRTFILE(*YES);`,
`@QSYS/CPYFRMSTMF FROMSTMF('${tempRmt}') TOMBR('${Tools.qualifyPath("QTEMP", "QTEMPSRC", "TEMPMEMBER", undefined)}') MBROPT(*REPLACE) STMFCCSID(1208) DBFCCSID(${this.config.sourceFileCCSID})`,
`@QSYS/CPYF FROMFILE(QTEMP/QTEMPSRC) FROMMBR(TEMPMEMBER) TOFILE(${library}/${sourceFile}) TOMBR(${member}) MBROPT(*REPLACE);`
].join("\n"));
} catch (error: any) {
copyResult.code = -1;
copyResult.stderr = String(error);
}
}
else {
copyResult = await this.ibmi.runCommand({
command: `QSYS/CPYFRMSTMF FROMSTMF('${tempRmt}') TOMBR('${path}') MBROPT(*REPLACE) STMFCCSID(1208) DBFCCSID(${this.config.sourceFileCCSID})`,
noLibList: true
});
}
if (copyResult.code === 0) {
const messages = Tools.parseMessages(copyResult.stderr);
if (messages.findId("CPIA083")) {
window.showWarningMessage(`${library}/${sourceFile}(${member}) was saved with truncated records!`);
}
return true;
} else {
if (!retry) {
const messages = Tools.parseMessages(copyResult.stderr);
if (messages.findId("CPFA0A9")) {
//The member may be located on SYSBAS
if (asp) {
path = Tools.qualifyPath(library, sourceFile, member);
retry = true;
}
}
else {
throw new Error(`Failed uploading member: ${copyResult.stderr}`);
}
}
}
}
} catch (error) {
console.log(`Failed uploading member: ` + error);
return Promise.reject(error);
}
}
/**
* Run SQL statements.
* Each statement must be separated by a semi-colon and a new line (i.e. ;\n).
* If a statement starts with @, it will be run as a CL command.
*
* @param statements
* @returns a Result set
*/
async runSQL(statements: string): Promise<Tools.DB2Row[]> {
const { 'QZDFMDB2.PGM': QZDFMDB2 } = this.ibmi.remoteFeatures;
if (QZDFMDB2) {
if (this.chgJobCCSID === undefined) {
this.chgJobCCSID = (this.ibmi.qccsid < 1 || this.ibmi.qccsid === 65535) && this.ibmi.defaultCCSID > 0 ? `@CHGJOB CCSID(${this.ibmi.defaultCCSID});\n` : '';
}
const output = await this.ibmi.sendCommand({
command: `LC_ALL=EN_US.UTF-8 system "call QSYS/QZDFMDB2 PARM('-d' '-i' '-t')"`,
stdin: Tools.fixSQL(`${this.chgJobCCSID}${statements}`)
})
if (output.stdout) {
return Tools.db2Parse(output.stdout);
} else {
throw new Error(`There was an error running the SQL statement.`);
}
} else {
throw new Error(`There is no way to run SQL on this system.`);
}
}
/**
* @param ileCommand Command that would change the library list, like CHGLIBL
*/
async getLibraryListFromCommand(ileCommand: string): Promise<{ currentLibrary: string; libraryList: string[]; } | undefined> {
if (this.ibmi.remoteFeatures[`GETNEWLIBL.PGM`]) {
const tempLib = this.config.tempLibrary;
const resultSet = await this.runSQL(`CALL ${tempLib}.GETNEWLIBL('${ileCommand.replace(new RegExp(`'`, 'g'), `''`)}')`);
let result = {
currentLibrary: `QGPL`,
libraryList: [] as string[]
};
resultSet.forEach(row => {
const libraryName = String(row.SYSTEM_SCHEMA_NAME);
switch (row.PORTION) {
case `CURRENT`:
result.currentLibrary = libraryName;
break;
case `USER`:
result.libraryList.push(libraryName);
break;
}
})
return result;
}
return undefined;
}
/**
* Download the contents of a table.
* @param library
* @param file
* @param member Will default to file provided
* @param deleteTable Will delete the table after download
*/
async getTable(library: string, file: string, member?: string, deleteTable?: boolean): Promise<Tools.DB2Row[]> {
if (!member) member = file; //Incase mbr is the same file
if (file === member && this.config.enableSQL) {
const data = await this.runSQL(`SELECT * FROM ${library}.${file}`);
if (deleteTable && this.config.autoClearTempData) {
await this.ibmi.runCommand({
command: `DLTOBJ OBJ(${library}/${file}) OBJTYPE(*FILE)`,
noLibList: true
});
}
return data;
} else {
const tempRmt = this.getTempRemote(Tools.qualifyPath(library, file, member));
const copyResult = await this.ibmi.runCommand({
command: `QSYS/CPYTOIMPF FROMFILE(${library}/${file} ${member}) ` +
`TOSTMF('${tempRmt}') ` +
`MBROPT(*REPLACE) STMFCCSID(1208) RCDDLM(*CRLF) DTAFMT(*DLM) RMVBLANK(*TRAILING) ADDCOLNAM(*SQL) FLDDLM(',') DECPNT(*PERIOD)`,
noLibList: true
});
if (copyResult.code === 0) {
let result = await this.downloadStreamfile(tempRmt);
if (this.config.autoClearTempData) {
Promise.allSettled([
this.ibmi.sendCommand({ command: `rm -rf ${tempRmt}`, directory: `.` }),
deleteTable ? this.ibmi.runCommand({ command: `DLTOBJ OBJ(${library}/${file}) OBJTYPE(*FILE)`, noLibList: true }) : Promise.resolve()
]);
}
return parse(result, {
columns: true,
skip_empty_lines: true,
cast: true,
onRecord(record) {
for (const key of Object.keys(record)) {
record[key] = record[key] === ` ` ? `` : record[key];
}
return record;
}
});
} else {
throw new Error(`Failed fetching table: ${copyResult.stderr}`);
}
}
}
/**
* Prepare a table in QTEMP using any number of preparation queries and return its content.
* @param prepareQueries : SQL statements that should create a table in QTEMP
* @param table : the name of the table expected to be found in QTEMP
* @returns : the table's content
*/
async getQTempTable(prepareQueries: string[], table: string): Promise<Tools.DB2Row[]> {
prepareQueries.push(`Select * From QTEMP.${table}`);
const fullQuery = prepareQueries.map(query => query.endsWith(';') ? query : `${query};`).join("\n");
return await this.runSQL(fullQuery);
}
/**
* Get list of libraries with description and attribute
* @param libraries Array of libraries to retrieve
* @returns an array of libraries as IBMiObject
*/
async getLibraryList(libraries: string[]): Promise<IBMiObject[]> {
let results: Tools.DB2Row[];
if (this.config.enableSQL) {
const statement = `
select os.OBJNAME as ODOBNM
, coalesce(os.OBJTEXT, '') as ODOBTX
, os.OBJATTRIBUTE as ODOBAT
from table( SYSTOOLS.SPLIT( INPUT_LIST => '${libraries.toString()}', DELIMITER => ',' ) ) libs
, table( QSYS2.OBJECT_STATISTICS( OBJECT_SCHEMA => 'QSYS', OBJTYPELIST => '*LIB', OBJECT_NAME => libs.ELEMENT ) ) os
`;
results = await this.runSQL(statement);
} else {
results = await this.getQTempTable(libraries.map(library => `@DSPOBJD OBJ(QSYS/${library}) OBJTYPE(*LIB) DETAIL(*TEXTATR) OUTPUT(*OUTFILE) OUTFILE(QTEMP/LIBLIST) OUTMBR(*FIRST *ADD)`), "LIBLIST");
if (results.length === 1 && !results[0].ODOBNM?.toString().trim()) {
return [];
}
results = results.filter(object => libraries.includes(this.ibmi.sysNameInLocal(String(object.ODOBNM))));
};
const objects = results.map(object => ({
library: 'QSYS',
type: '*LIB',
name: this.config.enableSQL ? object.ODOBNM : this.ibmi.sysNameInLocal(String(object.ODOBNM)),
attribute: object.ODOBAT,
text: object.ODOBTX
} as IBMiObject));
return libraries.map(library => {
return objects.find(info => info.name === library) ||
{
library: 'QSYS',
type: '*LIB',
name: library,
attribute: ``,
text: `*** NOT FOUND ***`
};
});
}
/**
* Validates a list of libraries
* @param newLibl Array of libraries to validate
* @returns Bad libraries
*/
async validateLibraryList(newLibl: string[]): Promise<string[]> {
let badLibs: string[] = [];
newLibl = newLibl.filter(lib => {
if (lib.match(/^\d/)) {
badLibs.push(lib);
return false;
}
if (lib.length > 10) {
badLibs.push(lib);
return false;
}
return true;
});
const result = await this.ibmi.sendQsh({
command: [
`liblist -d ` + Tools.sanitizeLibraryNames(this.ibmi.defaultUserLibraries).join(` `),
...newLibl.map(lib => `liblist -a ` + Tools.sanitizeLibraryNames([lib]))
].join(`; `)
});
if (result.stderr) {
const lines = result.stderr.split(`\n`);
lines.forEach(line => {
const badLib = newLibl.find(lib => line.includes(`ibrary ${lib} `) || line.includes(`ibrary ${Tools.sanitizeLibraryNames([lib])} `));
// If there is an error about the library, remove it
if (badLib) badLibs.push(badLib);
});
}
return badLibs;
}
async getLibraries(filters: { library: string; filterType?: FilterType }) {
return this.getObjectList({ library: "QSYS", object: filters.library, types: ["*LIB"], filterType: filters.filterType });
}
/**
* @param filters
* @param sortOrder
* @returns an array of IBMiObject
*/
async getObjectList(filters: { library: string; object?: string; types?: string[]; filterType?: FilterType }, sortOrder?: SortOrder): Promise<IBMiObject[]> {
const library = filters.library.toUpperCase();
if (!await this.checkObject({ library: "QSYS", name: library, type: "*LIB" })) {
throw new Error(`Library ${library} does not exist.`);
}
const singleEntry = filters.filterType !== 'regex' ? singleGenericName(filters.object) : undefined;
const nameFilter = parseFilter(filters.object, filters.filterType);
const object = filters.object && (nameFilter.noFilter || singleEntry) && filters.object !== `*` ? filters.object.toUpperCase() : `*ALL`;
const typeFilter = filters.types && filters.types.length > 1 ? (t: string) => filters.types?.includes(t) : undefined;
const type = filters.types && filters.types.length === 1 && filters.types[0] !== '*' ? filters.types[0] : '*ALL';
const sourceFilesOnly = filters.types && filters.types.length === 1 && filters.types.includes(`*SRCPF`);
const withSourceFiles = ['*ALL', '*SRCPF', '*FILE'].includes(type);
const queries: string[] = [];
if (!sourceFilesOnly) {
queries.push(`@DSPOBJD OBJ(${library}/${object}) OBJTYPE(${type}) OUTPUT(*OUTFILE) OUTFILE(QTEMP/CODE4IOBJD)`);
}
if (withSourceFiles) {
queries.push(`@DSPFD FILE(${library}/${object}) TYPE(*ATR) FILEATR(*PF) OUTPUT(*OUTFILE) OUTFILE(QTEMP/CODE4IFD)`);
}
let createOBJLIST;
if (sourceFilesOnly) {
//DSPFD only
createOBJLIST = `select PHFILE as NAME, ` +
`'*FILE' as TYPE, ` +
`PHFILA as ATTRIBUTE, ` +
`PHTXT as TEXT, ` +
`1 as IS_SOURCE, ` +
`PHNOMB as NB_MBR, ` +
'PHMXRL as SOURCE_LENGTH, ' +
'PHCSID as CCSID ' +
`from QTEMP.CODE4IFD where PHDTAT = 'S'`;
} else if (!withSourceFiles) {
//DSPOBJD only
createOBJLIST = `select ODOBNM as NAME, ` +
`ODOBTP as TYPE, ` +
`ODOBAT as ATTRIBUTE, ` +
`ODOBTX as TEXT, ` +
`0 as IS_SOURCE, ` +
`ODOBSZ as SIZE, ` +
`ODCCEN, ` +
`ODCDAT, ` +
`ODCTIM, ` +
`ODLCEN, ` +
`ODLDAT, ` +
`ODLTIM, ` +
`ODOBOW as OWNER, ` +
`ODCRTU as CREATED_BY, ` +
`ODSIZU as SIZE_IN_UNITS, ` +
`ODBPUN as BYTES_PER_UNIT ` +
`from QTEMP.CODE4IOBJD`;
}
else {
//Both DSPOBJD and DSPFD
createOBJLIST = `select ODOBNM as NAME, ` +
`ODOBTP as TYPE, ` +
`ODOBAT as ATTRIBUTE, ` +
`ODOBTX as TEXT, ` +
`Case When PHDTAT = 'S' Then 1 Else 0 End as IS_SOURCE, ` +
`PHNOMB as NB_MBR, ` +
'PHMXRL as SOURCE_LENGTH, ' +
'PHCSID as CCSID, ' +
`ODOBSZ as SIZE, ` +
`ODCCEN, ` +
`ODCDAT, ` +
`ODCTIM, ` +
`ODLCEN, ` +
`ODLDAT, ` +
`ODLTIM, ` +
`ODOBOW as OWNER, ` +
`ODCRTU as CREATED_BY, ` +
`ODSIZU as SIZE_IN_UNITS, ` +
`ODBPUN as BYTES_PER_UNIT ` +
`from QTEMP.CODE4IOBJD ` +
`left join QTEMP.CODE4IFD on PHFILE = ODOBNM And PHDTAT = 'S'`;
}
queries.push(`create table QTEMP.OBJLIST as (${createOBJLIST}) with data`);
const objects = (await this.getQTempTable(queries, "OBJLIST"));
return objects.map(object => ({
library,
name: this.ibmi.sysNameInLocal(String(object.NAME)),
type: String(object.TYPE),
attribute: String(object.ATTRIBUTE),
text: String(object.TEXT),
memberCount: object.NB_MBR !== undefined ? Number(object.NB_MBR) : undefined,
sourceFile: Boolean(object.IS_SOURCE),
sourceLength: object.SOURCE_LENGTH !== undefined ? Number(object.SOURCE_LENGTH) : undefined,
CCSID: object.CCSID !== undefined ? Number(object.CCSID) : undefined,
size: Number(object.SIZE) !== 9999999999 ? Number(object.SIZE) : Number(object.SIZE_IN_UNITS) * Number(object.BYTES_PER_UNIT),
created: this.getDspObjDdDate(String(object.ODCCEN), String(object.ODCDAT), String(object.ODCTIM)),
changed: this.getDspObjDdDate(String(object.ODLCEN), String(object.ODLDAT), String(object.ODLTIM)),
created_by: object.CREATED_BY,
owner: object.OWNER,
} as IBMiObject))
.filter(object => !typeFilter || typeFilter(object.type))
.filter(object => nameFilter.test(object.name))
.sort((a, b) => {
if (a.library.localeCompare(b.library) != 0) {
return a.library.localeCompare(b.library)
}
else if (sortOrder === `name`) {
return a.name.localeCompare(b.name)
}
else {
return ((ObjectTypes.get(a.type) || 0) - (ObjectTypes.get(b.type) || 0)) || a.name.localeCompare(b.name);
}
});
}
/**
*
* @param filter: the criterias used to list the members
* @returns
*/
async getMemberList(filter: { library: string, sourceFile: string, members?: string, extensions?: string, sort?: SortOptions, filterType?: FilterType }): Promise<IBMiMember[]> {
const sort = filter.sort || { order: 'name' };
const library = filter.library.toUpperCase();
const sourceFile = filter.sourceFile.toUpperCase();
const memberFilter = parseFilter(filter.members, filter.filterType);
const singleMember = memberFilter.noFilter && filter.members && !filter.members.includes(",") ? filter.members.toLocaleUpperCase().replace(/[*]/g, `%`) : undefined;
const memberExtensionFilter = parseFilter(filter.extensions, filter.filterType);
const singleMemberExtension = memberExtensionFilter.noFilter && filter.extensions && !filter.extensions.includes(",") ? filter.extensions.toLocaleUpperCase().replace(/[*]/g, `%`) : undefined;
const statement =
`With MEMBERS As (
SELECT
rtrim(cast(a.system_table_schema as char(10) for bit data)) as LIBRARY,
b.avgrowsize as RECORD_LENGTH,
a.iasp_number as ASP,
rtrim(cast(a.system_table_name as char(10) for bit data)) AS SOURCE_FILE,
rtrim(cast(b.system_table_member as char(10) for bit data)) as NAME,
coalesce(rtrim(cast(b.source_type as varchar(10) for bit data)), '') as TYPE,
coalesce(rtrim(varchar(b.partition_text)), '') as TEXT,
b.NUMBER_ROWS as LINES,
extract(epoch from (b.CREATE_TIMESTAMP))*1000 as CREATED,
extract(epoch from (b.LAST_SOURCE_UPDATE_TIMESTAMP))*1000 as CHANGED
FROM qsys2.systables AS a
JOIN qsys2.syspartitionstat AS b
ON b.table_schema = a.table_schema AND
b.table_name = a.table_name
)
Select * From MEMBERS
Where LIBRARY = '${library}'
${sourceFile !== `*ALL` ? `And SOURCE_FILE = '${sourceFile}'` : ``}
${singleMember ? `And NAME Like '${singleMember}'` : ''}
${singleMemberExtension ? `And TYPE Like '${singleMemberExtension}'` : ''}
Order By ${sort.order === 'name' ? 'NAME' : 'CHANGED'} ${!sort.ascending ? 'DESC' : 'ASC'}`;
const results = await this.runSQL(statement);
if (results.length) {
const asp = this.ibmi.aspInfo[Number(results[0].ASP)];
return results.map(result => ({
asp,
library,
file: String(result.SOURCE_FILE),
name: String(result.NAME),
extension: String(result.TYPE),
recordLength: Number(result.RECORD_LENGTH) - 12,
text: `${result.TEXT || ``}${sourceFile === `*ALL` ? ` (${result.SOURCE_FILE})` : ``}`.trim(),
lines: Number(result.LINES),
created: new Date(result.CREATED ? Number(result.CREATED) : 0),
changed: new Date(result.CHANGED ? Number(result.CHANGED) : 0)
} as IBMiMember))
.filter(member => memberFilter.test(member.name))
.filter(member => memberExtensionFilter.test(member.extension));
}
else {
return [];
}
}
/**
* Get list of items in a path
* @param remotePath
* @return an array of IFSFile
*/
async getFileList(remotePath: string, sort: SortOptions = { order: "name" }, onListError?: (errors: string[]) => void): Promise<IFSFile[]> {
const { 'stat': STAT } = this.ibmi.remoteFeatures;
const { 'sort': SORT } = this.ibmi.remoteFeatures;
const items: IFSFile[] = [];
let fileListResult: CommandResult;
if (STAT && SORT) {
fileListResult = (await this.ibmi.sendCommand({
command: `cd '${remotePath}' && ${STAT} --dereference --printf="%A\t%h\t%U\t%G\t%s\t%Y\t%n\n" * .* ${sort.order === `date` ? `| ${SORT} --key=6` : ``} ${(sort.order === `date` && !sort.ascending) ? ` --reverse` : ``}`
}));
if (fileListResult.stdout !== '') {
const fileStatList = fileListResult.stdout;
const fileList = fileStatList.split(`\n`);
//Remove current and dir up.
fileList.forEach(item => {
let auth: string, hardLinks: string, owner: string, group: string, size: string, modified: string, name: string;
[auth, hardLinks, owner, group, size, modified, name] = item.split(`\t`);
if (name !== `..` && name !== `.`) {
const type = (auth.startsWith(`d`) ? `directory` : `streamfile`);
items.push({
type: type,
name: name,
path: path.posix.join(remotePath, name),
size: Number(size),
modified: new Date(Number(modified) * 1000),
owner: owner
});
};
});
}
} else {
fileListResult = (await this.ibmi.sendCommand({
command: `${this.ibmi.remoteFeatures.ls} -a -p -L ${sort.order === "date" ? "-t" : ""} ${(sort.order === 'date' && sort.ascending) ? "-r" : ""} ${Tools.escapePath(remotePath)}`
}));
if (fileListResult.stdout !== '') {
const fileList = fileListResult.stdout;
//Remove current and dir up.
fileList.split(`\n`)
.filter(item => item !== `../` && item !== `./`)
.forEach(item => {
const type = (item.endsWith(`/`) ? `directory` : `streamfile`);
items.push({
type: type,
name: (type === `directory` ? item.substring(0, item.length - 1) : item),
path: path.posix.join(remotePath, item)
});
});
}
}
if (sort.order === "name") {
items.sort((f1, f2) => f1.name.localeCompare(f2.name));
if (sort.ascending === false) {
items.reverse();
}
}
if (fileListResult.code !== 0) {
//Filter out the errors occurring when stat is run on a directory with no hidden or regular files
const errors = fileListResult.stderr.split("\n")
.filter(e => !e.toLowerCase().includes("cannot stat '*'") && !e.toLowerCase().includes("cannot stat '.*'"))
.filter(Tools.distinct);
if (errors.length) {
onListError ? onListError(errors) : errors.forEach(console.log);
}
}
return items;
}
async memberResolve(member: string, files: QsysPath[]): Promise<IBMiMember | undefined> {
// Escape names for shell
const pathList = files
.map(file => {
const asp = file.asp || this.config.sourceASP;
if (asp && asp.length > 0) {
return [
Tools.qualifyPath(file.library, file.name, member, asp, true),
Tools.qualifyPath(file.library, file.name, member, undefined, true)
].join(` `);
} else {
return Tools.qualifyPath(file.library, file.name, member, undefined, true);
}
})
.join(` `)
.toUpperCase();
const command = `for f in ${pathList}; do if [ -f $f ]; then echo $f; break; fi; done`;
const result = await this.ibmi.sendCommand({
command,
});
if (result.code === 0) {
const firstMost = result.stdout;
if (firstMost) {
try {
const simplePath = Tools.unqualifyPath(firstMost);
// This can error if the path format is wrong for some reason.
// Not that this would ever happen, but better to be safe than sorry
return this.ibmi.parserMemberPath(simplePath);
} catch (e) {
console.log(e);
}
}
}
return undefined;
}
async objectResolve(object: string, libraries: string[]): Promise<string | undefined> {
const command = `for f in ${libraries.map(lib => `/QSYS.LIB/${lib.toUpperCase()}.LIB/${object.toUpperCase()}.*`).join(` `)}; do if [ -f $f ] || [ -d $f ]; then echo $f; break; fi; done`;
const result = await this.ibmi.sendCommand({
command,
});
if (result.code === 0) {
const firstMost = result.stdout;
if (firstMost) {
const lib = Tools.unqualifyPath(firstMost);
return lib.split('/')[1];
}
}
return undefined;
}
async streamfileResolve(names: string[], directories: string[]): Promise<string | undefined> {
const command = `for f in ${directories.flatMap(dir => names.map(name => `"${path.posix.join(dir, name)}"`)).join(` `)}; do if [ -f "$f" ]; then echo $f; break; fi; done`;
const result = await this.ibmi.sendCommand({
command,
});
if (result.code === 0 && result.stdout) {
const firstMost = result.stdout;
return firstMost;
}
return undefined;
}
/**
* @param errorsString; several lines of `code:text`...
* @returns errors
*/
parseIBMiErrors(errorsString: string): IBMiError[] {
return errorsString.split(`\n`)
.map(error => error.split(':'))
.map(codeText => ({ code: codeText[0], text: codeText[1] }));
}
/**
* @param century; century code (1=20xx, 0=19xx)
* @param dateString: string in YYMMDD
* @param timeString: string in HHMMSS
* @returns date
*/
getDspObjDdDate(century: string = `0`, MMDDYY: string = `010101`, HHMMSS: string = `000000`): Date {
let year: string, month: string, day: string, hours: string, minutes: string, seconds: string;
let YYMMDD: string = MMDDYY.slice(4,).concat(MMDDYY.slice(0, 4));
let dateString: string = (century === `1` ? `20` : `19`).concat(YYMMDD.padStart(6, `0`)).concat(HHMMSS.padStart(6, `0`));
[, year, month, day, hours, minutes, seconds] = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(dateString) || [];
return new Date(Date.UTC(Number(year), Number(month) - 1, Number(day), Number(hours), Number(minutes), Number(seconds)));
}
/**
* Return `true` if `remotePath` denotes a directory
*
* @param remotePath: a remote IFS path
*/
async isDirectory(remotePath: string) {
return (await this.ibmi.sendCommand({
command: `cd ${remotePath}`
})).code === 0;
}
async checkObject(object: { library: string, name: string, type: string, member?: string }, authorities: Authority[] = [`*NONE`]) {
return (await this.ibmi.runCommand({
command: IBMiContent.toCl(`CHKOBJ`, {
obj: `${object.library.toLocaleUpperCase()}/${object.name.toLocaleUpperCase()}`,
objtype: object.type.toLocaleUpperCase(),
aut: authorities.join(" "),
mbr: object.member
}),
noLibList: true
})).code === 0;
}
async testStreamFile(path: string, right: "r" | "w" | "x") {
return (await this.ibmi.sendCommand({ command: `test -${right} ${Tools.escapePath(path)}` })).code === 0;
}
isProtectedPath(path: string) {
if (path.startsWith('/')) { //IFS path
return this.config.protectedPaths.some(p => path.startsWith(p));
}
else { //QSYS path
const qsysObject = Tools.parseQSysPath(path);
return this.config.protectedPaths.includes(qsysObject.library.toLocaleUpperCase());
}
}
/**
*
* @param command Optionally qualified CL command
* @param parameters A key/value object of parameters
* @returns Formatted CL string
*/
static toCl(command: string, parameters: { [parameter: string]: string | number | undefined }) {
let cl = command;
for (const [key, value] of Object.entries(parameters)) {
let parmValue;
if (value !== undefined) {
if (typeof value === 'string') {
if (value === value.toLocaleUpperCase()) {
parmValue = value;
} else {
parmValue = value.replace(/'/g, `''`);
parmValue = `'${parmValue}'`;
}
} else {
parmValue = String(value);
}
cl += ` ${key.toUpperCase()}(${parmValue})`;
}
}
return cl;
}
}