xref: /openbmc/u-boot/fs/yaffs2/yaffs_attribs.c (revision 5c8d14df)
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_attribs.h"
15 
16 
17 void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
18 {
19 	obj->yst_uid = oh->yst_uid;
20 	obj->yst_gid = oh->yst_gid;
21 	obj->yst_atime = oh->yst_atime;
22 	obj->yst_mtime = oh->yst_mtime;
23 	obj->yst_ctime = oh->yst_ctime;
24 	obj->yst_rdev = oh->yst_rdev;
25 }
26 
27 
28 void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj)
29 {
30 #ifdef CONFIG_YAFFS_WINCE
31 		oh->win_atime[0] = obj->win_atime[0];
32 		oh->win_ctime[0] = obj->win_ctime[0];
33 		oh->win_mtime[0] = obj->win_mtime[0];
34 		oh->win_atime[1] = obj->win_atime[1];
35 		oh->win_ctime[1] = obj->win_ctime[1];
36 		oh->win_mtime[1] = obj->win_mtime[1];
37 #else
38 		oh->yst_uid = obj->yst_uid;
39 		oh->yst_gid = obj->yst_gid;
40 		oh->yst_atime = obj->yst_atime;
41 		oh->yst_mtime = obj->yst_mtime;
42 		oh->yst_ctime = obj->yst_ctime;
43 		oh->yst_rdev = obj->yst_rdev;
44 #endif
45 
46 }
47 
48 void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev)
49 {
50 
51 #ifdef CONFIG_YAFFS_WINCE
52 		yfsd_win_file_time_now(obj->win_atime);
53 		obj->win_ctime[0] = obj->win_mtime[0] = obj->win_atime[0];
54 		obj->win_ctime[1] = obj->win_mtime[1] = obj->win_atime[1];
55 
56 #else
57 	yaffs_load_current_time(obj, 1, 1);
58 	obj->yst_rdev = rdev;
59 	obj->yst_uid = uid;
60 	obj->yst_gid = gid;
61 #endif
62 }
63 
64 void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c)
65 {
66 #ifdef CONFIG_YAFFS_WINCE
67 		yfsd_win_file_time_now(the_obj->win_atime);
68 		the_obj->win_ctime[0] = the_obj->win_mtime[0] =
69 		    the_obj->win_atime[0];
70 		the_obj->win_ctime[1] = the_obj->win_mtime[1] =
71 		    the_obj->win_atime[1];
72 
73 #else
74 
75 	obj->yst_mtime = Y_CURRENT_TIME;
76 	if (do_a)
77 		obj->yst_atime = obj->yst_atime;
78 	if (do_c)
79 		obj->yst_ctime = obj->yst_atime;
80 #endif
81 }
82 
83 loff_t yaffs_get_file_size(struct yaffs_obj *obj)
84 {
85 	YCHAR *alias = NULL;
86 	obj = yaffs_get_equivalent_obj(obj);
87 
88 	switch (obj->variant_type) {
89 	case YAFFS_OBJECT_TYPE_FILE:
90 		return obj->variant.file_variant.file_size;
91 	case YAFFS_OBJECT_TYPE_SYMLINK:
92 		alias = obj->variant.symlink_variant.alias;
93 		if (!alias)
94 			return 0;
95 		return yaffs_strnlen(alias, YAFFS_MAX_ALIAS_LENGTH);
96 	default:
97 		return 0;
98 	}
99 }
100 
101 int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
102 {
103 	unsigned int valid = attr->ia_valid;
104 
105 	if (valid & ATTR_MODE)
106 		obj->yst_mode = attr->ia_mode;
107 	if (valid & ATTR_UID)
108 		obj->yst_uid = attr->ia_uid;
109 	if (valid & ATTR_GID)
110 		obj->yst_gid = attr->ia_gid;
111 
112 	if (valid & ATTR_ATIME)
113 		obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
114 	if (valid & ATTR_CTIME)
115 		obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
116 	if (valid & ATTR_MTIME)
117 		obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
118 
119 	if (valid & ATTR_SIZE)
120 		yaffs_resize_file(obj, attr->ia_size);
121 
122 	yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
123 
124 	return YAFFS_OK;
125 
126 }
127 
128 int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
129 {
130 	unsigned int valid = 0;
131 
132 	attr->ia_mode = obj->yst_mode;
133 	valid |= ATTR_MODE;
134 	attr->ia_uid = obj->yst_uid;
135 	valid |= ATTR_UID;
136 	attr->ia_gid = obj->yst_gid;
137 	valid |= ATTR_GID;
138 
139 	Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
140 	valid |= ATTR_ATIME;
141 	Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
142 	valid |= ATTR_CTIME;
143 	Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
144 	valid |= ATTR_MTIME;
145 
146 	attr->ia_size = yaffs_get_file_size(obj);
147 	valid |= ATTR_SIZE;
148 
149 	attr->ia_valid = valid;
150 
151 	return YAFFS_OK;
152 }
153