[mw-devel] [Git][arthur/mw][master] Fix up create_folder_file

Andrew Price welshbyte at sucs.org
Wed Jan 20 21:07:53 GMT 2016


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
ff58d063 by Andrew Price at 2016-01-20T21:02:50Z
Fix up create_folder_file

Use ftruncate instead of writing malloced garbage entries to set the
file size of folders.bb, which dictates the (seemingly arbitrary) 64
folder limit. Also make it return on error instead of exiting.

- - - - -


3 changed files:

- src/client/folders.c
- src/folders.c
- src/folders.h


Changes:

=====================================
src/client/folders.c
=====================================
--- a/src/client/folders.c
+++ b/src/client/folders.c
@@ -25,7 +25,8 @@ void add_folder(void)
 	int folnum = -1;
 	char tmp[11];
 
-	if (nofolders()) create_folder_file();
+	if (nofolders() && create_folder_file())
+		exit(1);
 	file=openfolderfile(O_RDWR);
 	do{
 		yes=get_folder_entry(file, &new);


=====================================
src/folders.c
=====================================
--- a/src/folders.c
+++ b/src/folders.c
@@ -50,24 +50,23 @@ int nofolders(void)
 	return 0;
 }
 
-void create_folder_file(void)
+int create_folder_file(void)
 {
-	int file,i;
-	struct folder *record;
+	int file;
+	int ret;
 
-	record=(struct folder *)malloc(sizeof(*record));
-	record->status=0;
-	file=open(FOLDERFILE,O_WRONLY|O_CREAT,0600);
+	file = open(FOLDERFILE, O_WRONLY|O_CREAT, 0600);
+	if (file < 0) {
+		perror(FOLDERFILE);
+		return 1;
+	}
 	Lock_File(file);
-	for (i=0;i<64;i++)
-		if (write(file,record,sizeof(*record))<0)
-		{
-			perror("creating blank folders");
-			exit(-1);
-		}
+	ret = ftruncate(file, sizeof(struct folder) * 64);
+	if (ret != 0)
+		perror("Truncating folder file");
 	Unlock_File(file);
 	close(file);
-	free(record);
+	return ret;
 }
 
 int foldernumber(const char *name)


=====================================
src/folders.h
=====================================
--- a/src/folders.h
+++ b/src/folders.h
@@ -60,7 +60,7 @@ extern const char *folder_types[4];
 
 int openfolderfile(int mode);
 int nofolders(void);
-void create_folder_file(void);
+int create_folder_file(void);
 int foldernumber(const char *name);
 int get_folder_entry(int file, struct folder *tmp);
 int get_folder_number(struct folder *fol, int num);



View it on GitLab: https://projects.sucs.org/arthur/mw/commit/ff58d063d9420bdef62faa1128e037e295eb737f
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20160120/14d254d8/attachment.html>


More information about the mw-devel mailing list