Detailed Description
The present invention will be described in further detail with reference to the accompanying drawings and specific embodiments.
The inventor analyzes the existing Squashfs and Cramfs upgrading method and finds that the defects are as follows:
firstly, because the data arrangement of each file in the Squashfs and the cramf must be closely arranged according to bytes, the upgrading of the Squashfs and the cramf must be a complete upgrading mode, so that the flash partitions where the Squashfs and the cramf are located need to be erased during upgrading, new Squashfs and cramf are written, if the equipment uses nor flash to store the Squashfs and the cramf, the whole upgrading process is time-consuming, meanwhile, the instability of the system is also increased, and if the equipment is powered off in the upgrading process, the upgrading process must be executed again after the equipment is restarted;
and secondly, the whole Squashfs and Cramfs are transmitted when the Squashfs and Cramfs are remotely upgraded, and the transmission time consumption of the upgrade package is long for remote mountainous areas with unstable signals.
The inventor analyzes and discovers that the upgrading process of Squashfs and Cramfs comprises the following steps:
when the Squashfs and Cramfs are upgraded, only a certain file in the Squashfs and the Cramfs needs to be updated actually, but the files are limited to be read-only file systems in the Squashfs and the Cramfs, and the file cannot be directly modified in the prior art, so that the prior art still needs to manufacture complete Squashfs and Cramfs.
According to the above analysis, in order to overcome the drawbacks of the prior art, the inventors present the following solutions:
fig. 1 is a flowchart of an upgrade file generation method according to an embodiment of the present invention, which includes the following specific steps:
step 101: when the read-only file system generates a new version, storing a data block which is updated relative to the read-only file system of the old version in the new version and the position of the data block in the read-only file system of the new version; the number of data blocks occupied by each file in the read-only file system is fixed, and the position of each data block in the read-only file system is fixed.
Specifically, when the read-only file system generates a new version, the data block pairs at the same position in the new version and the old version of the read-only file system are sequentially compared, and if the contents of the two data blocks in any data block pair are different, it is determined that the data block in the new version is updated.
Step 102: and constructing an upgrade file, and putting the position of the data block where the update occurs in the new version read-only file system, the data content of the data block where the update occurs and the position of the data block where the update occurs in the upgrade file into the upgrade file.
In the implementation of the invention, the number of the data blocks occupied by each file in the read-only file system is fixed, and the position of each data block in the read-only file system is fixed, so that when the data blocks in the read-only file system are updated, the data blocks in the new version and the old version can be determined by comparing the data block pairs at the same positions, and the updated file is constructed only according to the updated data blocks, so that the length of the updated file is reduced on the premise of ensuring the successful update of the read-only file system, and the transmission speed of the updated file is accelerated.
Specifically, placing the location of the data block where the update occurs in the new version read-only file system, the data content of the data block where the update occurs, and the location of the data block where the update occurs in the upgrade file into the upgrade file may include:
the method comprises the steps of placing the position of an updated data block in a new version read-only file system into a file header or a file tail or a content part of an upgrade file, and preferentially selecting the file header of the upgrade file, wherein when the file tail or the content part of the upgrade file is placed, the position of the file tail or the content part is required to be marked in the file header;
putting the data content of the updated data block into the content part of the upgrade file;
and placing the position of the updated data block in the upgrade file into a file header or a file tail or a content part of the upgrade file, and preferentially selecting the file header into which the upgrade file is placed, wherein when the file tail or the content part of the upgrade file is placed, the position of the file tail or the content part is required to be marked in the file header.
The position of the updated data block in the new version read-only file system can be represented by the offset of the data block in the new version read-only file system and the length of the data block; the location of the data block in the upgrade file where the update occurs may be represented by the offset of the data block in the upgrade file and the length of the data block.
In practical applications,step 102 may be further followed by:
according to the position of a data block which is updated in an upgrade file in a new version read-only file system, finding a data block to be updated in an old version read-only file system, wherein the position of the data block to be updated in the old version read-only file system corresponds to the position of the data block which is updated in the new version read-only file system; and erasing the data block to be updated, and writing the data block which is updated in the new version.
By the embodiment, when the read-only file system is updated, only the erasing and writing operation of the data block is needed to be carried out on the position where the updated data block is located in the read-only file system of the old version, so that the upgrading speed of the file system is accelerated.
In practical applications, afterstep 101 and beforestep 102, the method may further include: when a plurality of continuous data blocks in the new version are updated, taking the plurality of continuous data blocks as a data segment, and taking the independent data blocks which are updated as a data segment;
instep 102, the position of the data segment in the new version, which is updated, in the read-only file system of the new version and the position of the data segment in the new version, which is updated, in the upgrade file are put into the upgrade file;
and, after thestep 102, further comprising: reading the updated data segment from the content part of the upgrade file according to the position of the updated data segment in the upgrade file; according to the position of the updated data segment in the updated file in the new version read-only file system, the data segment to be updated is found in the old version read-only file system; and erasing the data segment to be updated, and writing the data segment which is updated in the new version.
Specifically, the placing the position of the data segment updated in the new version read-only file system and the position of the data segment updated in the new version in the upgrade file may include:
the method comprises the steps of placing the position of a data segment which is updated in a new version in a read-only file system of the new version into a file header or a file tail or a content part of an upgrade file, and preferentially selecting the file header of the upgrade file, wherein when the file tail or the content part of the upgrade file is placed, the position of the file tail or the content part is required to be marked in the file header;
and placing the position of the data segment which is updated in the new version in the upgrade file into the file header or the file tail or the content part of the upgrade file, and preferentially selecting the file header which is placed in the upgrade file, wherein when the file tail or the content part of the upgrade file is placed, the position of the file tail or the content part is required to be marked in the file header.
The position of the data segment which is updated in the new version read-only file system can be represented by the offset of the data segment in the new version read-only file system and the length of the data segment; the location of the data segment in the upgrade file where the update occurs in the new version may be represented by the offset of the data segment in the upgrade file and the length of the data segment.
Through the embodiment, the plurality of continuous data blocks which are updated are used as one data segment, so that the length of the upgrade file is further reduced, and the transmission speed of the upgrade file is further increased.
In practical applications,step 101 may further include: when the read-only file system of the initial version is manufactured, data blocks are distributed for each file according to the maximum data block number contained in each file in the file system which is configured in advance, and then the initial data of each file is written into the data blocks distributed for the file in sequence.
Through the embodiment, the data blocks are distributed for the files according to the maximum data block number contained in each file in the file system which is configured in advance, so that the file systems with different sizes can be upgraded by adopting the upgrading method provided by the embodiment of the invention.
In practical applications, afterstep 102, the method may further include: and calculating the encrypted value of the upgrade file by adopting a message digest algorithm, sending the encrypted value and the upgrade file to the equipment using the read-only file system, calculating the encrypted value of the upgrade file by adopting the same message digest algorithm after the equipment receives the encrypted value and the upgrade file, and determining that the upgrade file is normal if the calculated encrypted value is the same as the received encrypted value.
Through the embodiment, the upgrading file is encrypted, and the safe transmission of the upgrading file is ensured.
The read-only file system in the embodiment of the invention can be Squashfs or Cramfs.
Fig. 2 is a flowchart of an upgrade file generation method according to another embodiment of the present invention, which includes the following specific steps:
step 200: when a read-only file system of an initial version is manufactured, distributing data blocks for each file according to the maximum data block number contained in each file in the file system configured in advance, and then writing initial data of each file into the data blocks distributed for the file in sequence; the initial version of the read-only file system is configured on the device.
A read-only file system contains one or more files. The individual files are typically arranged in a read-only file system in the order of the file names from a to z.
Table 1 is a configuration table of the maximum number of data blocks included in each file in a read-only file system:
TABLE 1
In a read-only file system of each version (including the original version), the data blocks included in each file are not necessarily written with data, i.e., there are some reserved data blocks.
Fig. 3 is a schematic structural diagram of Squashfs when the read-only file system in table 1 is Squashfs. As shown in fig. 3, block 0 is a super block and is used to store the structure of file system metadata, block n is used to store information such as index, and other blocks include: blocks 1-n-1 are used to store file data, where which blocks are assigned to which file is predefined, as in FIG. 3, blocks 1-3 are used to store data for file 1, and blocks 4-7 are used to store data for file 2, …. Since it is ensured that all versions of file data can be written into corresponding blocks, enough blocks are allocated to each file, so that after the initial version of file data is written into a block, some reserved blocks exist, as shown in fig. 3, the data of file 1 only occupies all of block 1 and a part of block 2, and the data of file 2 only occupies all of block 4 and a part of block 5.
FIG. 4 is a schematic structural diagram of Cramfs when the read-only file system in Table 1 is Cramfs. As shown in FIG. 4, blocks 0 through x-1 are used to store superblock information, indexes, directories, etc., and blocks x and beyond are used to store file data, where which blocks are assigned to which file is predefined, as in FIG. 4, blocks x through x +2 are used to store data for file 1, and blocks x +3 through x +6 are used to store data for file 2, …. Since it is ensured that all versions of file data can be written into corresponding blocks, enough blocks are allocated to each file, so that after the initial version of file data is written into a block, some reserved blocks exist, as shown in fig. 4, the data of file 1 only occupies all of block x and a part of block x +1, and the data of file 2 only occupies all of block x +3 and a part of block x + 4.
Step 201: when a remote client finds that the read-only file system generates a new version, sequentially comparing each data block pair at the same position in the new version and the old version of the read-only file system, if the contents of two data blocks in any data block pair are different, determining that the data block in the new version is updated, and storing the data block which is updated in the new version, the offset of the data block in the read-only file system of the new version and the length of the data block.
Since the location of each file in the read-only file system is fixed. Therefore, by comparing whether the contents of the data blocks at the same position in the new version and the old version are the same or not, which data blocks are updated can be known, and only the data blocks in the new version which are updated need to be stored.
Step 202: and when the remote client determines that all the data block pairs are compared, regarding each updated data block in the stored new version, if continuous data blocks exist, taking each group of continuous data blocks as a data segment, and taking each remaining data block as a data segment.
Consecutive data blocks, i.e. data blocks where an update has occurred, are located adjacently in the new version of the read-only file system.
Step 203: the remote client constructs an upgrade file.
The method comprises the following steps of placing the offset of each data segment which is updated in a new version in a read-only file system of the new version, the length of each data segment and the number of the data segments which are updated into a file header of an upgrade file; sequentially placing the data segments into the content part of the upgrade file according to the sequence from small to large of the data block numbers contained in the data segments; then, the offset of each data segment in the upgrade file is put into the file header of the upgrade file.
It should be noted that, in practical application, the offset of each data segment in the new version that is updated in the read-only file system of the new version, the length of each data segment, and the number of data segments that are updated may also be placed at the end of the file and the content of the updated file, and at this time, the specific positions of the end of the file and the content of the updated file need to be marked in the header of the updated file;
meanwhile, the offset of each data segment in the upgrade file can also be placed in the file tail or the content part of the upgrade file, and at this time, the specific positions of the file tail and the content part of the upgrade file need to be marked in the file header of the upgrade file.
Fig. 5 is a diagram showing a structure example of an upgrade file, and as shown in fig. 5, the upgrade file mainly includes an upgrade file header and an upgrade file content, and data of data segments 1 to k in a new version, which are updated, are stored in the upgrade file content.
Fig. 6 is a diagram showing an example of the structure of a file header of an upgrade file, in this example, the number k of data segments in which an update occurs in a new version, and each data segment in which an update occurs in a new version include: the offset of the data segments 1-k in the upgrade file and each data segment updated in the new version comprise: the offsets of the data segments 1-k in the read-only file system of the new version are all placed in the header of the upgrade file.
Step 204: the remote client calculates the encryption value of the upgrade file by using an MD (Message-Digest) 5 algorithm, and generates an MD5 file according to the encryption value.
Step 205: and the remote client packs the upgrade file and the MD5 file into an upgrade pack and then sends the upgrade pack to the equipment.
Step 206: the equipment receives the upgrade package, obtains the upgrade file and the MD5 file after decompression, analyzes the encryption value from the MD5 file, performs MD5 calculation on the upgrade file to obtain the encryption value, and determines that the upgrade file is normal if the obtained encryption value is consistent with the analyzed encryption value.
Step 207: the equipment analyzes the file header of the upgrade file, and reads each data segment from the content part of the upgrade file in sequence according to the analyzed number of the updated data segments and the offset of each data segment in the upgrade file.
Step 208: the device searches each data segment to be updated in the read-only file system of the old version stored on the device in sequence according to the offset of each data segment which is analyzed from the file header and is updated in the read-only file system of the new version and the length of each data segment, erases the data segment to be updated from the corresponding storage position, and writes the corresponding data segment of the new version in the corresponding storage position.
The read-only file system in the embodiment of the present invention may be a mirror file system, such as: binary file system in format of BIN (Binary), etc.
Fig. 7 is an upgrade file generation apparatus according to an embodiment of the present invention, where the apparatus mainly includes: acomparison module 71 and an upgradefile construction module 72, wherein:
thecomparison module 71 is configured to, when the read-only file system generates a new version, store a data block that is updated with respect to the read-only file system of the old version in the new version, and a position of the data block in the read-only file system of the new version; the number of data blocks occupied by each file in the read-only file system is fixed, and the position of each data block in the read-only file system is fixed.
An upgradefile constructing module 72, configured to construct an upgrade file according to the data block updated in the new version and the location of the data block in the new version read-only file system, which are stored in the comparingmodule 71, and place the location of the data block in the new version read-only file system, the data content of the data block, and the location of the data block in the upgrade file.
Another embodiment of the present invention provides an upgrade file generation apparatus, which includes a processor configured to perform the steps of the method as described in fig. 1 or fig. 2.
Embodiments of the invention also provide a non-transitory computer readable storage medium storing instructions that, when executed by a processor, cause the processor to perform the steps of the method as described in fig. 1 or fig. 2.
Embodiments of the present invention also provide an electronic device, including the non-transitory computer readable storage medium as described above, and the above processor having access to the non-transitory computer readable storage medium.
The embodiment of the invention has the following beneficial technical effects:
the structure of the read-only file system is set as follows: the number of the data blocks occupied by each file is fixed, and the position of each data block in the read-only file system is fixed, so that when a new version is generated in the read-only file system, only the data block which is updated in the new version compared with the old version needs to be put into the upgrade file, the length of the upgrade file is reduced on the premise of ensuring the successful upgrade of the read-only file system, and the transmission speed of the upgrade file is accelerated;
in addition, for the equipment using the read-only file system, the whole read-only file system is not required to be erased and written, but only the data block which needs to be updated in the old version of the read-only file system is required to be erased and written, the upgrading speed is greatly improved, the system stability is also improved, if the equipment is powered off in the upgrading process, only the file which is being upgraded is influenced, other files cannot be influenced, and after the equipment is restarted, the upgrading process of the file which is being upgraded is required to be restarted.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.