1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.nfs.nfs3.request;
19 
20 import java.util.EnumSet;
21 
22 import org.apache.hadoop.nfs.NfsTime;
23 import org.apache.hadoop.oncrpc.XDR;
24 
25 /**
26  * SetAttr3 contains the file attributes that can be set from the client. The
27  * fields are the same as the similarly named fields in the NFS3Attributes
28  * structure.
29  */
30 public class SetAttr3 {
31   // Options for time stamp change
32   public static final int TIME_DONT_CHANGE = 0;
33   public static final int TIME_SET_TO_SERVER_TIME = 1;
34   public static final int TIME_SET_TO_CLIENT_TIME = 2;
35 
36   private int mode;
37   private int uid;
38   private int gid;
39   private long size;
40   private NfsTime atime;
41   private NfsTime mtime;
42   private EnumSet<SetAttrField> updateFields;
43 
44   public static enum SetAttrField {
45     MODE, UID, GID, SIZE, ATIME, MTIME
46   };
47 
SetAttr3()48   public SetAttr3() {
49     mode = 0;
50     uid = 0;
51     gid = 0;
52     size = 0;
53     updateFields = EnumSet.noneOf(SetAttrField.class);
54   }
55 
SetAttr3(int mode, int uid, int gid, long size, NfsTime atime, NfsTime mtime, EnumSet<SetAttrField> updateFields)56   public SetAttr3(int mode, int uid, int gid, long size, NfsTime atime,
57       NfsTime mtime, EnumSet<SetAttrField> updateFields) {
58     this.mode = mode;
59     this.uid = uid;
60     this.gid = gid;
61     this.size = size;
62     this.updateFields = updateFields;
63   }
64 
getMode()65   public int getMode() {
66     return mode;
67   }
68 
getUid()69   public int getUid() {
70     return uid;
71   }
72 
getGid()73   public int getGid() {
74     return gid;
75   }
76 
setGid(int gid)77   public void setGid(int gid) {
78     this.gid = gid;
79   }
80 
getSize()81   public long getSize() {
82     return size;
83   }
84 
getAtime()85   public NfsTime getAtime() {
86     return atime;
87   }
88 
getMtime()89   public NfsTime getMtime() {
90     return mtime;
91   }
92 
getUpdateFields()93   public EnumSet<SetAttrField> getUpdateFields() {
94     return updateFields;
95   }
96 
setUpdateFields(EnumSet<SetAttrField> updateFields)97   public void setUpdateFields(EnumSet<SetAttrField> updateFields) {
98     this.updateFields = updateFields;
99   }
100 
serialize(XDR xdr)101   public void serialize(XDR xdr) {
102     if (!updateFields.contains(SetAttrField.MODE)) {
103       xdr.writeBoolean(false);
104     } else {
105       xdr.writeBoolean(true);
106       xdr.writeInt(mode);
107     }
108     if (!updateFields.contains(SetAttrField.UID)) {
109       xdr.writeBoolean(false);
110     } else {
111       xdr.writeBoolean(true);
112       xdr.writeInt(uid);
113     }
114     if (!updateFields.contains(SetAttrField.GID)) {
115       xdr.writeBoolean(false);
116     } else {
117       xdr.writeBoolean(true);
118       xdr.writeInt(gid);
119     }
120     if (!updateFields.contains(SetAttrField.SIZE)) {
121       xdr.writeBoolean(false);
122     } else {
123       xdr.writeBoolean(true);
124       xdr.writeLongAsHyper(size);
125     }
126     if (!updateFields.contains(SetAttrField.ATIME)) {
127       xdr.writeBoolean(false);
128     } else {
129       xdr.writeBoolean(true);
130       atime.serialize(xdr);
131     }
132     if (!updateFields.contains(SetAttrField.MTIME)) {
133       xdr.writeBoolean(false);
134     } else {
135       xdr.writeBoolean(true);
136       mtime.serialize(xdr);
137     }
138   }
139 
deserialize(XDR xdr)140   public void deserialize(XDR xdr) {
141     if (xdr.readBoolean()) {
142       mode = xdr.readInt();
143       updateFields.add(SetAttrField.MODE);
144     }
145     if (xdr.readBoolean()) {
146       uid = xdr.readInt();
147       updateFields.add(SetAttrField.UID);
148     }
149     if (xdr.readBoolean()) {
150       gid = xdr.readInt();
151       updateFields.add(SetAttrField.GID);
152 
153     }
154     if (xdr.readBoolean()) {
155       size = xdr.readHyper();
156       updateFields.add(SetAttrField.SIZE);
157 
158     }
159     int timeSetHow = xdr.readInt();
160     if (timeSetHow == TIME_SET_TO_CLIENT_TIME) {
161       atime = NfsTime.deserialize(xdr);
162       updateFields.add(SetAttrField.ATIME);
163     } else if (timeSetHow == TIME_SET_TO_SERVER_TIME) {
164       atime = new NfsTime(System.currentTimeMillis());
165       updateFields.add(SetAttrField.ATIME);
166     }
167 
168     timeSetHow = xdr.readInt();
169     if (timeSetHow == TIME_SET_TO_CLIENT_TIME) {
170       mtime = NfsTime.deserialize(xdr);
171       updateFields.add(SetAttrField.MTIME);
172     } else if (timeSetHow == TIME_SET_TO_SERVER_TIME) {
173       mtime = new NfsTime(System.currentTimeMillis());
174       updateFields.add(SetAttrField.MTIME);
175     }
176   }
177 }
178