blob: 32ac9e602e01d915b9605e126caae677db9acd82 [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
12#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000013#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000014#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000016#include <string.h>
17#include <time.h>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000020#include "ext2fs.h"
21
22/*
23 * This function returns 1 if the inode's block entries actually
24 * contain block entries.
25 */
JP Abgrall65f0aab2014-03-06 13:50:20 -080026int ext2fs_inode_has_valid_blocks(struct ext2_inode *inode)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000027{
28 /*
29 * Only directories, regular files, and some symbolic links
30 * have valid block entries.
31 */
32 if (!LINUX_S_ISDIR(inode->i_mode) && !LINUX_S_ISREG(inode->i_mode) &&
33 !LINUX_S_ISLNK(inode->i_mode))
34 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040035
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000036 /*
37 * If the symbolic link is a "fast symlink", then the symlink
38 * target is stored in the block entries.
39 */
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040040 if (LINUX_S_ISLNK (inode->i_mode)) {
JP Abgrall65f0aab2014-03-06 13:50:20 -080041 if (inode->i_file_acl == 0) {
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040042 /* With no EA block, we can rely on i_blocks */
43 if (inode->i_blocks == 0)
44 return 0;
45 } else {
46 /* With an EA block, life gets more tricky */
47 if (inode->i_size >= EXT2_N_BLOCKS*4)
48 return 1; /* definitely using i_block[] */
Theodore Ts'oee504122002-08-20 01:14:30 -040049 if (inode->i_size > 4 && inode->i_block[1] == 0)
50 return 1; /* definitely using i_block[] */
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040051 return 0; /* Probably a fast symlink */
52 }
53 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000054 return 1;
55}