[PATCH] hfs: set correct ctime
[linux-2.6/mini2440.git] / fs / hfsplus / catalog.c
blob04255af347091b1bae58b2a0ccd794ba15513cbe
1 /*
2 * linux/fs/hfsplus/catalog.c
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of catalog records
9 */
11 #include <linux/sched.h>
13 #include "hfsplus_fs.h"
14 #include "hfsplus_raw.h"
16 int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
17 const hfsplus_btree_key *k2)
19 __be32 k1p, k2p;
21 k1p = k1->cat.parent;
22 k2p = k2->cat.parent;
23 if (k1p != k2p)
24 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
26 return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name);
29 int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
30 const hfsplus_btree_key *k2)
32 __be32 k1p, k2p;
34 k1p = k1->cat.parent;
35 k2p = k2->cat.parent;
36 if (k1p != k2p)
37 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
39 return hfsplus_strcmp(&k1->cat.name, &k2->cat.name);
42 void hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
43 u32 parent, struct qstr *str)
45 int len;
47 key->cat.parent = cpu_to_be32(parent);
48 if (str) {
49 hfsplus_asc2uni(sb, &key->cat.name, str->name, str->len);
50 len = be16_to_cpu(key->cat.name.length);
51 } else {
52 key->cat.name.length = 0;
53 len = 0;
55 key->key_len = cpu_to_be16(6 + 2 * len);
58 static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
59 struct hfsplus_unistr *name)
61 int ustrlen;
63 ustrlen = be16_to_cpu(name->length);
64 key->cat.parent = cpu_to_be32(parent);
65 key->cat.name.length = cpu_to_be16(ustrlen);
66 ustrlen *= 2;
67 memcpy(key->cat.name.unicode, name->unicode, ustrlen);
68 key->key_len = cpu_to_be16(6 + ustrlen);
71 static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
73 if (inode->i_flags & S_IMMUTABLE)
74 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
75 else
76 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
77 if (inode->i_flags & S_APPEND)
78 perms->rootflags |= HFSPLUS_FLG_APPEND;
79 else
80 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
81 HFSPLUS_I(inode).rootflags = perms->rootflags;
82 HFSPLUS_I(inode).userflags = perms->userflags;
83 perms->mode = cpu_to_be16(inode->i_mode);
84 perms->owner = cpu_to_be32(inode->i_uid);
85 perms->group = cpu_to_be32(inode->i_gid);
88 static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
90 if (S_ISDIR(inode->i_mode)) {
91 struct hfsplus_cat_folder *folder;
93 folder = &entry->folder;
94 memset(folder, 0, sizeof(*folder));
95 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
96 folder->id = cpu_to_be32(inode->i_ino);
97 HFSPLUS_I(inode).create_date =
98 folder->create_date =
99 folder->content_mod_date =
100 folder->attribute_mod_date =
101 folder->access_date = hfsp_now2mt();
102 hfsplus_set_perms(inode, &folder->permissions);
103 if (inode == HFSPLUS_SB(inode->i_sb).hidden_dir)
104 /* invisible and namelocked */
105 folder->user_info.frFlags = cpu_to_be16(0x5000);
106 return sizeof(*folder);
107 } else {
108 struct hfsplus_cat_file *file;
110 file = &entry->file;
111 memset(file, 0, sizeof(*file));
112 file->type = cpu_to_be16(HFSPLUS_FILE);
113 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
114 file->id = cpu_to_be32(cnid);
115 HFSPLUS_I(inode).create_date =
116 file->create_date =
117 file->content_mod_date =
118 file->attribute_mod_date =
119 file->access_date = hfsp_now2mt();
120 if (cnid == inode->i_ino) {
121 hfsplus_set_perms(inode, &file->permissions);
122 file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
123 file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
124 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
125 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
126 } else {
127 file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
128 file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
129 file->user_info.fdFlags = cpu_to_be16(0x100);
130 file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
132 return sizeof(*file);
136 static int hfsplus_fill_cat_thread(struct super_block *sb,
137 hfsplus_cat_entry *entry, int type,
138 u32 parentid, struct qstr *str)
140 entry->type = cpu_to_be16(type);
141 entry->thread.reserved = 0;
142 entry->thread.parentID = cpu_to_be32(parentid);
143 hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
144 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
147 /* Try to get a catalog entry for given catalog id */
148 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
149 struct hfs_find_data *fd)
151 hfsplus_cat_entry tmp;
152 int err;
153 u16 type;
155 hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
156 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
157 if (err)
158 return err;
160 type = be16_to_cpu(tmp.type);
161 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
162 printk(KERN_ERR "hfs: found bad thread record in catalog\n");
163 return -EIO;
166 hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
167 &tmp.thread.nodeName);
168 return hfs_brec_find(fd);
171 int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
173 struct hfs_find_data fd;
174 struct super_block *sb;
175 hfsplus_cat_entry entry;
176 int entry_size;
177 int err;
179 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
180 sb = dir->i_sb;
181 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
183 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
184 entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
185 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
186 dir->i_ino, str);
187 err = hfs_brec_find(&fd);
188 if (err != -ENOENT) {
189 if (!err)
190 err = -EEXIST;
191 goto err2;
193 err = hfs_brec_insert(&fd, &entry, entry_size);
194 if (err)
195 goto err2;
197 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
198 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
199 err = hfs_brec_find(&fd);
200 if (err != -ENOENT) {
201 /* panic? */
202 if (!err)
203 err = -EEXIST;
204 goto err1;
206 err = hfs_brec_insert(&fd, &entry, entry_size);
207 if (err)
208 goto err1;
210 dir->i_size++;
211 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
212 mark_inode_dirty(dir);
213 hfs_find_exit(&fd);
214 return 0;
216 err1:
217 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
218 if (!hfs_brec_find(&fd))
219 hfs_brec_remove(&fd);
220 err2:
221 hfs_find_exit(&fd);
222 return err;
225 int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
227 struct super_block *sb;
228 struct hfs_find_data fd;
229 struct hfsplus_fork_raw fork;
230 struct list_head *pos;
231 int err, off;
232 u16 type;
234 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
235 sb = dir->i_sb;
236 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
238 if (!str) {
239 int len;
241 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
242 err = hfs_brec_find(&fd);
243 if (err)
244 goto out;
246 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
247 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
248 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
249 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
250 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
251 fd.search_key->key_len = cpu_to_be16(6 + len);
252 } else
253 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
255 err = hfs_brec_find(&fd);
256 if (err)
257 goto out;
259 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
260 if (type == HFSPLUS_FILE) {
261 #if 0
262 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
263 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
264 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
265 #endif
267 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
268 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
269 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
272 list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
273 struct hfsplus_readdir_data *rd =
274 list_entry(pos, struct hfsplus_readdir_data, list);
275 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
276 rd->file->f_pos--;
279 err = hfs_brec_remove(&fd);
280 if (err)
281 goto out;
283 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
284 err = hfs_brec_find(&fd);
285 if (err)
286 goto out;
288 err = hfs_brec_remove(&fd);
289 if (err)
290 goto out;
292 dir->i_size--;
293 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
294 mark_inode_dirty(dir);
295 out:
296 hfs_find_exit(&fd);
298 return err;
301 int hfsplus_rename_cat(u32 cnid,
302 struct inode *src_dir, struct qstr *src_name,
303 struct inode *dst_dir, struct qstr *dst_name)
305 struct super_block *sb;
306 struct hfs_find_data src_fd, dst_fd;
307 hfsplus_cat_entry entry;
308 int entry_size, type;
309 int err = 0;
311 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
312 dst_dir->i_ino, dst_name->name);
313 sb = src_dir->i_sb;
314 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
315 dst_fd = src_fd;
317 /* find the old dir entry and read the data */
318 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
319 err = hfs_brec_find(&src_fd);
320 if (err)
321 goto out;
323 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
324 src_fd.entrylength);
326 /* create new dir entry with the data from the old entry */
327 hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
328 err = hfs_brec_find(&dst_fd);
329 if (err != -ENOENT) {
330 if (!err)
331 err = -EEXIST;
332 goto out;
335 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
336 if (err)
337 goto out;
338 dst_dir->i_size++;
339 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
340 mark_inode_dirty(dst_dir);
342 /* finally remove the old entry */
343 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
344 err = hfs_brec_find(&src_fd);
345 if (err)
346 goto out;
347 err = hfs_brec_remove(&src_fd);
348 if (err)
349 goto out;
350 src_dir->i_size--;
351 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
352 mark_inode_dirty(src_dir);
354 /* remove old thread entry */
355 hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
356 err = hfs_brec_find(&src_fd);
357 if (err)
358 goto out;
359 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
360 err = hfs_brec_remove(&src_fd);
361 if (err)
362 goto out;
364 /* create new thread entry */
365 hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
366 entry_size = hfsplus_fill_cat_thread(sb, &entry, type, dst_dir->i_ino, dst_name);
367 err = hfs_brec_find(&dst_fd);
368 if (err != -ENOENT) {
369 if (!err)
370 err = -EEXIST;
371 goto out;
373 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
374 out:
375 hfs_bnode_put(dst_fd.bnode);
376 hfs_find_exit(&src_fd);
377 return err;