diff options
Diffstat (limited to 'apps/netutils/ftpc/ftpc_getfile.c')
-rw-r--r-- | apps/netutils/ftpc/ftpc_getfile.c | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/apps/netutils/ftpc/ftpc_getfile.c b/apps/netutils/ftpc/ftpc_getfile.c index e9816e5b4..bf9fc69ea 100644 --- a/apps/netutils/ftpc/ftpc_getfile.c +++ b/apps/netutils/ftpc/ftpc_getfile.c @@ -270,20 +270,30 @@ int ftpc_getfile(SESSION handle, FAR const char *rname, FAR const char *lname, FAR struct ftpc_session_s *session = (FAR struct ftpc_session_s *)handle; struct stat statbuf; FILE *loutstream; + FAR char *abslpath; off_t offset; int ret; + /* Get the full path to the local file */ + + abslpath = ftpc_abslpath(session, lname); + if (!abslpath) + { + ndbg("ftpc_abslpath(%s) failed: %d\n", errno); + goto errout; + } + /* Get information about the local file */ - ret = stat(lname, &statbuf); + ret = stat(abslpath, &statbuf); if (ret == 0) { /* It already exists. Is it a directory? */ if (S_ISDIR(statbuf.st_mode)) { - ndbg("'%s' is a directory\n", lname); - return ERROR; + ndbg("'%s' is a directory\n", abslpath); + goto errout_with_abspath; } } @@ -292,8 +302,8 @@ int ftpc_getfile(SESSION handle, FAR const char *rname, FAR const char *lname, #ifdef S_IWRITE if (!(statbuf.st_mode & S_IWRITE)) { - ndbg("'%s' permission denied\n", lname); - return ERROR; + ndbg("'%s' permission denied\n", abslpath); + goto errout_with_abspath; } #endif @@ -316,36 +326,33 @@ int ftpc_getfile(SESSION handle, FAR const char *rname, FAR const char *lname, if (ret != OK) { ndbg("ftpc_recvinit failed\n"); - return ERROR; + goto errout_with_abspath; } - loutstream = fopen(lname, (offset > 0 || (how == FTPC_GET_APPEND)) ? "a" : "w"); + loutstream = fopen(abslpath, (offset > 0 || (how == FTPC_GET_APPEND)) ? "a" : "w"); if (!loutstream) { ndbg("fopen failed: %d\n", errno); - session->offset = 0; - return ERROR; + goto errout_with_abspath; } + /* Save the new local and remote file names */ + + free(session->rname); + free(session->lname); + session->rname = strdup(rname); + session->lname = abslpath; + if (offset > 0) { ret = fseek(loutstream, offset, SEEK_SET); if (ret != OK) { ndbg("fseek failed: %d\n", errno); - fclose(loutstream); - session->offset = 0; - return ERROR; + goto errout_with_outstream; } } - /* Save the new local and remote file names */ - - free(session->rname); - free(session->lname); - session->rname = strdup(rname); - session->lname = strdup(lname); - /* And receive the new file data */ if (xfrmode == FTPC_XFRMODE_ASCII) @@ -364,8 +371,26 @@ int ftpc_getfile(SESSION handle, FAR const char *rname, FAR const char *lname, fptc_getreply(session); } + /* On success, the last rname and lname are retained for debug purpose */ + + if (ret == OK && !FTPC_INTERRUPTED(session)) + { + fclose(loutstream); + return OK; + } + + /* Various error exits */ + +errout_with_outstream: fclose(loutstream); - return (ret == OK && !FTPC_INTERRUPTED(session)) ? OK : ERROR; + free(session->rname); + session->rname = NULL; + session->lname = NULL; +errout_with_abspath: + free(abslpath); + session->offset = 0; +errout: + return ERROR; } /**************************************************************************** |