• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.jimfs;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static com.google.common.jimfs.UserLookupService.createUserPrincipal;
21 
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import java.io.IOException;
25 import java.nio.file.attribute.FileAttributeView;
26 import java.nio.file.attribute.FileOwnerAttributeView;
27 import java.nio.file.attribute.UserPrincipal;
28 import java.util.Map;
29 import org.checkerframework.checker.nullness.compatqual.NullableDecl;
30 
31 /**
32  * Attribute provider that provides the {@link FileOwnerAttributeView} ("owner").
33  *
34  * @author Colin Decker
35  */
36 final class OwnerAttributeProvider extends AttributeProvider {
37 
38   private static final ImmutableSet<String> ATTRIBUTES = ImmutableSet.of("owner");
39 
40   private static final UserPrincipal DEFAULT_OWNER = createUserPrincipal("user");
41 
42   @Override
name()43   public String name() {
44     return "owner";
45   }
46 
47   @Override
fixedAttributes()48   public ImmutableSet<String> fixedAttributes() {
49     return ATTRIBUTES;
50   }
51 
52   @Override
defaultValues(Map<String, ?> userProvidedDefaults)53   public ImmutableMap<String, ?> defaultValues(Map<String, ?> userProvidedDefaults) {
54     Object userProvidedOwner = userProvidedDefaults.get("owner:owner");
55 
56     UserPrincipal owner = DEFAULT_OWNER;
57     if (userProvidedOwner != null) {
58       if (userProvidedOwner instanceof String) {
59         owner = createUserPrincipal((String) userProvidedOwner);
60       } else {
61         throw invalidType("owner", "owner", userProvidedOwner, String.class, UserPrincipal.class);
62       }
63     }
64 
65     return ImmutableMap.of("owner:owner", owner);
66   }
67 
68   @NullableDecl
69   @Override
get(File file, String attribute)70   public Object get(File file, String attribute) {
71     if (attribute.equals("owner")) {
72       return file.getAttribute("owner", "owner");
73     }
74     return null;
75   }
76 
77   @Override
set(File file, String view, String attribute, Object value, boolean create)78   public void set(File file, String view, String attribute, Object value, boolean create) {
79     if (attribute.equals("owner")) {
80       checkNotCreate(view, attribute, create);
81       UserPrincipal user = checkType(view, attribute, value, UserPrincipal.class);
82       // TODO(cgdecker): Do we really need to do this? Any reason not to allow any UserPrincipal?
83       if (!(user instanceof UserLookupService.JimfsUserPrincipal)) {
84         user = createUserPrincipal(user.getName());
85       }
86       file.setAttribute("owner", "owner", user);
87     }
88   }
89 
90   @Override
viewType()91   public Class<FileOwnerAttributeView> viewType() {
92     return FileOwnerAttributeView.class;
93   }
94 
95   @Override
view( FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews)96   public FileOwnerAttributeView view(
97       FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
98     return new View(lookup);
99   }
100 
101   /** Implementation of {@link FileOwnerAttributeView}. */
102   private static final class View extends AbstractAttributeView implements FileOwnerAttributeView {
103 
View(FileLookup lookup)104     public View(FileLookup lookup) {
105       super(lookup);
106     }
107 
108     @Override
name()109     public String name() {
110       return "owner";
111     }
112 
113     @Override
getOwner()114     public UserPrincipal getOwner() throws IOException {
115       return (UserPrincipal) lookupFile().getAttribute("owner", "owner");
116     }
117 
118     @Override
setOwner(UserPrincipal owner)119     public void setOwner(UserPrincipal owner) throws IOException {
120       lookupFile().setAttribute("owner", "owner", checkNotNull(owner));
121     }
122   }
123 }
124