Skip to content

Commit

Permalink
gcoap/fileserver: recursive directory deletion as default
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian18 committed Aug 3, 2022
1 parent 70a8cf0 commit f357d99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 10 additions & 2 deletions sys/include/net/gcoap/fileserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
* In opposite, the If-None-Match option requires a request to be processed,
* only if the resource does not yet exist, and is most useful for file creation.
*
* Directories are expressed to URIs with trailing slashes and can be DELETEd
* when empty.
* Directories are expressed to URIs with trailing slashes. Directories and
* their content are deleted as if one would do an `$rm -r`. If you only would
* like to delete a directory if it is empty, you must supply an If-Match option
* with the special value @ref COAPFILESERVER_DIR_DELETE_ETAG.
*
* @note The file server uses ETag for cache validation. The ETags are built
* from the file system stat values. As clients rely on the ETag to differ when
Expand Down Expand Up @@ -80,6 +82,12 @@ extern "C" {

#include "net/nanocoap.h"

/**
* @brief Randomly generated Etag, used by a client when a directory should only be
* deleted, if it is empty
*/
#define COAPFILESERVER_DIR_DELETE_ETAG (0x6ce88b56u)

/**
* @brief File server handler
*
Expand Down
16 changes: 13 additions & 3 deletions sys/net/application_layer/gcoap/fileserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "net/gcoap/fileserver.h"
#include "net/gcoap.h"
#include "vfs.h"
#include "vfs_util.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down Expand Up @@ -456,10 +457,19 @@ static ssize_t _delete_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len,
{
int err;
if (request->options.exists.if_match && request->options.if_match_len) {
return gcoap_fileserver_error_handler(pdu, buf, len, COAP_CODE_PRECONDITION_FAILED);
if (request->options.if_match != byteorder_htonl(COAPFILESERVER_DIR_DELETE_ETAG).u32) {
return gcoap_fileserver_error_handler(pdu, buf, len, COAP_CODE_PRECONDITION_FAILED);
}
if ((err = vfs_rmdir(request->namebuf)) < 0) {
return gcoap_fileserver_error_handler(pdu, buf, len, err);
}
}
if ((err = vfs_rmdir(request->namebuf)) < 0) {
return gcoap_fileserver_error_handler(pdu, buf, len, err);
else if (IS_USED(MODULE_VFS_UTIL)) {
if ((err = vfs_unlink_recursive(request->namebuf,
request->namebuf,
sizeof(request->namebuf))) < 0) {
gcoap_fileserver_error_handler(pdu, buf, len, err);
}
}
gcoap_resp_init(pdu, buf, len, COAP_CODE_DELETED);
return coap_opt_finish(pdu, COAP_OPT_FINISH_NONE);
Expand Down

0 comments on commit f357d99

Please sign in to comment.