@@ -152,8 +152,10 @@ protected S3GuardTool(Configuration conf, String...opts) {
152
152
/**
153
153
* Parse DynamoDB region from either -m option or a S3 path.
154
154
*
155
- * This function should only be called from {@link S3GuardTool.Init} or
156
- * {@link S3GuardTool.Destroy}.
155
+ * Note that as a side effect, if the paths included an S3 path,
156
+ * and there is no region set on the CLI, then the S3A FS is
157
+ * initialized, after which {@link #filesystem} will no longer
158
+ * be null.
157
159
*
158
160
* @param paths remaining parameters from CLI.
159
161
* @throws IOException on I/O errors.
@@ -338,6 +340,7 @@ protected MetadataStore initMetadataStore(boolean forceCreate)
338
340
* @throws ExitUtil.ExitException if the FS is not an S3A FS
339
341
*/
340
342
protected void initS3AFileSystem (String path ) throws IOException {
343
+ LOG .debug ("Initializing S3A FS to {}" , path );
341
344
URI uri = toUri (path );
342
345
// Make sure that S3AFileSystem does not hold an actual MetadataStore
343
346
// implementation.
@@ -363,6 +366,28 @@ protected void initS3AFileSystem(String path) throws IOException {
363
366
filesystem = (S3AFileSystem ) fs ;
364
367
}
365
368
369
+ /**
370
+ * Initialize the filesystem if there is none bonded to already and
371
+ * the command line path list is not empty.
372
+ * @param paths path list.
373
+ * @return true if at the end of the call, getFilesystem() is not null
374
+ * @throws IOException failure to instantiate.
375
+ */
376
+ @ VisibleForTesting
377
+ boolean maybeInitFilesystem (final List <String > paths )
378
+ throws IOException {
379
+ // is there an S3 FS to create?
380
+ if (getFilesystem () == null ) {
381
+ // none yet -create one
382
+ if (!paths .isEmpty ()) {
383
+ initS3AFileSystem (paths .get (0 ));
384
+ } else {
385
+ LOG .debug ("No path on command line, so not instantiating FS" );
386
+ }
387
+ }
388
+ return getFilesystem () != null ;
389
+ }
390
+
366
391
/**
367
392
* Parse CLI arguments and returns the position arguments.
368
393
* The options are stored in {@link #commandFormat}.
@@ -592,6 +617,7 @@ public int run(String[] args, PrintStream out) throws Exception {
592
617
// Validate parameters.
593
618
try {
594
619
parseDynamoDBRegion (paths );
620
+ maybeInitFilesystem (paths );
595
621
} catch (ExitUtil .ExitException e ) {
596
622
errorln (USAGE );
597
623
throw e ;
@@ -644,6 +670,7 @@ public int run(String[] args, PrintStream out) throws Exception {
644
670
checkBucketNameOrDDBTableNameProvided (paths );
645
671
checkIfS3BucketIsGuarded (paths );
646
672
parseDynamoDBRegion (paths );
673
+ maybeInitFilesystem (paths );
647
674
} catch (ExitUtil .ExitException e ) {
648
675
errorln (USAGE );
649
676
throw e ;
@@ -1064,11 +1091,13 @@ public int run(String[] args, PrintStream out) throws
1064
1091
InterruptedException , IOException {
1065
1092
List <String > paths = parseArgs (args );
1066
1093
try {
1094
+ checkBucketNameOrDDBTableNameProvided (paths );
1067
1095
parseDynamoDBRegion (paths );
1068
1096
} catch (ExitUtil .ExitException e ) {
1069
1097
errorln (USAGE );
1070
1098
throw e ;
1071
1099
}
1100
+ maybeInitFilesystem (paths );
1072
1101
initMetadataStore (false );
1073
1102
1074
1103
Configuration conf = getConf ();
0 commit comments