structure fails to update

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, I have a disturbing bug in my code that maybe one of you may have run into before.  The function is listed below. For some reason the contents are not getting updated by the call to, "hardware_write_block".

 

Okay then maybe it is something wrong with the function listed... BUT

 

This might be hard to follow.

But I'm calling code elsewhere after that which should also update the values but the data remains the same for data length and valid data. The other data pending to be updated is correctly updated.

 

 

unsigned long long int write_file_length(struct ff_file * file, const char * name, unsigned long long file_length){


	struct directory_entry     * directory = (struct directory_entry * )file->cluster_data;
	struct stream_extension    * streamExt;
	struct file_name_extension * nameExt;

	char recorded_name[255];


	while(directory->entry_type != 0x00){

		if(directory->entry_type == DATAEXT){
			streamExt = (struct stream_extension    *) directory + 1;

			if(streamExt->entry_type == STREAMEXT){
				nameExt = (struct file_name_extension *) streamExt + 1;

				int name_length = streamExt->name_length;

				// get name
				if(nameExt->entry_type == FILEEXT){

					unsigned char * ptr = nameExt->file_name;

					int i = 1;
					for(i = 1; i < name_length + 1; i++){
						recorded_name[i - 1] = * ptr; ptr += 2;

						if((i % 15) == 0){
							nameExt++; ptr = &nameExt->file_name[0];
						}
					}recorded_name[i - 1] = '\0';

					// if name matches update data length and valid data
					if(memcmp(&recorded_name[0], name, strlen(name)) == 0){

						streamExt->data_length[0] = 0xff;//file_length >> 0;
						streamExt->data_length[1] = 0xff;//file_length >> 8;
						streamExt->data_length[2] = 0xff;//file_length >> 16;
						streamExt->data_length[3] = 0xff;//file_length >> 24;
						streamExt->data_length[4] = 0xff;//file_length >> 32;
						streamExt->data_length[5] = 0xff;//file_length >> 40;
						streamExt->data_length[6] = 0xff;//file_length >> 48;
						streamExt->data_length[7] = 0xff;//file_length >> 56;

						streamExt->valid_data[0] = 0xff;//file_length >> 0;
						streamExt->valid_data[1] = 0xff;//file_length >> 8;
						streamExt->valid_data[2] = 0xff;//file_length >> 16;
						streamExt->valid_data[3] = 0xff;//file_length >> 24;
						streamExt->valid_data[4] = 0xff;//file_length >> 32;
						streamExt->valid_data[5] = 0xff;//file_length >> 40;
						streamExt->valid_data[6] = 0xff;//file_length >> 48;
						streamExt->valid_data[7] = 0xff;//file_length >> 56;

						int CheckSum = EntrySetCheckSum((unsigned char *)&directory->entry_type, ((directory->secondary_count + 1) * 32));

						directory->set_checksum[0] = CheckSum;
						directory->set_checksum[1] = CheckSum >> 8;

						// This updates the contents of sd card
						hardware_write_block(1, file->cluster_addr, Allocation_Unit(), file->cluster_data, 0);
						return 1;
					}
				}
			}
		}
		directory++;
	}
	return 0;
}

 

 

 

This topic has a solution.
Last Edited: Tue. May 28, 2019 - 08:31 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay, I have a little more information.

 

The function "hardware_write_block" works.  However the structure streamExt seems to be pointed at a previous structure instance of itself.

 

EDITED: Found the location, the following is not correctly working:

 

if(memcmp(&recorded_name[0], name, strlen(name)) == 0){

Last Edited: Tue. May 28, 2019 - 07:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

Okay, I have a little more information.

 

The function "hardware_write_block" works.  However the structure streamExt seems to be pointed at a previous structure instance of itself.

 

EDITED: Found the location, the following is not correctly working:

 

if(memcmp(&recorded_name[0], name, strlen(name)) == 0){

What are you expecting that line to do?

 

That call to memcmp will compare only the first n characters of recorded_name, where n is the length of name. If recorded_name is "ABCDE" and name is "ABC", for example, then memcmp will return 0 because the first 3 characters match.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi christop, I'm comparing the entry file name with a file name, the name of the file that is opened.

The code works, but somewhere in my project I have accidently changed it.

 

No biggy.

 

Thanks

Last Edited: Tue. May 28, 2019 - 09:19 PM