/** * @file tarfs_util.c * * Helper routines for tar parsing * * @author Jaroslav Drazan * @author Petr Cermak */ /* ************************************************************************* * * * * 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 2 of the License, or * * (at your option) any later version. * * * * ************************************************************************* */ #include "tarfs_util.h" #include "tarfs_common.h" /** * @brief Ceil value of nom / denom * * @param nom Nominator * @param denom Denominator * @return Ceil value of nom / denom */ unsigned long hcc(unsigned long nom, unsigned long denom) { if (nom == 0) { return 0; } return (nom - 1) / denom + 1; } /** * @brief Reads block from device * * Reads #i block from device specified in super block s * * @param s Super block * @param i No. of block * @param blok Allocated buffer in which to store data (size TAFS_BLKSIZE) * @return 1 - OK, 0 - error */ int read_block(struct super_block* s, unsigned long i, void* blok) { struct buffer_head* bh; bh = sb_bread(s, i); if (!bh) { return 0; } memcpy(blok, (void*) bh->b_data, TARFS_BLKSIZE); brelse(bh); return 1; } /** * @brief Checks if given block is full of zeroes * * @param blok Block of data to check * @param size Size of block * @return 1 - OK, 0 - some non-zero character found or error */ int is_nullblock(void *blok, unsigned long size) { unsigned long i; if(!blok) { return 0; } for(i = 0;i second_length + 1) { return 0;//osekne vetsinu pripadu } //porovnam vetsinu for(i = 0; i < second_length; i++) { if(aa[i] != bb[i]) { //ruzne return 0; } } //pokud jsou stejne, nebo a=b.'/', kde . je concatenace if (i == first_length || aa[first_length - 1] == '/') { return 1; } return 0; } /** * @brief Compares two paths (strings) * * sais, whether the dname is right prefix of fname. * It's equal to dname contains fname* * @param dname full path of directory (char*, but couldn't be NULL-terminated) * @param dsize size of dname * @param fname full file path (char*, but couldn't be NULL-terminated) * @param fsize size of fname * @return 1 - dname contains fname, 0 - dname is not ancestor of fname in dir tree * - can be changed by makro TARFS_CAN_ADD-1 - dname is ancestor, but not father * @see TARFS_CAN_ADD */ int is_right_prefix(char* dname, unsigned int dsize, char* fname, unsigned int fsize) { unsigned int i; if (dsize >= fsize) { return TARFS_CAN_ADD; } for (i = 0; i < dsize; i++) { if (dname[i] != fname[i]) { return TARFS_CAN_ADD; } } //vim, ze dname je predek fname, jestemusim overit, ze to je otec for(; i < fsize - 1; i++) {//do fsize-1, protoze, kdyz fname je adresar, pak ma na konci jmena / if (fname[i] == '/') { return -1; } } return 1; } /** * @brief Checks if given character is '0' - '7' * * @param ch Character * @return 0 - no, nonzero - yes */ int is_octal(char ch) { return(ch - '0' >= 0 && ch - '0' < 8); } /** * @brief Converts octal number into decimal integer * * Supposes that the number is in big endian format * * @param what Number in octal format (string) * @param length Length of what - string * @return Number in decimal format, TARFS_EVAL - error */ unsigned long oct_to_dec(void* what, unsigned int length) { unsigned long sum = 0; int i; if (length < 1) { return TARFS_EVAL; } for (i = 0; i < length; i++) { if (!is_octal(*(char*) what)) { return TARFS_EVAL; } //horner:-) sum = sum << 3; sum += ((char*)what)[i]-'0'; } return sum; } /** * @brief Returns length of filename stored in one block * * @param name Any zero-terminated string * @return min(length of name, 100) */ unsigned int short_name_size(char* name) { unsigned int i; for (i = 0; i < 100; i++) { if(!name[i]) { break; } } return i; }