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