/*
 * Copyright (c) 2001-2002  Ben Fennema
 * Copyright (c) 2016-2019  Pali Rohár <pali.rohar@gmail.com>
 * All rights reserved.
 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. *
 *
 * Author:     zhangsheng<zhangsheng@uniontech.com>
 *
 * Maintainer: max-lv<lvwujun@uniontech.com>
 *             xushitong<xushitong@uniontech.com>
 *             dengkeyun<dengkeyun@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef FILE_H
#define FILE_H

#include "udffs.h"

tag query_tag(udf_image *, udf_extent *, udf_desc *, uint16_t);
extern void path_to_uname(char *, dchars *, uint8_t *);
extern tag udf_query_tag(udf_image *, uint16_t, uint16_t, uint32_t, udf_data *, uint32_t, uint32_t);
extern int udf_alloc_blocks(udf_image *, udf_extent *, uint32_t, uint32_t);
extern udf_desc *udf_create(udf_image *, udf_extent *, const dchars *, uint8_t, uint32_t, udf_desc *, uint8_t, uint8_t, uint16_t);
extern udf_desc *udf_mkdir(udf_image *,  udf_extent *, const dchars *, uint8_t, uint32_t, udf_desc *);
extern void insert_fid(udf_image *, udf_extent *, udf_desc *, udf_desc *, const dchars *, uint8_t, uint8_t);

extern void pre_insert_fid(udf_image *, udf_extent *, udf_desc *, udf_file *, uint8_t);
extern void copyFile(udf_image *,  udf_extent *, udf_file *, struct stat *, udf_desc *);
extern void copyDirectory(udf_image *,  udf_extent *, udf_file *, udf_desc *);
extern void linkFile(udf_image *,  udf_extent *, udf_file *, struct fileEntry *, udf_desc *);
extern udf_desc *linkDirectory(udf_image *,  udf_extent *, udf_file *, udf_desc *);
extern void cpdir(udf_image *, udf_extent *, udf_desc *, size_t, char **);

static inline void clear_bits(uint8_t *bitmap, uint32_t offset, uint64_t length)
{
    for (;length>0;length--)
    {
        bitmap[(length+offset-1)/8] &= ~(1 << ((offset+length-1)%8));
    }
}

static inline struct logicalVolIntegrityDescImpUse *query_lvidiu(udf_image *img)
{
    return (struct logicalVolIntegrityDescImpUse *)&(img->udf_lvid->data[le32_to_cpu(img->udf_lvd[0]->numPartitionMaps) * 2 * sizeof(uint32_t)]);
}

#endif // FILE_H
