119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project/*
219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * valid_blk.c --- does the inode have valid blocks?
319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project *
419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * Copyright 1997 by Theodore Ts'o
53984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt *
619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * %Begin-Header%
78558eab78390d1924cd6b255686ceef133f294d5Ken Sumrall * This file may be redistributed under the terms of the GNU Library
88558eab78390d1924cd6b255686ceef133f294d5Ken Sumrall * General Public License, version 2.
919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * %End-Header%
1019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project */
1119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
1219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdio.h>
1319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#if HAVE_UNISTD_H
1419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <unistd.h>
1519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif
1619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <string.h>
1719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <time.h>
1819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
1919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include "ext2_fs.h"
2019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include "ext2fs.h"
2119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
2219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project/*
2319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * This function returns 1 if the inode's block entries actually
2419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * contain block entries.
2519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project */
2619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectint ext2fs_inode_has_valid_blocks(struct ext2_inode *inode)
2719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{
2819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	/*
2919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	 * Only directories, regular files, and some symbolic links
3019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	 * have valid block entries.
3119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	 */
3219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (!LINUX_S_ISDIR(inode->i_mode) && !LINUX_S_ISREG(inode->i_mode) &&
3319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	    !LINUX_S_ISLNK(inode->i_mode))
3419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		return 0;
353984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	/*
3719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	 * If the symbolic link is a "fast symlink", then the symlink
3819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	 * target is stored in the block entries.
3919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	 */
4019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (LINUX_S_ISLNK (inode->i_mode)) {
4119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		if (inode->i_file_acl == 0) {
4219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project			/* With no EA block, we can rely on i_blocks */
4319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project			if (inode->i_blocks == 0)
4419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project				return 0;
4519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		} else {
4619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project			/* With an EA block, life gets more tricky */
4719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project			if (inode->i_size >= EXT2_N_BLOCKS*4)
4819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project				return 1; /* definitely using i_block[] */
4919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project			if (inode->i_size > 4 && inode->i_block[1] == 0)
5019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project				return 1; /* definitely using i_block[] */
5119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project			return 0; /* Probably a fast symlink */
5219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		}
5319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
5419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	return 1;
5519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project}
56