📄 fat.lst
字号:
756 *----------------------------------------------------------------------------
757 * PURPOSE:
758 * Go to the subdirectory
759 *----------------------------------------------------------------------------
760 * EXAMPLE:
761 *----------------------------------------------------------------------------
762 * NOTE:
763 * Only go to subdirectory if current file is a directory
764 *----------------------------------------------------------------------------
765 * REQUIREMENTS:
766 *****************************************************************************/
767 bit fat_goto_subdir (Byte id)
768 {
769 1 /* check if current file is a directory */
770 1 if ((fat_cache.info.attributes & ATTR_DIRECTORY) == ATTR_DIRECTORY)
771 1 {
772 2 /* computes the sector address (RELATIVE) */
773 2 /* check also if we go to the root dir or not */
774 2 if (fat_cache.info.start_cluster != 0)
775 2 {
776 3 dir_is_root = FALSE; /* not the root dir */
777 3 fat_get_clusters(&dir_clusters); /* get directory allocation table */
778 3
779 3 /* initialize fat pointers */
780 3 fat_current_dir_fragment = 1;
781 3 fat_current_dir_cluster = 0;
782 3 fat_current_sector_cluster = 0;
783 3 fat_current_byte_counter = 0;
784 3
785 3 /* computes sector number from allocation table */
786 3 fat_current_sector = (((Uint32)(dir_clusters.cluster[0])) *
787 3 fat_cluster_size) + (fat_ptr_data - fat_ptr_rdir);
788 3 }
789 2 else
790 2 {
791 3 return fat_get_root_directory(id); /* goto root directory */
792 3 }
793 2
794 2 /* set the ABSOLUTE sector address */
795 2 fat_current_sector += fat_ptr_rdir;
796 2
797 2 fat_get_directory_chain(id);
798 2
C51 COMPILER V6.20c FAT 07/10/2002 15:17:46 PAGE 14
799 2 fat_file_ptr = 1;
800 2 /* now we are at the beginning of directory */
801 2 if (fat_dseek((Int16)(fat_directory_chain[fat_file_ptr] * 32)) == OK)
802 2 {
803 3 /* update parent info in ".." entry*/
804 3 fat_fetch_directory_info(&fat_cache.parent);
805 3
806 3 /* point next file */
807 3 return fat_goto_next();
808 3 }
809 2 else
810 2 {
811 3 return KO;
812 3 }
813 2 }
814 1 }
815
816
817 /*F**************************************************************************
818 * NAME: fat_goto_parentdir
819 *----------------------------------------------------------------------------
820 * PARAMS:
821 * id: file extension to select
822 *
823 * return:
824 *----------------------------------------------------------------------------
825 * PURPOSE:
826 * Go to the parent directory
827 *----------------------------------------------------------------------------
828 * EXAMPLE:
829 *----------------------------------------------------------------------------
830 * NOTE:
831 *----------------------------------------------------------------------------
832 * REQUIREMENTS:
833 *****************************************************************************/
834 bit fat_goto_parentdir (Byte id)
835 {
836 1 /* goto the parent directory */
837 1 fat_cache.info = fat_cache.parent;
838 1
839 1 /* issue the equivalent to a cd .. DOS command */
840 1 return (fat_goto_subdir(id));
841 1 }
842
843
844 /*F**************************************************************************
845 * NAME: fat_fopen
846 *----------------------------------------------------------------------------
847 * PARAMS:
848 * mode: READ: open file for read
849 * WRITE: open file for write
850 *
851 * return:
852 * - OK: file opened
853 * - KO: file not opened: - file is empty
854 * - file is too much fragmented
855 *----------------------------------------------------------------------------
856 * PURPOSE:
857 * Open the file in read or write mode
858 *----------------------------------------------------------------------------
859 * EXAMPLE:
860 *----------------------------------------------------------------------------
C51 COMPILER V6.20c FAT 07/10/2002 15:17:46 PAGE 15
861 * NOTE:
862 *----------------------------------------------------------------------------
863 * REQUIREMENTS:
864 *****************************************************************************/
865 bit fat_fopen (bit mode)
866 {
867 1 if (fat_cache.info.size == 0)
868 1 {
869 2 return KO; /* file empty */
870 2 }
871 1 else
872 1 {
873 2 /* fat file byte counter */
874 2 fat_file_byte_counter = 0;
875 2
876 2 /* reset the allocation list variable */
877 2 fat_chain_index = 0;
878 2 fat_chain_cluster = 0;
879 2 if (fat_get_clusters(&clusters) != OK)
880 2 {
881 3 return KO; /* file too much fragmented */
882 3 }
883 2
884 2 /* seek to the beginning of the file */
885 2 if (mode == READ)
886 2 {
887 3 fat_open_mode = READ;
888 3 return Hard_read_open(fat_ptr_data + ((Uint32)(clusters.cluster[0])
889 3 * fat_cluster_size));
890 3 }
891 2 else
892 2 {
893 3 fat_open_mode = WRITE;
894 3 return Hard_write_open(fat_ptr_data + ((Uint32)(clusters.cluster[0])
895 3 * fat_cluster_size));
896 3 }
897 2 }
898 1 }
899
900
901 /*F**************************************************************************
902 * NAME: fat_fclose
903 *----------------------------------------------------------------------------
904 * PARAMS:
905 *
906 * return:
907 *----------------------------------------------------------------------------
908 * PURPOSE:
909 * Close opened file
910 *----------------------------------------------------------------------------
911 * EXAMPLE:
912 *----------------------------------------------------------------------------
913 * NOTE:
914 *----------------------------------------------------------------------------
915 * REQUIREMENTS:
916 *****************************************************************************/
917 void fat_fclose (void)
918 {
919 1 if (fat_open_mode == READ)
920 1 {
921 2 Hard_read_close(); /* close reading */
922 2 }
C51 COMPILER V6.20c FAT 07/10/2002 15:17:46 PAGE 16
923 1 else
924 1 {
925 2 Hard_write_close(); /* close writing */
926 2 }
927 1 }
928
929
930 /*F**************************************************************************
931 * NAME: fat_get_clusters
932 *----------------------------------------------------------------------------
933 * PARAMS:
934 * chain: allocation list address
935 *
936 * return:
937 * - OK: allocation done
938 * - KO: allocation not done: file too much fragmented
939 *----------------------------------------------------------------------------
940 * PURPOSE:
941 * Prepare a list of all the file clusters built as two tables:
942 * cluster[n] contains the staring cluster number of a fragment
943 * number[n] contains the number of contiguous clusters in fragment
944 * This way implies that file is not be too much fragmented
945 * The number of possible fragment is defined by the constant
946 * MAX_FILE_FRAGMENT_NUMBER in fat.h header file
947 *----------------------------------------------------------------------------
948 * EXAMPLE:
949 *----------------------------------------------------------------------------
950 * NOTE:
951 *----------------------------------------------------------------------------
952 * REQUIREMENTS:
953 *****************************************************************************/
954 bit fat_get_clusters (fat_cluster_chain *chain)
955 {
956 1 idata Uint16 new_cluster;
957 1 idata Uint16 old_cluster;
958 1 idata Byte index; /* index in chain */
959 1 idata Uint16 i;
960 1 idata Uint16 fat12_cluster;
961 1 bdata bit fat12_parity;
962 1
963 1 /* build the first entry of the allocation list */
964 1 chain->number[0] = 1;
965 1 chain->cluster[0] = fat_cache.info.start_cluster - 2; /* 2 = 1st cluster */
966 1 old_cluster = fat_cache.info.start_cluster;
967 1 index = 0;
968 1
969 1
970 1 /* calculate the first offset in fat and read the corresponding sector */
971 1 if (fat_is_fat16)
972 1 Hard_read_open(fat_ptr_fats + (old_cluster / (SECTOR_SIZE / 2)));
973 1 else
974 1 Hard_read_open(fat_ptr_fats);
975 1
976 1 if (fat_is_fat16) /* FAT16 management */
977 1 { /* compute offset in sector */
978 2 for (i = (old_cluster % (SECTOR_SIZE / 2)); i != 0; i--)
979 2 {
980 3 Hard_read_byte(); /* dummy FAT read */
981 3 Hard_read_byte();
982 3 }
983 2 /* read first entry */
984 2 ((Byte *)&new_cluster)[1]= Hard_read_byte();
C51 COMPILER V6.20c FAT 07/10/2002 15:17:46 PAGE 17
985 2 ((Byte *)&new_cluster)[0]= Hard_read_byte();
986 2
987 2 while (new_cluster != LAST_CLUSTER) /* loop until last cluster found */
988 2 {
989 3 if ((new_cluster == (old_cluster + 1)) && (chain->number[index] != 255))
990 3 { /* contiguous cluster */
991 4 chain->number[index]++;
992 4 }
993 3 else
994 3 { /* compute fragmentation */
995 4 index++;
996 4 chain->number[index] = 1;
997 4 chain->cluster[index] = new_cluster - 2; /* 2 = 1st cluster */
998 4 for (i = new_cluster - old_cluster - 1; i != 0; i--)
999 4 {
1000 5 Hard_read_byte(); /* dummy FAT read */
1001 5 Hard_read_byte();
1002 5 }
1003 4 }
1004 3 old_cluster = new_cluster;
1005 3 if (index == MAX_FILE_FRAGMENT_NUMBER - 2)
1006 3 {
1007 4 return KO; /* file too much fragmented */
1008 4 }
1009 3 else
1010 3 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -