Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / lib / ext2fs / link.c
1 /*
2  * link.c --- create links in a ext2fs directory
3  *
4  * Copyright (C) 1993, 1994 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18
19 #include "ext2_fs.h"
20 #include "ext2fs.h"
21
22 struct link_struct  {
23         ext2_filsys     fs;
24         const char      *name;
25         int             namelen;
26         ext2_ino_t      inode;
27         int             flags;
28         int             done;
29         unsigned int    blocksize;
30         errcode_t       err;
31         struct ext2_super_block *sb;
32 };
33
34 static int link_proc(struct ext2_dir_entry *dirent,
35                      int        offset,
36                      int        blocksize,
37                      char       *buf,
38                      void       *priv_data)
39 {
40         struct link_struct *ls = (struct link_struct *) priv_data;
41         struct ext2_dir_entry *next;
42         unsigned int rec_len, min_rec_len, curr_rec_len;
43         int ret = 0;
44         int csum_size = 0;
45
46         if (ls->done)
47                 return 0;
48
49         rec_len = EXT2_DIR_REC_LEN(ls->namelen);
50
51         ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len);
52         if (ls->err)
53                 return DIRENT_ABORT;
54
55         if (EXT2_HAS_RO_COMPAT_FEATURE(ls->fs->super,
56                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
57                 csum_size = sizeof(struct ext2_dir_entry_tail);
58         /*
59          * See if the following directory entry (if any) is unused;
60          * if so, absorb it into this one.
61          */
62         next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len);
63         if ((offset + (int) curr_rec_len < blocksize - (8 + csum_size)) &&
64             (next->inode == 0) &&
65             (offset + (int) curr_rec_len + (int) next->rec_len <= blocksize)) {
66                 curr_rec_len += next->rec_len;
67                 ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
68                 if (ls->err)
69                         return DIRENT_ABORT;
70                 ret = DIRENT_CHANGED;
71         }
72
73         /*
74          * If the directory entry is used, see if we can split the
75          * directory entry to make room for the new name.  If so,
76          * truncate it and return.
77          */
78         if (dirent->inode) {
79                 min_rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(dirent));
80                 if (curr_rec_len < (min_rec_len + rec_len))
81                         return ret;
82                 rec_len = curr_rec_len - min_rec_len;
83                 ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent);
84                 if (ls->err)
85                         return DIRENT_ABORT;
86                 next = (struct ext2_dir_entry *) (buf + offset +
87                                                   dirent->rec_len);
88                 next->inode = 0;
89                 ext2fs_dirent_set_name_len(next, 0);
90                 ext2fs_dirent_set_file_type(next, 0);
91                 ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next);
92                 if (ls->err)
93                         return DIRENT_ABORT;
94                 return DIRENT_CHANGED;
95         }
96
97         /*
98          * If we get this far, then the directory entry is not used.
99          * See if we can fit the request entry in.  If so, do it.
100          */
101         if (curr_rec_len < rec_len)
102                 return ret;
103         dirent->inode = ls->inode;
104         ext2fs_dirent_set_name_len(dirent, ls->namelen);
105         strncpy(dirent->name, ls->name, ls->namelen);
106         if (ls->sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
107                 ext2fs_dirent_set_file_type(dirent, ls->flags & 0x7);
108
109         ls->done++;
110         return DIRENT_ABORT|DIRENT_CHANGED;
111 }
112
113 /*
114  * Note: the low 3 bits of the flags field are used as the directory
115  * entry filetype.
116  */
117 #ifdef __TURBOC__
118  #pragma argsused
119 #endif
120 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
121                       ext2_ino_t ino, int flags)
122 {
123         errcode_t               retval;
124         struct link_struct      ls;
125         struct ext2_inode       inode;
126
127         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
128
129         if (!(fs->flags & EXT2_FLAG_RW))
130                 return EXT2_ET_RO_FILSYS;
131
132         ls.fs = fs;
133         ls.name = name;
134         ls.namelen = name ? strlen(name) : 0;
135         ls.inode = ino;
136         ls.flags = flags;
137         ls.done = 0;
138         ls.sb = fs->super;
139         ls.blocksize = fs->blocksize;
140         ls.err = 0;
141
142         retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
143                                     0, link_proc, &ls);
144         if (retval)
145                 return retval;
146         if (ls.err)
147                 return ls.err;
148
149         if (!ls.done)
150                 return EXT2_ET_DIR_NO_SPACE;
151
152         if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0)
153                 return retval;
154
155         if (inode.i_flags & EXT2_INDEX_FL) {
156                 inode.i_flags &= ~EXT2_INDEX_FL;
157                 if ((retval = ext2fs_write_inode(fs, dir, &inode)) != 0)
158                         return retval;
159         }
160
161         return 0;
162 }