Skip to content

Commit b630a97

Browse files
author
Ivan Stoev
committed
Create a folder with privs
1 parent 2a0d727 commit b630a97

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pbusinessaccount.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "papi.h"
2929
#include "plibs.h"
3030
#include "pnetlibs.h"
31+
#include "pfolder.h"
3132

3233
#include <stdio.h>
3334
typedef struct _email_vis_params {
@@ -672,4 +673,50 @@ void psync_update_cryptostatus(){
672673
psync_sql_free_result(q);
673674

674675

676+
}
677+
678+
static int check_write_permissions (psync_folderid_t folderid) {
679+
psync_sql_res* res;
680+
psync_uint_row row;
681+
int ret = 0;
682+
683+
res=psync_sql_query_nolock("SELECT permissions, flags, name FROM folder WHERE id=?");
684+
psync_sql_bind_uint(res, 1, folderid);
685+
row=psync_sql_fetch_rowint(res);
686+
if (unlikely(!row))
687+
debug(D_ERROR, "could not find folder of folderid %lu", (unsigned long)folderid);
688+
else if (((row[1])&3)!=O_RDONLY && !((row[0])&(PSYNC_PERM_MODIFY|PSYNC_PERM_CREATE)))
689+
ret = 1;
690+
691+
psync_sql_free_result(res);
692+
return ret;
693+
}
694+
695+
psync_folderid_t psync_check_and_create_folder (const char * path) {
696+
psync_folderid_t folderid=psync_get_folderid_by_path_or_create(path);
697+
if (folderid==PSYNC_INVALID_FOLDERID || (!check_write_permissions(folderid))){
698+
char *buff;
699+
uint32_t bufflen;
700+
int ind = 1;
701+
char *err;
702+
703+
while (ind < 100) {
704+
folderid=PSYNC_INVALID_FOLDERID;
705+
bufflen = strlen(path) + 1 /*zero char*/ + 3 /*parenthesis*/ + 3 /*up to 3 digit index*/;
706+
buff = (char *) psync_malloc(bufflen);
707+
snprintf(buff, bufflen - 1, "%s (%d)", path, ind);
708+
if (psync_create_remote_folder_by_path(buff, &err)!= 0) {
709+
folderid=psync_get_folderid_by_path_or_create(buff);
710+
if ((folderid != PSYNC_INVALID_FOLDERID)&&check_write_permissions(folderid)) {
711+
psync_free(buff);
712+
break;
713+
}
714+
} else
715+
debug(D_NOTICE,"Unable to create folder %s error is %s.", buff, err);
716+
++ind;
717+
psync_free(buff);
718+
}
719+
}
720+
721+
return folderid;
675722
}

psynclib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,9 @@ int psync_setlanguage(const char *language, char **err);
11781178
// Update crypto status information from userinfo.
11791179
void psync_update_cryptostatus();
11801180

1181+
// Checks and creates new folder with write permissions on it and adds suffix to the name if necessary i.e. New Folder (1) etc..
1182+
psync_folderid_t psync_check_and_create_folder (const char * path);
1183+
11811184
#ifdef __cplusplus
11821185
}
11831186
#endif

0 commit comments

Comments
 (0)