Get timothy quick tests building cleanly.
[yaffs2.git] / yaffs_attribs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include "yaffs_guts.h"
15 #include "yaffs_attribs.h"
16
17 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
18 #define IATTR_UID ia_uid
19 #define IATTR_GID ia_gid
20 #else
21 #define IATTR_UID ia_uid.val
22 #define IATTR_GID ia_gid.val
23 #endif
24
25 /*
26  * Loading attibs from/to object header assumes the object header
27  * is in cpu endian.
28  */
29 void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
30 {
31         obj->yst_uid = oh->yst_uid;
32         obj->yst_gid = oh->yst_gid;
33         obj->yst_atime = oh->yst_atime;
34         obj->yst_mtime = oh->yst_mtime;
35         obj->yst_ctime = oh->yst_ctime;
36         obj->yst_rdev = oh->yst_rdev;
37 }
38
39 void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj)
40 {
41         oh->yst_uid = obj->yst_uid;
42         oh->yst_gid = obj->yst_gid;
43         oh->yst_atime = obj->yst_atime;
44         oh->yst_mtime = obj->yst_mtime;
45         oh->yst_ctime = obj->yst_ctime;
46         oh->yst_rdev = obj->yst_rdev;
47
48 }
49
50 void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c)
51 {
52         obj->yst_mtime = Y_CURRENT_TIME;
53         if (do_a)
54                 obj->yst_atime = obj->yst_mtime;
55         if (do_c)
56                 obj->yst_ctime = obj->yst_mtime;
57 }
58
59 void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev)
60 {
61         yaffs_load_current_time(obj, 1, 1);
62         obj->yst_rdev = rdev;
63         obj->yst_uid = uid;
64         obj->yst_gid = gid;
65 }
66
67 static loff_t yaffs_get_file_size(struct yaffs_obj *obj)
68 {
69         YCHAR *alias = NULL;
70         obj = yaffs_get_equivalent_obj(obj);
71
72         switch (obj->variant_type) {
73         case YAFFS_OBJECT_TYPE_FILE:
74                 return obj->variant.file_variant.file_size;
75         case YAFFS_OBJECT_TYPE_SYMLINK:
76                 alias = obj->variant.symlink_variant.alias;
77                 if (!alias)
78                         return 0;
79                 return strnlen(alias, YAFFS_MAX_ALIAS_LENGTH);
80         default:
81                 return 0;
82         }
83 }
84
85 int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
86 {
87         unsigned int valid = attr->ia_valid;
88
89         if (valid & ATTR_MODE)
90                 obj->yst_mode = attr->ia_mode;
91         if (valid & ATTR_UID)
92                 obj->yst_uid = attr->IATTR_UID;
93         if (valid & ATTR_GID)
94                 obj->yst_gid = attr->IATTR_GID;
95
96         if (valid & ATTR_ATIME)
97                 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
98         if (valid & ATTR_CTIME)
99                 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
100         if (valid & ATTR_MTIME)
101                 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
102
103         if (valid & ATTR_SIZE)
104                 yaffs_resize_file(obj, attr->ia_size);
105
106         yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
107
108         return YAFFS_OK;
109
110 }
111
112 int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
113 {
114         unsigned int valid = 0;
115
116         attr->ia_mode = obj->yst_mode;
117         valid |= ATTR_MODE;
118         attr->IATTR_UID = obj->yst_uid;
119         valid |= ATTR_UID;
120         attr->IATTR_GID = obj->yst_gid;
121         valid |= ATTR_GID;
122
123         Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
124         valid |= ATTR_ATIME;
125         Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
126         valid |= ATTR_CTIME;
127         Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
128         valid |= ATTR_MTIME;
129
130         attr->ia_size = yaffs_get_file_size(obj);
131         valid |= ATTR_SIZE;
132
133         attr->ia_valid = valid;
134
135         return YAFFS_OK;
136 }