Skip to content

Commit

Permalink
* ADD function to set blocksz without recompile.
Browse files Browse the repository at this point in the history
Signed-off-by: wanghaemq <[email protected]>
  • Loading branch information
wanghaEMQ committed Jun 11, 2024
1 parent a49b33b commit 57afa5c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ nftp_file_blocks(char *fpath, size_t *blocks)
return rv;
}

*blocks = sz/NFTP_BLOCK_SZ + 1;
*blocks = sz/nftp_get_blocksz() + 1;
return (0);
}

Expand All @@ -181,15 +181,15 @@ nftp_file_readblk(char *fpath, int n, char **strp, size_t *sz)
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);

if (n > (int)filesize/NFTP_BLOCK_SZ) {
if ((size_t)n > filesize/nftp_get_blocksz()) {
return (NFTP_ERR_BLOCKS);
} else if (n == (int)filesize/NFTP_BLOCK_SZ) {
blksz = filesize - n*NFTP_BLOCK_SZ;
} else if ((size_t)n == filesize/nftp_get_blocksz()) {
blksz = filesize - n*nftp_get_blocksz();
} else {
blksz = NFTP_BLOCK_SZ;
blksz = nftp_get_blocksz();
}

fseek(fp, n*NFTP_BLOCK_SZ, SEEK_SET);
fseek(fp, n*nftp_get_blocksz(), SEEK_SET);

str = (char *) malloc(blksz + 1);
memset(str, '\0', blksz + 1);
Expand Down
8 changes: 7 additions & 1 deletion src/nftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#define NFTP_TYPE_GIVEME 0x05

#define NFTP_SIZE 32
#define NFTP_BLOCK_SZ (32 * 1024) // Maximal size of single package
#define NFTP_BLOCK_NUM (0xFFFF) // Maximal number of blocks
#define NFTP_FILES 32 // Receive up to 32 files at once
#define NFTP_HASH(p, n) nftp_crc32c(p, n)
Expand Down Expand Up @@ -253,7 +252,14 @@ int nftp_proto_handler(char *msg, int len, char **rmsg, int *rlen);
int nftp_proto_register(char *, int (*cb)(void *), void *);
int nftp_proto_unregister(char *);

/*
* Setting recvdir or blocksz is not thread-safe.
* Those two functions just set a global inner variable and then return.
*/
int nftp_set_recvdir(char *);
int nftp_set_blocksz(uint32_t);
uint32_t nftp_get_blocksz();

int test();

#endif
Expand Down
18 changes: 16 additions & 2 deletions src/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#endif

static char *recvdir = NULL;
static uint32_t blocksz = 32*1024; // default block size

struct file_cb {
char *fname;
Expand Down Expand Up @@ -307,10 +308,10 @@ nftp_proto_maker(char *fpath, int type, int key, int n, char **rmsg, int *rlen)
if (0 != (rv = nftp_file_size(fpath, &len)))
return rv;

blocks = (len/NFTP_BLOCK_SZ) + 1;
blocks = (len/nftp_get_blocksz()) + 1;
if (blocks > (0xFFFF)) {
nftp_log("File is too large (MAXSIZE: %dKB).",
(NFTP_BLOCK_SZ * NFTP_BLOCK_NUM / 1024));
(nftp_get_blocksz() * NFTP_BLOCK_NUM / 1024));
return NFTP_ERR_BLOCKS;
}
p->blocks = (uint16_t)blocks;
Expand Down Expand Up @@ -747,6 +748,19 @@ nftp_set_recvdir(char * dir)
return (0);
}

int
nftp_set_blocksz(uint32_t blksz)
{
blocksz = blksz;
return (0);
}

uint32_t
nftp_get_blocksz()
{
return blocksz;
}

int
test()
{
Expand Down

0 comments on commit 57afa5c

Please sign in to comment.