-
Notifications
You must be signed in to change notification settings - Fork 716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Attempt to create log file if it does not exist. #1322
base: main
Are you sure you want to change the base?
Changes from all commits
12a516c
76a31a2
c6c8d1f
eee3949
aa3480d
4815aef
94f1871
e8ba5b2
35e0ffd
7c7c9d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,71 @@ const char *fc_strerror(fc_error_t fcerror) | |
} | ||
} | ||
|
||
int fc_upsert_logg_file(fc_config *fcConfig) | ||
{ | ||
if (fcConfig->logFile == NULL) { | ||
return 0; | ||
} | ||
int ret = 0, field_no = 1; | ||
char *current_path, *file_path = cli_safer_strdup(fcConfig->logFile), *token; | ||
FILE *logg_fp = NULL; | ||
token = cli_strtok(file_path, field_no++, PATHSEP); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be easier to follow if this line were moved down to just above the On the topic of readability, Some blank lines between unrelated lines of code would be nice on the eyes. |
||
struct passwd *current_user, *db_owner; | ||
#ifndef _WIN32 | ||
current_user = getpwuid(getuid()); | ||
db_owner = getpwnam(fcConfig->dbOwner); | ||
#endif /* _WIN32 */ | ||
current_path = (char *)malloc(2); | ||
strcpy(current_path, PATHSEP); | ||
STATBUF sb; | ||
while (token != NULL) { | ||
current_path = (char *)realloc(current_path, strlen(current_path) + strlen(token) + 2); | ||
strcat(current_path, token); | ||
free(token); | ||
token = cli_strtok(file_path, field_no++, PATHSEP); | ||
if (token == NULL) { | ||
break; | ||
} | ||
if (LSTAT(current_path, &sb) == -1) { | ||
if (mkdir(current_path, 0755) == -1) { | ||
printf("ERROR: Failed to create required directory %s. Will continue without writing in %s.\n", current_path, fcConfig->logFile); | ||
ret = -1; | ||
goto cleanup; | ||
} | ||
#ifndef _WIN32 | ||
if (current_user->pw_uid != db_owner->pw_uid) { | ||
if (lchown(current_path, db_owner->pw_uid, db_owner->pw_gid) == -1) { | ||
printf("ERROR: Failed to change owner of %s to %s. Will continue without writing in %s.\n", current_path, fcConfig->dbOwner, fcConfig->logFile); | ||
ret = -1; | ||
goto cleanup; | ||
} | ||
} | ||
#endif /* _WIN32 */ | ||
} | ||
strcat(current_path, PATHSEP); | ||
} | ||
if ((logg_fp = fopen(fcConfig->logFile, "at")) == NULL) { | ||
printf("ERROR: Can't open %s in append mode (check permissions!).\n", fcConfig->logFile); | ||
ret = -1; | ||
goto cleanup; | ||
} | ||
#ifndef _WIN32 | ||
lchown(fcConfig->logFile, db_owner->pw_uid, db_owner->pw_gid); | ||
#endif /* _WIN32 */ | ||
Comment on lines
+140
to
+176
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some inline comments on this process would be nice. My understanding is it basically tries to do a The logic seems good to me for linux/unix systems. I'll have to try it out. I see an effort to compile on Windows but I don't think it'll work on Windows. Paths on windows start with a drive letter, unless it's a relative path. Since this is called for all systems, I expect it to fail to run on Windows, or create some extra directories somewhere (maybe in the current directory?). This change is also going to require that the |
||
|
||
cleanup: | ||
if (token != NULL) { | ||
free(token); | ||
} | ||
free(current_path); | ||
free(file_path); | ||
if (logg_fp != NULL) { | ||
fclose(logg_fp); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
fc_error_t fc_initialize(fc_config *fcConfig) | ||
{ | ||
fc_error_t status = FC_EARG; | ||
|
@@ -157,6 +222,8 @@ fc_error_t fc_initialize(fc_config *fcConfig) | |
logg_rotate = (fcConfig->logFlags & FC_CONFIG_LOG_ROTATE) ? 1 : 0; | ||
logg_size = fcConfig->maxLogSize; | ||
/* Set a log file if requested, and is not already set */ | ||
fc_upsert_logg_file(fcConfig); | ||
|
||
if ((NULL == logg_file) && (NULL != fcConfig->logFile)) { | ||
logg_file = cli_safer_strdup(fcConfig->logFile); | ||
if (0 != logg(LOGG_INFO_NF, "--------------------------------------\n")) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These pointers should be initialized to NULL.
The strdup call should also be on a separate line and should have error handling.