DEV Community

Cover image for A open-source contribution story!
Md. Mahmudul Hasan Mabud
Md. Mahmudul Hasan Mabud

Posted on

A open-source contribution story!

Hello everybody, I'm writing today to share my Tuesday's update. I dedicate my Tuesdays to contributing to open-source projects. So I'm searching project for contribute as usual and found a interesting project called TinyFS-UNO, A crash-consistent, wear-aware, integrity-checked log-structured miniature filesystem for the Arduino Uno (ATmega328P) internal 1 KB EEPROM. Then I'm exploring it to understand, I clone it to my local machine and try to compile it. But
when I'm running make run inside the TinyFS-UNO/tests directory, the build fails during the linking stage due to missing function references.

ERROR

gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs_eeprom_mock.c -o ../src/tinyfs_eeprom_mock.o
gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs_storage.c -o ../src/tinyfs_storage.o
gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs_crc.c -o ../src/tinyfs_crc.o
gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs_record.c -o ../src/tinyfs_record.o
gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs_metadata.c -o ../src/tinyfs_metadata.o
gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs_gc.c -o ../src/tinyfs_gc.o
gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs_cli.c -o ../src/tinyfs_cli.o
gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs.c -o ../src/tinyfs.o

../src/tinyfs.c: In function ‘tfs_rename’:
../src/tinyfs.c:478:22: warning: implicit declaration of function ‘tfs_read_file’; did you mean ‘tfs_read’? [-Wimplicit-function-declaration]
  478 |     int bytes_read = tfs_read_file(old_filename, buffer, sizeof(buffer));
      |                      ^~~~~~~~~~~~~
      |                      tfs_read

../src/tinyfs.c:481:21: warning: implicit declaration of function ‘tfs_write_file’; did you mean ‘tfs_write’? [-Wimplicit-function-declaration]
  481 |     int write_res = tfs_write_file(new_filename, buffer, bytes_read);
      |                     ^~~~~~~~~~~~~~
      |                     tfs_write

../src/tinyfs.c:484:22: warning: implicit declaration of function ‘tfs_delete_file’; did you mean ‘tfs_delete’? [-Wimplicit-function-declaration]
  484 |     int delete_res = tfs_delete_file(old_filename);
      |                      ^~~~~~~~~~~~~~~
      |                      tfs_delete

gcc -Wall -Wextra -std=c99 -g -I../src -c test_tinyfs.c -o test_tinyfs.o
gcc -Wall -Wextra -std=c99 -g -I../src -o test_tinyfs ../src/tinyfs_eeprom_mock.o ../src/tinyfs_storage.o ../src/tinyfs_crc.o ../src/tinyfs_record.o ../src/tinyfs_metadata.o ../src/tinyfs_gc.o ../src/tinyfs_cli.o ../src/tinyfs.o test_tinyfs.o

/usr/bin/ld: ../src/tinyfs.o: in function `tfs_rename':
/home/mahmudul/TinyFS-UNO/tests/../src/tinyfs.c:478: undefined reference to `tfs_read_file'
/usr/bin/ld: /home/mahmudul/TinyFS-UNO/tests/../src/tinyfs.c:481: undefined reference to `tfs_write_file'
/usr/bin/ld: /home/mahmudul/TinyFS-UNO/tests/../src/tinyfs.c:484: undefined reference to `tfs_delete_file'
collect2: error: ld returned 1 exit status
make: *** [Makefile:12: test_tinyfs] Error 1
Enter fullscreen mode Exit fullscreen mode

I read this message carefully and identify what happening here. Let's discuss in details.

PROBLEM

Inside the tfs_rename function in the /src/tinyfc.c file, some undefined function are called like tfs_read_file(), tfs_write_file() and tfs_delete_file(). Let's see some interesting thing about compiler, it's suggest me by giving message did you mean ‘tfs_read’? and so on. Because in header file the function prototype are defined by tfs_read(),tfs_write(),tfs_delete() and in source code also declared like that but in tfs_rename() funtion called them including extra _file so compiler do not find them and throw error.

WHAT I DID

Simply I replace tfs_read_file() to tfs_read(), tfs_write_file() to tfs_write() and tfs_delet_file() to tfs_delete() in tfs_rename() function of tinyfs.c file. Then went to /tests by cd../tests and again run make run

ERROR AGAIN

gcc -Wall -Wextra -std=c99 -g -I../src -c ../src/tinyfs.c -o ../src/tinyfs.o
../src/tinyfs.c: In function ‘tfs_rename’:
../src/tinyfs.c:478:22: error: too few arguments to function ‘tfs_read’
  478 |     int bytes_read = tfs_read(old_filename, buffer, sizeof(buffer));
      |                      ^~~~~~~~
../src/tinyfs.c:214:5: note: declared here
  214 | int tfs_read(const char *filename, uint8_t *buf, uint16_t len, uint16_t offset_bytes) {
      |     ^~~~~~~~
make: *** [Makefile:15: ../src/tinyfs.o] Error 1
Enter fullscreen mode Exit fullscreen mode

WHAT HAPPENED

The tfs_read() function declared as 4 arguments function but in tfs_rename() funtion just 3 argument passing to it.

SOLUTION

Now it's require to understand what is doing this function actually. tfs_rename function just open old file read from 0 index to EOF and store buffer and open new file (user's given name) and save it buffer's data. and delete the old file. It's need to read file from zero index so send 0 as the offset_bytes.

int bytes_read = tfs_read(old_filename, buffer, sizeof(buffer), 0);
Enter fullscreen mode Exit fullscreen mode

And now run again make run, It's work!
All tests are passed!

TOTAL CHANGES

 diff --git a/src/tinyfs.c b/src/tinyfs.c

index 9a4724b..0d65d08 100644

--- a/src/tinyfs.c

+++ b/src/tinyfs.c

@@ -475,14 +475,14 @@ int tfs_rename(const char *old_filename, const char *new_filename) {

     }



     uint8_t buffer[256];

-    int bytes_read = tfs_read_file(old_filename, buffer, sizeof(buffer));

+    int bytes_read = tfs_read(old_filename, buffer, sizeof(buffer), 0);

     if (bytes_read < 0) return -1;



-    int write_res = tfs_write_file(new_filename, buffer, bytes_read);

+    int write_res = tfs_write(new_filename, buffer, bytes_read);

     if (write_res < 0) return -1;



-    int delete_res = tfs_delete_file(old_filename);

+    int delete_res = tfs_delete(old_filename);

     if (delete_res < 0) return -1;



     return 0;

-}

\ No newline at end of file

+} 
Enter fullscreen mode Exit fullscreen mode

PUSH AND CREATE PULL REQUEST

I committed my changes in fix/rename-api local branch and push it to my forked remote repo, went github and create a pull request.
And with that today's mission I has completed.

My github - mahmudul626

I want to know your story please share in the comment section.
Happy wishes to you!

Top comments (0)