blob: 895e36ef0f5d818f3a54c55cfcfc49c8b06b8bb2 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
2 * valid_blk.c --- does the inode have valid blocks?
3 *
4 * Copyright 1997 by Theodore Ts'o
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000010 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000013#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000017#include <string.h>
18#include <time.h>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000019
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000020#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000021#include "ext2fs.h"
22
23/*
24 * This function returns 1 if the inode's block entries actually
25 * contain block entries.
26 */
Theodore Ts'o0c80c442011-10-16 20:29:00 -040027int ext2fs_inode_has_valid_blocks2(ext2_filsys fs, struct ext2_inode *inode)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000028{
29 /*
30 * Only directories, regular files, and some symbolic links
31 * have valid block entries.
32 */
33 if (!LINUX_S_ISDIR(inode->i_mode) && !LINUX_S_ISREG(inode->i_mode) &&
34 !LINUX_S_ISLNK(inode->i_mode))
35 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040036
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000037 /*
38 * If the symbolic link is a "fast symlink", then the symlink
39 * target is stored in the block entries.
40 */
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040041 if (LINUX_S_ISLNK (inode->i_mode)) {
Theodore Ts'o0c80c442011-10-16 20:29:00 -040042 if (ext2fs_file_acl_block(fs, inode) == 0) {
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040043 /* With no EA block, we can rely on i_blocks */
44 if (inode->i_blocks == 0)
45 return 0;
46 } else {
47 /* With an EA block, life gets more tricky */
48 if (inode->i_size >= EXT2_N_BLOCKS*4)
49 return 1; /* definitely using i_block[] */
Theodore Ts'oee504122002-08-20 01:14:30 -040050 if (inode->i_size > 4 && inode->i_block[1] == 0)
51 return 1; /* definitely using i_block[] */
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040052 return 0; /* Probably a fast symlink */
53 }
54 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000055 return 1;
56}
Theodore Ts'o0c80c442011-10-16 20:29:00 -040057
58int ext2fs_inode_has_valid_blocks(struct ext2_inode *inode)
59{
60 return ext2fs_inode_has_valid_blocks2(NULL, inode);
61}