-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AlluxioFuseFileSystem.java
826 lines (750 loc) · 26.6 KB
/
AlluxioFuseFileSystem.java
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
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.fuse;
import alluxio.AlluxioURI;
import alluxio.Configuration;
import alluxio.PropertyKey;
import alluxio.client.file.FileSystem;
import alluxio.client.file.URIStatus;
import alluxio.client.file.options.SetAttributeOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.DirectoryNotEmptyException;
import alluxio.exception.FileAlreadyExistsException;
import alluxio.exception.FileDoesNotExistException;
import alluxio.exception.InvalidPathException;
import alluxio.security.authorization.Mode;
import alluxio.security.group.provider.ShellBasedUnixGroupsMapping;
import alluxio.util.CommonUtils;
import alluxio.util.WaitForOptions;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import jnr.ffi.Pointer;
import jnr.ffi.types.gid_t;
import jnr.ffi.types.mode_t;
import jnr.ffi.types.off_t;
import jnr.ffi.types.size_t;
import jnr.ffi.types.uid_t;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.serce.jnrfuse.ErrorCodes;
import ru.serce.jnrfuse.FuseFillDir;
import ru.serce.jnrfuse.FuseStubFS;
import ru.serce.jnrfuse.struct.FileStat;
import ru.serce.jnrfuse.struct.FuseFileInfo;
import ru.serce.jnrfuse.struct.Timespec;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import javax.annotation.concurrent.ThreadSafe;
/**
* Main FUSE implementation class.
*
* Implements the FUSE callbacks defined by jnr-fuse.
*/
@ThreadSafe
final class AlluxioFuseFileSystem extends FuseStubFS {
private static final Logger LOG = LoggerFactory.getLogger(AlluxioFuseFileSystem.class);
private static final int MAX_OPEN_FILES = Integer.MAX_VALUE;
private static final int MAX_OPEN_WAITTIME_MS = 5000;
private static final long UID = AlluxioFuseUtils.getUid(System.getProperty("user.name"));
private static final long GID = AlluxioFuseUtils.getGid(System.getProperty("user.name"));
private final boolean mIsShellGroupMapping;
private final FileSystem mFileSystem;
// base path within Alluxio namespace that is used for FUSE operations
// For example, if alluxio-fuse is mounted in /mnt/alluxio and mAlluxioRootPath
// is /users/foo, then an operation on /mnt/alluxio/bar will be translated on
// an action on the URI alluxio://<master>:<port>/users/foo/bar
private final Path mAlluxioRootPath;
// Keeps a cache of the most recently translated paths from String to Alluxio URI
private final LoadingCache<String, AlluxioURI> mPathResolverCache;
// Table of open files with corresponding InputStreams and OutputStreams
private final Map<Long, OpenFileEntry> mOpenFiles;
private long mNextOpenFileId;
/**
* Creates a new instance of {@link AlluxioFuseFileSystem}.
*
* @param fs Alluxio file system
* @param opts options
*/
AlluxioFuseFileSystem(FileSystem fs, AlluxioFuseOptions opts) {
super();
mFileSystem = fs;
mAlluxioRootPath = Paths.get(opts.getAlluxioRoot());
mNextOpenFileId = 0L;
mOpenFiles = new HashMap<>();
final int maxCachedPaths = Configuration.getInt(PropertyKey.FUSE_CACHED_PATHS_MAX);
mIsShellGroupMapping = ShellBasedUnixGroupsMapping.class.getName()
.equals(Configuration.get(PropertyKey.SECURITY_GROUP_MAPPING_CLASS));
mPathResolverCache = CacheBuilder.newBuilder()
.maximumSize(maxCachedPaths)
.build(new PathCacheLoader());
Preconditions.checkArgument(mAlluxioRootPath.isAbsolute(),
"alluxio root path should be absolute");
}
/**
* Changes the mode of an Alluxio file.
*
* @param path the path of the file
* @param mode the mode to change to
* @return 0 on success, a negative value on error
*/
@Override
public int chmod(String path, @mode_t long mode) {
AlluxioURI uri = mPathResolverCache.getUnchecked(path);
SetAttributeOptions options = SetAttributeOptions.defaults().setMode(new Mode((short) mode));
try {
mFileSystem.setAttribute(uri, options);
} catch (IOException | AlluxioException e) {
LOG.error("Exception on {} of changing mode to {}", path, mode, e);
return -ErrorCodes.EIO();
}
return 0;
}
/**
* Changes the owner of an Alluxio file.
*
* This operation only works when the group mapping service is shelled based and the user is
* registered in unix. Otherwise it errors as not implemented. This is because the input uid and
* gid must match the user and group in Unix.
*/
@Override
public int chown(String path, @uid_t long uid, @gid_t long gid) {
if (!mIsShellGroupMapping) {
LOG.info("Cannot change the owner of path {} because the group mapping is not shell based",
path);
// not supported if the group mapping is not shell based
return -ErrorCodes.ENOSYS();
}
try {
String userName = AlluxioFuseUtils.getUserName(uid);
String groupName = AlluxioFuseUtils.getGroupName(uid);
if (userName.isEmpty()) {
LOG.error("Failed to get user name from uid {}", uid);
return -ErrorCodes.EFAULT();
}
if (groupName.isEmpty()) {
LOG.error("Failed to get group name from uid {}", uid);
return -ErrorCodes.EFAULT();
}
SetAttributeOptions options =
SetAttributeOptions.defaults().setGroup(groupName).setOwner(userName);
final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
LOG.info("Change owner and group of file {} to {}:{}", path, userName, groupName);
mFileSystem.setAttribute(uri, options);
} catch (IOException | AlluxioException e) {
LOG.error("Exception on {}", path, e);
return -ErrorCodes.EIO();
}
return 0;
}
/**
* Creates and opens a new file.
*
* @param path The FS path of the file to open
* @param mode mode flags
* @param fi FileInfo data struct kept by FUSE
* @return 0 on success. A negative value on error
*/
@Override
public int create(String path, @mode_t long mode, FuseFileInfo fi) {
final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
final int flags = fi.flags.get();
LOG.trace("create({}, {}) [Alluxio: {}]", path, Integer.toHexString(flags), uri);
try {
synchronized (mOpenFiles) {
if (mOpenFiles.size() >= MAX_OPEN_FILES) {
LOG.error("Cannot open {}: too many open files (MAX_OPEN_FILES: {})", uri,
MAX_OPEN_FILES);
return -ErrorCodes.EMFILE();
}
final OpenFileEntry ofe = new OpenFileEntry(null, mFileSystem.createFile(uri));
LOG.debug("Alluxio OutStream created for {}", path);
mOpenFiles.put(mNextOpenFileId, ofe);
fi.fh.set(mNextOpenFileId);
// Assuming I will never wrap around (2^64 open files are quite a lot anyway)
mNextOpenFileId += 1;
}
LOG.debug("{} created and opened", path);
} catch (FileAlreadyExistsException e) {
LOG.debug("File {} already exists", uri, e);
return -ErrorCodes.EEXIST();
} catch (IOException e) {
LOG.error("IOException on {}", uri, e);
return -ErrorCodes.EIO();
} catch (AlluxioException e) {
LOG.error("AlluxioException on {}", uri, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception on {}", path, e);
return -ErrorCodes.EFAULT();
}
return 0;
}
/**
* Flushes cached data on Alluxio.
*
* Called on explicit sync() operation or at close().
*
* @param path The path on the FS of the file to close
* @param fi FileInfo data struct kept by FUSE
* @return 0 on success, a negative value on error
*/
@Override
public int flush(String path, FuseFileInfo fi) {
LOG.trace("flush({})", path);
final long fd = fi.fh.get();
OpenFileEntry oe;
synchronized (mOpenFiles) {
oe = mOpenFiles.get(fd);
}
if (oe == null) {
LOG.error("Cannot find fd for {} in table", path);
return -ErrorCodes.EBADFD();
}
if (oe.getOut() != null) {
try {
oe.getOut().flush();
} catch (IOException e) {
LOG.error("IOException on {}", path, e);
return -ErrorCodes.EIO();
}
} else {
LOG.debug("Not flushing: {} was not open for writing", path);
}
return 0;
}
/**
* Retrieves file attributes.
*
* @param path The path on the FS of the file
* @param stat FUSE data structure to fill with file attrs
* @return 0 on success, negative value on error
*/
@Override
public int getattr(String path, FileStat stat) {
final AlluxioURI turi = mPathResolverCache.getUnchecked(path);
LOG.trace("getattr({}) [Alluxio: {}]", path, turi);
try {
if (!mFileSystem.exists(turi)) {
return -ErrorCodes.ENOENT();
}
final URIStatus status = mFileSystem.getStatus(turi);
stat.st_size.set(status.getLength());
final long ctime_sec = status.getLastModificationTimeMs() / 1000;
//keeps only the "residual" nanoseconds not caputred in
// citme_sec
final long ctime_nsec = (status.getLastModificationTimeMs() % 1000) * 1000;
stat.st_ctim.tv_sec.set(ctime_sec);
stat.st_ctim.tv_nsec.set(ctime_nsec);
stat.st_mtim.tv_sec.set(ctime_sec);
stat.st_mtim.tv_nsec.set(ctime_nsec);
// for shell-based group mapping, use the uid and gid of the corresponding user registered in
// unix; otherwise use uid and gid of the user running alluxio-fuse
if (mIsShellGroupMapping) {
String owner = status.getOwner();
stat.st_uid.set(AlluxioFuseUtils.getUid(owner));
stat.st_gid.set(AlluxioFuseUtils.getGid(owner));
} else {
stat.st_uid.set(UID);
stat.st_gid.set(GID);
}
int mode = status.getMode();
if (status.isFolder()) {
mode |= FileStat.S_IFDIR;
} else {
mode |= FileStat.S_IFREG;
}
stat.st_mode.set(mode);
} catch (InvalidPathException e) {
LOG.debug("Invalid path {}", path, e);
return -ErrorCodes.ENOENT();
} catch (FileDoesNotExistException e) {
LOG.debug("File does not exist {}", path, e);
return -ErrorCodes.ENOENT();
} catch (IOException e) {
LOG.error("IOException on {}", path, e);
return -ErrorCodes.EIO();
} catch (AlluxioException e) {
LOG.error("AlluxioException on {}", path, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception on {}", path, e);
return -ErrorCodes.EFAULT();
}
return 0;
}
/**
* @return Name of the file system
*/
@Override
public String getFSName() {
return Configuration.get(PropertyKey.FUSE_FS_NAME);
}
/**
* Creates a new dir.
*
* @param path the path on the FS of the new dir
* @param mode Dir creation flags (IGNORED)
* @return 0 on success, a negative value on error
*/
@Override
public int mkdir(String path, @mode_t long mode) {
final AlluxioURI turi = mPathResolverCache.getUnchecked(path);
LOG.trace("mkdir({}) [Alluxio: {}]", path, turi);
try {
mFileSystem.createDirectory(turi);
} catch (FileAlreadyExistsException e) {
LOG.debug("Cannot make dir. {} already exists", path, e);
return -ErrorCodes.EEXIST();
} catch (InvalidPathException e) {
LOG.debug("Cannot make dir. Invalid path: {}", path, e);
return -ErrorCodes.ENOENT();
} catch (IOException e) {
LOG.error("Cannot make dir. IOException: {}", path, e);
return -ErrorCodes.EIO();
} catch (AlluxioException e) {
LOG.error("Cannot make dir. {}", path, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception on {}", path, e);
return -ErrorCodes.EFAULT();
}
return 0;
}
/**
* Opens an existing file for reading.
*
* Note that the opening an existing file would fail, because of Alluxio's write-once semantics.
*
* @param path the FS path of the file to open
* @param fi FileInfo data structure kept by FUSE
* @return 0 on success, a negative value on error
*/
@Override
public int open(String path, FuseFileInfo fi) {
final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
// (see {@code man 2 open} for the structure of the flags bitfield)
// File creation flags are the last two bits of flags
final int flags = fi.flags.get();
LOG.trace("open({}, 0x{}) [Alluxio: {}]", path, Integer.toHexString(flags), uri);
try {
if (!mFileSystem.exists(uri)) {
LOG.error("File {} does not exist", uri);
return -ErrorCodes.ENOENT();
}
final URIStatus status = mFileSystem.getStatus(uri);
if (status.isFolder()) {
LOG.error("File {} is a directory", uri);
return -ErrorCodes.EISDIR();
}
if (!status.isCompleted() && !waitForFileCompleted(uri)) {
LOG.error("File {} has not completed", uri);
return -ErrorCodes.EFAULT();
}
synchronized (mOpenFiles) {
if (mOpenFiles.size() == MAX_OPEN_FILES) {
LOG.error("Cannot open {}: too many open files", uri);
return ErrorCodes.EMFILE();
}
final OpenFileEntry ofe = new OpenFileEntry(mFileSystem.openFile(uri), null);
mOpenFiles.put(mNextOpenFileId, ofe);
fi.fh.set(mNextOpenFileId);
// Assuming I will never wrap around (2^64 open files are quite a lot anyway)
mNextOpenFileId += 1;
}
} catch (FileDoesNotExistException e) {
LOG.debug("File does not exist {}", path, e);
return -ErrorCodes.ENOENT();
} catch (IOException e) {
LOG.error("IOException on {}", path, e);
return -ErrorCodes.EIO();
} catch (AlluxioException e) {
LOG.error("AlluxioException on {}", path, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception on {}", path, e);
return -ErrorCodes.EFAULT();
}
return 0;
}
/**
* Reads data from an open file.
*
* @param path the FS path of the file to read
* @param buf FUSE buffer to fill with data read
* @param size how many bytes to read. The maximum value that is accepted
* on this method is {@link Integer#MAX_VALUE} (note that current
* FUSE implementation will call this metod whit a size of
* at most 128K).
* @param offset offset of the read operation
* @param fi FileInfo data structure kept by FUSE
* @return the number of bytes read or 0 on EOF. A negative
* value on error
*/
@Override
public int read(String path, Pointer buf, @size_t long size, @off_t long offset,
FuseFileInfo fi) {
if (size > Integer.MAX_VALUE) {
LOG.error("Cannot read more than Integer.MAX_VALUE");
return -ErrorCodes.EINVAL();
}
LOG.trace("read({}, {}, {})", path, size, offset);
final int sz = (int) size;
final long fd = fi.fh.get();
OpenFileEntry oe;
synchronized (mOpenFiles) {
oe = mOpenFiles.get(fd);
}
if (oe == null) {
LOG.error("Cannot find fd for {} in table", path);
return -ErrorCodes.EBADFD();
}
int rd = 0;
int nread = 0;
if (oe.getIn() == null) {
LOG.error("{} was not open for reading", path);
return -ErrorCodes.EBADFD();
}
try {
oe.getIn().seek(offset);
final byte[] dest = new byte[sz];
while (rd >= 0 && nread < size) {
rd = oe.getIn().read(dest, nread, sz - nread);
if (rd >= 0) {
nread += rd;
}
}
if (nread == -1) { // EOF
nread = 0;
} else if (nread > 0) {
buf.put(0, dest, 0, nread);
}
} catch (IOException e) {
LOG.error("IOException while reading from {}.", path, e);
return -ErrorCodes.EIO();
} catch (Throwable e) {
LOG.error("Unexpected exception on {}", path, e);
return -ErrorCodes.EFAULT();
}
return nread;
}
/**
* Reads the contents of a directory.
*
* @param path The FS path of the directory
* @param buff The FUSE buffer to fill
* @param filter FUSE filter
* @param offset Ignored in alluxio-fuse
* @param fi FileInfo data structure kept by FUSE
* @return 0 on success, a negative value on error
*/
@Override
public int readdir(String path, Pointer buff, FuseFillDir filter,
@off_t long offset, FuseFileInfo fi) {
final AlluxioURI turi = mPathResolverCache.getUnchecked(path);
LOG.trace("readdir({}) [Alluxio: {}]", path, turi);
try {
if (!mFileSystem.exists(turi)) {
return -ErrorCodes.ENOENT();
}
final URIStatus status = mFileSystem.getStatus(turi);
if (!status.isFolder()) {
return -ErrorCodes.ENOTDIR();
}
final List<URIStatus> ls = mFileSystem.listStatus(turi);
// standard . and .. entries
filter.apply(buff, ".", null, 0);
filter.apply(buff, "..", null, 0);
for (final URIStatus file : ls) {
filter.apply(buff, file.getName(), null, 0);
}
} catch (FileDoesNotExistException e) {
LOG.debug("File does not exist {}", path, e);
return -ErrorCodes.ENOENT();
} catch (InvalidPathException e) {
LOG.debug("Invalid path {}", path, e);
return -ErrorCodes.ENOENT();
} catch (IOException e) {
LOG.error("IOException on {}", path, e);
return -ErrorCodes.EIO();
} catch (AlluxioException e) {
LOG.error("AlluxioException on {}", path, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception on {}", path, e);
return -ErrorCodes.EFAULT();
}
return 0;
}
/**
* Releases the resources associated to an open file.
*
* Guaranteed to be called once for each open() or create().
*
* @param path the FS path of the file to release
* @param fi FileInfo data structure kept by FUSE
* @return 0. The return value is ignored by FUSE (any error should be reported
* on flush instead)
*/
@Override
public int release(String path, FuseFileInfo fi) {
LOG.trace("release({})", path);
final long fd = fi.fh.get();
OpenFileEntry oe;
synchronized (mOpenFiles) {
oe = mOpenFiles.remove(fd);
if (oe == null) {
LOG.error("Cannot find fd for {} in table", path);
return -ErrorCodes.EBADFD();
}
}
try {
oe.close();
} catch (IOException e) {
LOG.error("Failed closing {} [in]", path, e);
}
return 0;
}
/**
* Renames a path.
*
* @param oldPath the source path in the FS
* @param newPath the destination path in the FS
* @return 0 on success, a negative value on error
*/
@Override
public int rename(String oldPath, String newPath) {
final AlluxioURI oldUri = mPathResolverCache.getUnchecked(oldPath);
final AlluxioURI newUri = mPathResolverCache.getUnchecked(newPath);
LOG.trace("rename({}, {}) [Alluxio: {}, {}]", oldPath, newPath, oldUri, newUri);
try {
if (!mFileSystem.exists(oldUri)) {
LOG.error("File {} does not exist", oldPath);
return -ErrorCodes.ENOENT();
}
if (mFileSystem.exists(newUri)) {
LOG.error("File {} already exists, please delete the destination file first", newPath);
return -ErrorCodes.EEXIST();
}
mFileSystem.rename(oldUri, newUri);
} catch (FileDoesNotExistException e) {
LOG.debug("File {} does not exist", oldPath);
return -ErrorCodes.ENOENT();
} catch (IOException e) {
LOG.error("IOException while moving {} to {}", oldPath, newPath, e);
return -ErrorCodes.EIO();
} catch (AlluxioException e) {
LOG.error("Exception while moving {} to {}", oldPath, newPath, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception on mv {} {}", oldPath, newPath, e);
return -ErrorCodes.EFAULT();
}
return 0;
}
/**
* Deletes an empty directory.
*
* @param path The FS path of the directory
* @return 0 on success, a negative value on error
*/
@Override
public int rmdir(String path) {
LOG.trace("rmdir({})", path);
return rmInternal(path, false);
}
/**
* Changes the size of a file. This operation would not succeed because of Alluxio's write-once
* model.
*/
@Override
public int truncate(String path, long size) {
final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
try {
if (!mFileSystem.exists(uri)) {
LOG.error("File {} does not exist", uri);
return -ErrorCodes.ENOENT();
}
} catch (IOException e) {
LOG.error("IOException encountered at path {}", path, e);
return -ErrorCodes.EIO();
} catch (AlluxioException e) {
LOG.error("AlluxioException encountered at path {}", path, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception at path {}", path, e);
return -ErrorCodes.EFAULT();
}
LOG.error("File {} exists and cannot be overwritten. Please delete the file first", uri);
return -ErrorCodes.EEXIST();
}
/**
* Deletes a file from the FS.
*
* @param path the FS path of the file
* @return 0 on success, a negative value on error
*/
@Override
public int unlink(String path) {
LOG.trace("unlink({})", path);
return rmInternal(path, true);
}
/**
* Alluxio does not have access time, and the file is created only once. So this operation is a
* no-op.
*/
@Override
public int utimens(String path, Timespec[] timespec) {
return 0;
}
/**
* Writes a buffer to an open Alluxio file. Random write is not supported, so the offset argument
* is ignored. Also, due to an issue in OSXFUSE that may write the same content at a offset
* multiple times, the write also checks that the subsequent write of the same offset is ignored.
*
* @param buf The buffer with source data
* @param size How much data to write from the buffer. The maximum accepted size for writes is
* {@link Integer#MAX_VALUE}. Note that current FUSE implementation will anyway call write
* with at most 128K writes
* @param offset The offset where to write in the file (IGNORED)
* @param fi FileInfo data structure kept by FUSE
* @return number of bytes written on success, a negative value on error
*/
@Override
public int write(String path, Pointer buf, @size_t long size, @off_t long offset,
FuseFileInfo fi) {
if (size > Integer.MAX_VALUE) {
LOG.error("Cannot write more than Integer.MAX_VALUE");
return ErrorCodes.EIO();
}
LOG.trace("write({}, {}, {})", path, size, offset);
final int sz = (int) size;
final long fd = fi.fh.get();
OpenFileEntry oe;
synchronized (mOpenFiles) {
oe = mOpenFiles.get(fd);
}
if (oe == null) {
LOG.error("Cannot find fd for {} in table", path);
return -ErrorCodes.EBADFD();
}
if (oe.getOut() == null) {
LOG.error("{} already exists in Alluxio and cannot be overwritten."
+ " Please delete this file first.", path);
return -ErrorCodes.EEXIST();
}
if (offset < oe.getWriteOffset()) {
// no op
return sz;
}
try {
final byte[] dest = new byte[sz];
buf.get(0, dest, 0, sz);
oe.getOut().write(dest);
oe.setWriteOffset(offset + size);
} catch (IOException e) {
LOG.error("IOException while writing to {}.", path, e);
return -ErrorCodes.EIO();
}
return sz;
}
/**
* Convenience internal method to remove files or directories.
*
* @param path The path to remove
* @param mustBeFile When true, returns an error when trying to
* remove a directory
* @return 0 on success, a negative value on error
*/
private int rmInternal(String path, boolean mustBeFile) {
final AlluxioURI turi = mPathResolverCache.getUnchecked(path);
try {
if (!mFileSystem.exists(turi)) {
LOG.error("File {} does not exist", turi);
return -ErrorCodes.ENOENT();
}
final URIStatus status = mFileSystem.getStatus(turi);
if (mustBeFile && status.isFolder()) {
LOG.error("File {} is a directory", turi);
return -ErrorCodes.EISDIR();
}
mFileSystem.delete(turi);
} catch (FileDoesNotExistException e) {
LOG.debug("File does not exist {}", path, e);
return -ErrorCodes.ENOENT();
} catch (IOException e) {
LOG.error("IOException on {}", path, e);
return -ErrorCodes.EIO();
} catch (DirectoryNotEmptyException e) {
LOG.error("{} is not empty", path, e);
return -ErrorCodes.ENOTEMPTY();
} catch (AlluxioException e) {
LOG.error("AlluxioException on {}", path, e);
return -ErrorCodes.EFAULT();
} catch (Throwable e) {
LOG.error("Unexpected exception on {}", path, e);
return -ErrorCodes.EFAULT();
}
return 0;
}
/**
* Waits for the file to complete before opening it.
*
* @param uri the file path to check
* @return whether the file is completed or not
*/
private boolean waitForFileCompleted(AlluxioURI uri) {
try {
CommonUtils.waitFor("file completed", () -> {
try {
return mFileSystem.getStatus(uri).isCompleted();
} catch (Exception e) {
throw new RuntimeException(e);
}
}, WaitForOptions.defaults().setTimeoutMs(MAX_OPEN_WAITTIME_MS));
return true;
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return false;
} catch (TimeoutException te) {
return false;
}
}
/**
* Exposed for testing.
*/
LoadingCache<String, AlluxioURI> getPathResolverCache() {
return mPathResolverCache;
}
/**
* Resolves a FUSE path into {@link AlluxioURI} and possibly keeps it in the cache.
*/
private final class PathCacheLoader extends CacheLoader<String, AlluxioURI> {
/**
* Constructs a new {@link PathCacheLoader}.
*/
public PathCacheLoader() {}
@Override
public AlluxioURI load(String fusePath) {
// fusePath is guaranteed to always be an absolute path (i.e., starts
// with a fwd slash) - relative to the FUSE mount point
final String relPath = fusePath.substring(1);
final Path tpath = mAlluxioRootPath.resolve(relPath);
return new AlluxioURI(tpath.toString());
}
}
}