blob: 63b11b45072d7cbd4d72546aee3253ea84dd96b4 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002 * link.c --- create links in a ext2fs directory
Theodore Ts'o3839e651997-04-26 13:21:57 +00003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <stdlib.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000018
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <linux/ext2_fs.h>
20
21#include "ext2fs.h"
22
23struct link_struct {
24 const char *name;
25 int namelen;
26 ino_t inode;
27 int flags;
28 int done;
29};
30
31static int link_proc(struct ext2_dir_entry *dirent,
32 int offset,
33 int blocksize,
34 char *buf,
35 void *private)
36{
37 struct link_struct *ls = (struct link_struct *) private;
38 struct ext2_dir_entry *next;
39 int rec_len;
40 int ret = 0;
41
42 rec_len = EXT2_DIR_REC_LEN(ls->namelen);
43
44 /*
45 * See if the following directory entry (if any) is unused;
46 * if so, absorb it into this one.
47 */
48 next = (struct ext2_dir_entry *) (buf + offset + dirent->rec_len);
49 if ((offset + dirent->rec_len < blocksize - 8) &&
50 (next->inode == 0) &&
51 (offset + dirent->rec_len + next->rec_len <= blocksize)) {
52 dirent->rec_len += next->rec_len;
53 ret = DIRENT_CHANGED;
54 }
55
56 /*
57 * If the directory entry is used, see if we can split the
58 * directory entry to make room for the new name. If so,
59 * truncate it and return.
60 */
61 if (dirent->inode) {
62 if (dirent->rec_len < (EXT2_DIR_REC_LEN(dirent->name_len) +
63 rec_len))
64 return ret;
65 rec_len = dirent->rec_len - EXT2_DIR_REC_LEN(dirent->name_len);
66 dirent->rec_len = EXT2_DIR_REC_LEN(dirent->name_len);
67 next = (struct ext2_dir_entry *) (buf + offset +
68 dirent->rec_len);
69 next->inode = 0;
70 next->name_len = 0;
71 next->rec_len = rec_len;
72 return DIRENT_CHANGED;
73 }
74
75 /*
76 * If we get this far, then the directory entry is not used.
77 * See if we can fit the request entry in. If so, do it.
78 */
79 if (dirent->rec_len < rec_len)
80 return ret;
81 dirent->inode = ls->inode;
82 dirent->name_len = ls->namelen;
83 strncpy(dirent->name, ls->name, ls->namelen);
84
85 ls->done++;
86 return DIRENT_ABORT|DIRENT_CHANGED;
87}
88
89errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name, ino_t ino,
90 int flags)
91{
92 errcode_t retval;
93 struct link_struct ls;
94
Theodore Ts'of3db3561997-04-26 13:34:30 +000095 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
96
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 if (!(fs->flags & EXT2_FLAG_RW))
98 return EXT2_ET_RO_FILSYS;
99
100 ls.name = name;
101 ls.namelen = name ? strlen(name) : 0;
102 ls.inode = ino;
103 ls.flags = 0;
104 ls.done = 0;
105
106 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
107 0, link_proc, &ls);
108 if (retval)
109 return retval;
110
111 return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;
112}