Embodiment
In order to improve the efficiency of management of smart card document system and the utilization factor to smart cards for storage space, the embodiment of the present invention provides a kind of management method of smart card document system.
Below in conjunction with accompanying drawing, the preferred embodiment of the present invention is elaborated.
As shown in Figure 1, in the embodiment of the present invention, as follows to the method detailed flow process of smart card document system management:
Step 101: receive Application Protocol Data Unit (APDU) instruction that the file system of smart card is operated.
In the present embodiment, the operation that file system is carried out can be at least to create file or delete file operation.
Step 102: the pieces using the storage space of the difference size preset as different sizes, determine whether to exist according to the size of the pending file carrying in APDU instruction the piece mating with the size of pending file, if exist, process pending file according to the piece of this coupling; If do not exist, determine the combination of the piece of the different sizes that match from the size of pending file, according to the pending file of the combined treatment of the piece of this difference size.
Preferably, in the piece of different sizes, the size of each is a doubly byte of index of 2.In practical application, can determine according to the size of file minimum in file system the size of smallest blocks in different big or small pieces, and make it to meet a doubly byte of index of 2.For example, the piece of default difference size is divided into 6 grades, and is respectively 32 bytes, 64 bytes, 128 bytes, 256 bytes, 512 bytes and 1024 bytes.
In the present embodiment, default fragment recording areas in file system, with start address and the chip size information of each fragment in log file system.
Wherein, while creating pending file according to the piece mating with pending file, judge whether the fragment that exists the piece that mates with this to match, if existence, the fragment of selecting the piece mating with this to match is preserved pending file, and fragment can be divided into the piece of different sizes; If do not exist, further judge whether unappropriated continuous space in file system can hold the piece of this coupling, if energy is pending file allocation storage space according to the piece of this coupling and preserves pending file, if can not, file system is carried out to defragmentation.
Or, while creating pending file according to the combination of the piece of the different sizes of mating from pending file, according to the each fragment record in fragment recording areas, judge whether the fragment that existence matches from the combination of the piece of different sizes, if exist, selection is preserved pending file from the fragment that the combination of the piece of different sizes matches, and fragment can be divided into the pieces of different sizes; If do not exist, further judge whether unappropriated continuous space in file system can hold the combination of the pieces of different sizes, if can, being combined as pending file allocation storage space and preserving pending file according to different big or small pieces, if can not, file system is carried out to defragmentation.
For example, as shown in Figure 2, the detailed process that creates file is as follows:
Step 201: create file instruction and import into by the establishment document entry of file system;
Step 202: check the legitimacy of instruction, arrange and determine the structure (as file header) of file to be created;
Step 203: determine the combination of the piece of the piece mating or the difference size matching according to the size of file to be created, judge whether the total surplus space of file system can hold the combination of the piece of this coupling or the piece of this difference size, this total surplus space comprises unappropriated continuous space and fragment space, if can not, execution step 204, otherwise, execution step 205;
Step 204: carry out error handling processing;
Step 205: search fragment recording areas, judge whether to exist the fragment that can match from the combination of the piece of the piece of above-mentioned coupling or different sizes, if exist, perform step 206, otherwise, execution step 207;
Step 206: determine the fragment that can match from the combination of the piece of the piece of above-mentioned coupling or above-mentioned different sizes, and file to be created is deposited to this fragment, revise fragment recording areas simultaneously, go to step 210;
Step 207: judge whether unappropriated continuous space in file system can hold the combination of the piece of above-mentioned coupling or the pieces of different sizes, if so, goes to step 209, otherwise, go to step 208;
Step 208: the fragment in file system is arranged, fragment is incorporated into unappropriated continuous space;
Step 209: the combination according to the piece of the piece of above-mentioned coupling or different sizes is saved to file to be created in the unappropriated continuous space of file system;
Step 210: complete the file process that creates.
Wherein, in the time creating pending file, preserve pending file if select the fragment matching from the combination of the piece of above-mentioned coupling or the piece of different sizes, behind the Coutinuous store space taking in fragment from the corresponding size of combination of the piece of above-mentioned coupling or the piece of different sizes, if the size of the remaining space of this fragment is the one in different big or small pieces, or, the size of the remaining space of this fragment is greater than maximum one in different big or small pieces,, for the remaining space of fragment, in fragment recording areas, add corresponding fragment record; Otherwise, the remaining space of fragment is split into the pieces of each different sizes, and each of obtaining for fractionation adds corresponding fragment record in fragment recording areas.
Preferably, while determining the combination of the piece of the required above-mentioned coupling taking of file to be created or the piece of different sizes, to waste minimum space as standard, and in the combination of the piece of different sizes, the storage space at each piece place is continuous.For example, the size of file to be created is 76 bytes, needs to take the piece of 64 bytes and 32 bytes.
Again for example, in current fragment, there is the fragment of 32 bytes and the fragment of 256 bytes, as the file of 63 byte-sized of need establishment, need the fragment of 256 bytes to be split as the piece of 128 bytes and the piece of two 64 bytes, the piece preservation that takies one of them 64 byte needs to create file, and the piece of the piece of another 64 byte and 128 bytes is saved to fragment recording areas as two fragments records.
Wherein, during according to the pending file of block delete of above-mentioned coupling, the pointer of revised file system context association, by the address pointed pointer of the previous file being associated with pending file, be revised as the address of the pointer of pending file file pointed, the shared storage space of piece of above-mentioned coupling becomes fragment, and in fragment recording areas, adds corresponding fragment record.Or, while deleting pending file according to the combination of the piece of above-mentioned different sizes, the pointer of revised file system context association, by the address pointed pointer of the previous file being associated with pending file, be revised as the address of the pointer of pending file file pointed, the shared storage space of combination of the piece of above-mentioned different sizes becomes fragment, and in fragment recording areas, adds corresponding fragment record.
Wherein, deleting pending file, and the shared storage space of the combination of the piece of the piece matching from pending file or the different sizes that match becomes after fragment, for each in the combination of the piece of above-mentioned coupling or the piece of different sizes, add corresponding fragment record in fragment recording areas.
In the present embodiment, add corresponding fragment record for each in fragment recording areas, be specially: all fragment records of traversal fragment recording areas, record start address and the size information of corresponding the first fragment according to fragment to be added, when determining while there is storage space and adjacent the second fragment of the first fragment, judge that whether space size after the first fragment and the second fragment merge is any one in Bu Tong big or small piece, if, revise start address and the size information of the second fragment fragment record, so that fragment record to be added and the fragment record of the second fragment are merged, otherwise, further judge whether the space size after the first fragment and the second fragment merge is greater than one maximum in Bu Tong big or small piece, if, the fragment record of corresponding the first fragment fragment record to be added and the second fragment is merged into and is absorbed piece fragment record, and by with the fragment record merging that absorbs piece fragment and record corresponding fragment and can form all fragments in Coutinuous store space, if determine and do not have the fragment adjacent with the first fragment, or the space size after the first fragment merges from adjacent the second fragment is not any in the piece of different sizes, and be not more than in the pieces of different sizes maximum when a kind of, whether the remaining space that judges fragment recording areas can add new fragment record, if can, add the fragment record of the first fragment, if can not, file system is carried out to defragmentation.Wherein, size is greater than to the default maximum piece of a kind of and is defined as absorption piece.For example, piece maximum in the piece of default difference size is 256 bytes, if there is the fragment record of the piece of 256 bytes, and the fragment record of the piece of 64 bytes, to record corresponding piece continuous at storage space for these two fragments, can merge into absorption piece, be about to two fragment records and merge.
Again for example, the file that needs deleted file to be combined into by the piece of 32 bytes and the piece of 64 bytes, after file is deleted, in the time that fragment recording areas interpolation fragment records, judge whether on storage space continuous with deleted file, and the fragment record that can merge with the piece of the piece of 32 bytes or 64 bytes, if be present in the piece of 32 bytes at the fragment record of continuous 32 block of bytes of storage space, these two fragment records are merged into the fragment record corresponding to piece of 64 bytes, simultaneously, the piece that merges 64 bytes that the fragment record that obtains 64 block of bytes forms with deleted file is again continuous at storage space, and can merge the piece that obtains 128 bytes, merge into the fragment record of the piece of 128 bytes, if do not add respectively and two corresponding fragment records of piece in fragment recording areas.
In practical application, can be also, while reaching predetermined threshold in the occupied space of fragment recording areas, file system to be carried out to defragmentation operation.Wherein, in fragment recording areas, occupied space reaches setting threshold, can be that the occupied space of fragment recording areas reaches setting threshold with respect to the ratio in whole fragment recording areas space.
In practical application, add after fragment record in fragment recording areas at every turn, can calculate the shared space of all fragments in file system according to the fragment record of fragment recording areas, and judge that whether the shared space of these all fragments reaches setting threshold with respect to the ratio of whole file system space, triggers defragmentation if reach setting threshold.For the trigger condition of other forms of triggering defragmentation, can be applied to the embodiment of the present invention, the present invention is also included.
For example, as shown in Figure 3, the detailed process of deleted file is as follows:
Step 301: deleted file instruction is imported into by the deleted file entrance of file system;
Step 302: the pointer of revised file system context association, by the address pointed pointer of the previous file being associated with file to be deleted, is revised as the address of the pointer of file to be deleted file pointed;
Step 303: the shared storage space of combination of the piece of the piece matching from file to be deleted or the different sizes that match becomes fragment, and in fragment recording areas, add and each corresponding fragment record in the combination of the piece of above-mentioned coupling or the piece of different sizes, and in the time that interpolation fragment records, the all fragment records of traversal fragment recording areas, merge rule judgment according to predetermined fragment record and whether have the fragment record that can merge with the corresponding each fragment record of file to be deleted, if exist and perform step 304, otherwise, execution step 305;
Step 304: merge according to predetermined fragment record the fragment record merging that rule can merge, go to step 307;
Step 305: in the time that the remaining space of fragment recording areas can hold new fragment and records, add new fragment record;
Step 306: inquiry defragmentation condition, while reaching setting threshold in the occupied space of definite fragment recording areas, carry out defragmentation;
Step 307: finish deleted file process.
In the present embodiment, file system is carried out to defragmentation, be specially: determine whether to arrange completely, if, according to each fragment record in fragment recording areas, successively by the first fragment corresponding to multiple fragment record continuous storage space, recording with storage space and the first fragment the second fragment that multiple fragment records adjacent and that storage space is continuous are corresponding merges, and while merging, corresponding fragment record is merged at every turn, until judge while having merged into a fragment, this fragment is incorporated into unappropriated continuous space; If not, according to each fragment record in fragment recording areas, successively by the first fragment corresponding to multiple fragment record continuous storage space, second fragment corresponding to multiple fragment records adjacent with the first fragment with storage space merges, and in the time of each merging, corresponding fragment record is merged, until judge when remaining space in shared storage space or the fragment recording areas of fragment merging reaches setting threshold, finish defragmentation process.
Wherein, when file system is carried out to defragmentation operation, can start to merge successively each fragment from file system top, until judge while having merged into a fragment, this fragment is incorporated into unappropriated continuous space.In practical application, also can be that the maximum fragment that takes up room from file system starts to arrange, for example, by taking up room, maximum fragment merges with the fragment being positioned at thereafter successively, until merge with unappropriated continuous space, also can be to start to carry out defragmentation from the bottom of file system, can select according to actual needs different Collator Modes.
For example, as shown in Figure 4, the detailed process of defragmentation is as follows:
Step 401: defragmentation instruction is imported into by the defragmentation entrance of file system;
Step 402: judge that whether predetermined defragmentation mode is for arranging completely, if execution step 404, otherwise execution step 403;
Step 403: judge whether the shared storage space of fragment arranging reaches predetermined threshold, if execution step 408, otherwise, execution step 404;
Step 404: judge whether fragment recording areas only has a fragment record, if execution step 407, otherwise, execution step 405;
Step 405: the start address according to the each fragment record of fragment recording areas sorts, and determines the first two fragment that is positioned at file system top;
Step 406: the first two fragment that is positioned at file system top is merged, and revise after related fragment record execution step 402;
Step 407: unappropriated continuous space in fragment and file system is merged, and empty fragment recording areas;
Step 408: complete defragmentation process.
Again for example, as accompanying drawing 5A is depicted as the allocation of space situation of current file system, in this file system, preserve 4 files, be followed successively by file a, file b, file c and file d, between every two files, there is a fragment, have 3 fragments, be respectively fragment A, fragment B, fragment C, after the each fragment record in fragment recording areas being sorted according to start address, determine by multiple fragment records that storage space is continuous the fragment A that is positioned at file system top, and be positioned at immediate another fragment of fragment B on top with this, and merge two fragments, revise the relative recording of fragment recording areas simultaneously, merging rule according to fragment record merges each fragment record relevant to fragment A each fragment record relevant fragment B, as accompanying drawing 5B is depicted as the schematic diagram after fragment merging for the first time, carrying out in an identical manner fragment for the second time merges, by the fragment after fragment A and fragment B merging, merge with fragment C, each fragment record that each fragment record after simultaneously each fragment record relevant to fragment A fragment B being merged according to fragment record merging rule is correlated with fragment C again merges, as accompanying drawing 5C is depicted as the schematic diagram after merging for the second time, finally, by fragment A, B, fragment and unappropriated continuous space after C merges merge, as shown in accompanying drawing 5D, empty by fragment recording areas.
Based on above-mentioned principle, as shown in Figure 6, in the embodiment of the present invention, smart card mainly comprises following processing unit:
Receiving element 601, for receiving the APDU instruction that the file system of smart card is operated;
Processing unit 602, be used for the storage space of the difference size to preset as the pieces of different sizes, determine whether to exist according to the size of the pending file carrying in APDU instruction the piece mating with the size of pending file, if exist, process pending file according to the piece of this coupling; If do not exist, determine the combination of the piece of the different sizes that match from the size of pending file, according to the pending file of the combined treatment of the piece of this difference size.
Wherein, also comprise pretreatment unit 603, for presetting fragment recording areas in file system, with start address and the chip size information of each fragment in log file system.
Based on technique scheme, in the embodiment of the present invention, receiving after the Application Protocol Data Unit instruction that the file system of smart card is operated, pieces using the storage space of the difference size preset as different sizes, determine whether to exist according to the size of the pending file carrying in APDU instruction the piece mating with the size of pending file, if exist, process pending file according to the piece of this coupling, if do not exist, determine the combination of the piece of the different sizes that match from the size of pending file, according to the pending file of the combined treatment of the piece of this difference size, thereby in the time that file system is managed, can carry out corresponding operating taking the piece of different sizes as unit, in allocation space, while producing the operations such as fragment or defragmentation, all taking different big or small pieces as unit, improve the efficiency of dirigibility and the file system management of file system management, and improve the utilization factor to smart cards for storage space.
Simultaneously, in the embodiment of the present invention, by default fragment recording areas in file system, to record start address and the chip size information of fragment, make fragment information clear, thus can be in the time creating file, deleted file, utilize already present fragment to carry out corresponding operating according to fragment record, the efficiency and the dirigibility that have further improved smart card document system management, meanwhile, improved efficiency and the dirigibility of defragmentation to a great extent.Especially in the time of deleted file, do not do the integrated moving of file system, greatly reduced the execution time.And, due to the one in the piece of difference size corresponding to each fragment record, can directly fragment be recorded to corresponding fragment for preserving file to be created, save time, and improve the utilization factor in smart cards for storage space.
Obviously, those skilled in the art can carry out various changes and modification and not depart from the spirit and scope of the present invention the present invention.Like this, if these amendments of the present invention and within modification belongs to the scope of the claims in the present invention and equivalent technologies thereof, the present invention is also intended to comprise these changes and modification interior.