eclipse-threadx/filex

Fail to create empty file althogu there is enough space

smithBraun opened this issue · 6 comments

https://github.com/azure-rtos/filex/blob/6817aa3097a54f36f4caf68f8af4a8c1d766b257/common/src/fx_directory_free_search.c#L437
In case that there is exactly enough clusters for new directory entry, the system refuses to create it. (Although maybe I want empty file, or I want afterward to release other space).
need to fix to media_ptr -> fx_media_available_clusters >= clusters_needed

The issue also effect case of renaming file when just one cluster available and in the directory file there is no space to new dir entry

Here is template code to reproduce the issue:

#define DIR_PATH "MYDIR\"
#define DIR_PATH_SIZE (sizeof(DIR_PATH) - 1)
	int general_rc;
	UINT fx_rc;
	FX_FILE my_file;
	ULONG available_clusters;
	uint8_t buff[CLUSTER_SIZE];
	int num_of_files = 0;
	char dummy_file_path[DIR_PATH_SIZE + 3];
	memcpy(dummy_file_path, DIR_PATH, DIR_PATH_SIZE);

	ASSERT(fx_rc == FX_SUCCESS);

    /* Create a file called TEST.TXT.*/
	fx_rc =  fx_file_create(my_disk, DIR_PATH "TEST.TXT");
	ASSERT(fx_rc == FX_SUCCESS);

	available_clusters = my_disk->fx_media_available_clusters;

	/*
	 * Ensure there is no spair space in the cluster used for the directory
	 */
	dummy_file_path[sizeof(DIR_PATH) - 1] = 'A';
	dummy_file_path[sizeof(DIR_PATH)] = '\0';
	for (num_of_files = 0; available_clusters == my_disk->fx_media_available_clusters; num_of_files ++)
	{
		dummy_file_path[sizeof(DIR_PATH) - 1] ++;
		fx_rc =  fx_file_create(my_disk, dummy_file_path);
		ASSERT(fx_rc == FX_SUCCESS);
	}
	// remove the file that allocated new cluster
	fx_rc =  fx_file_delete(my_disk, dummy_file_path);
	ASSERT(fx_rc == FX_SUCCESS);
	ASSERT(available_clusters == my_disk->fx_media_available_clusters);



    /* Open the test file.  */
	fx_rc =  fx_file_open(my_disk, &my_file, DIR_PATH "TEST.TXT", FX_OPEN_FOR_WRITE);
	ASSERT(fx_rc == FX_SUCCESS);

	for (int i = 0; i < available_clusters - 1; ++i)
	{
		/* Write a one cluster size to the test file.  */
		fx_rc =  fx_file_write(&my_file, buff, sizeof(buff));
		ASSERT(fx_rc == FX_SUCCESS);
	}

	ASSERT(my_disk->fx_media_available_clusters == 1);

	// The dir entry should be created on new cluster which is the end that was free (see bug in this case https://github.com/azure-rtos/filex/issues/26)
	fx_rc =  fx_file_rename(my_disk, DIR_PATH "TEST.TXT", DIR_PATH "TESTNEW.TXT");
	ASSERT(fx_rc == FX_SUCCESS);
	ASSERT(my_disk->fx_media_available_clusters == 0);

@smithBraun Thanks for reporting the issue. We will fix it.

Do you agree about the issue and the fix? (I want to change it locally now so need to know I am correct)

the suggested fix looks good. We are working on it now.

The fix was incorporated and will be available in the upcoming release