Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
makedirs: pass subsystem, add mstrict command, fixes #3459
  • Loading branch information
perexg committed Feb 8, 2016
1 parent 9ff6b9b commit 8eb4f40
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/config.c
Expand Up @@ -1443,7 +1443,7 @@ dobackup(const char *oldver)
}

snprintf(outfile, sizeof(outfile), "%s/backup", root);
if (makedirs(outfile, 0700, -1, -1))
if (makedirs("config", outfile, 0700, 1, -1, -1))
goto fatal;
if (chdir(root)) {
tvherror("config", "unable to find directory '%s'", root);
Expand Down Expand Up @@ -1647,7 +1647,7 @@ config_boot ( const char *path, gid_t gid, uid_t uid )
/* Ensure directory exists */
if (stat(path, &st)) {
config_newcfg = 1;
if (makedirs(path, 0700, gid, uid)) {
if (makedirs("config", path, 0700, 1, gid, uid)) {
tvhwarn("START", "failed to create settings directory %s,"
" settings will not be saved", path);
return;
Expand Down
3 changes: 2 additions & 1 deletion src/dvr/dvr_rec.c
Expand Up @@ -746,7 +746,8 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss)
dirsep = path + l;
}
htsstr_unescape_to(path, filename, sizeof(filename));
if (makedirs(filename, cfg->dvr_muxcnf.m_directory_permissions, -1, -1) != 0)
if (makedirs("dvr", filename,
cfg->dvr_muxcnf.m_directory_permissions, 0, -1, -1) != 0)
return -1;
max = pathconf(filename, _PC_NAME_MAX);
if (max < 8)
Expand Down
2 changes: 1 addition & 1 deletion src/settings.c
Expand Up @@ -82,7 +82,7 @@ hts_settings_makedirs ( const char *inpath )
}
x--;
}
return makedirs(path, 0700, -1, -1);
return makedirs("settings", path, 0700, 1, -1, -1);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/timeshift/timeshift_filemgr.c
Expand Up @@ -159,7 +159,7 @@ int timeshift_filemgr_makedirs ( int index, char *buf, size_t len )
if (timeshift_filemgr_get_root(buf, len))
return 1;
snprintf(buf+strlen(buf), len-strlen(buf), "/%d", index);
return makedirs(buf, 0700, -1, -1);
return makedirs("timeshift", buf, 0700, 0, -1, -1);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/tvheadend.h
Expand Up @@ -777,7 +777,7 @@ static inline uint8_t *sbuf_peek(sbuf_t *sb, int off) { return sb->sb_data + off

char *md5sum ( const char *str );

int makedirs ( const char *path, int mode, gid_t gid, uid_t uid );
int makedirs ( const char *subsys, const char *path, int mode, int mstrict, gid_t gid, uid_t uid );

int rmtree ( const char *path );

Expand Down
1 change: 1 addition & 0 deletions src/tvhlog.h
Expand Up @@ -128,6 +128,7 @@ static inline void tvhtrace_no_warnings(const char *fmt, ...) { (void)fmt; }
#define tvhwarn(...) tvhlog(LOG_WARNING, ##__VA_ARGS__)
#define tvhnotice(...) tvhlog(LOG_NOTICE, ##__VA_ARGS__)
#define tvherror(...) tvhlog(LOG_ERR, ##__VA_ARGS__)
#define tvhalert(...) tvhlog(LOG_ALERT, ##__VA_ARGS__)

time_t dispatch_clock_update(struct timespec *ts);

Expand Down
21 changes: 15 additions & 6 deletions src/utils.c
Expand Up @@ -484,7 +484,8 @@ md5sum ( const char *str )
#define FILE_MODE_BITS(x) (x&(S_IRWXU|S_IRWXG|S_IRWXO))

int
makedirs ( const char *inpath, int mode, gid_t gid, uid_t uid )
makedirs ( const char *subsys, const char *inpath, int mode,
int mstrict, gid_t gid, uid_t uid )
{
int err, ok;
size_t x;
Expand All @@ -506,17 +507,25 @@ makedirs ( const char *inpath, int mode, gid_t gid, uid_t uid )
if (!err && gid != -1 && uid != -1)
err = chown(path, uid, gid);
if (!err && !stat(path, &st) &&
FILE_MODE_BITS(mode) != FILE_MODE_BITS(st.st_mode))
FILE_MODE_BITS(mode) != FILE_MODE_BITS(st.st_mode)) {
err = chmod(path, mode); /* override umode */
tvhtrace("settings", "Creating directory \"%s\" with octal permissions "
"\"%o\" gid %d uid %d", path, mode, gid, uid);
if (!mstrict) {
err = 0;
tvhwarn(subsys, "Unable to change directory permissions "
"to \"%o\" for \"%s\" (keeping \"%o\")",
mode, path, FILE_MODE_BITS(st.st_mode));
mode = FILE_MODE_BITS(st.st_mode);
}
}
tvhtrace(subsys, "Creating directory \"%s\" with octal permissions "
"\"%o\" gid %d uid %d", path, mode, gid, uid);
} else {
err = S_ISDIR(st.st_mode) ? 0 : 1;
errno = ENOTDIR;
}
if (err) {
tvhlog(LOG_ALERT, "settings", "Unable to create dir \"%s\": %s",
path, strerror(errno));
tvhalert(subsys, "Unable to create dir \"%s\": %s",
path, strerror(errno));
return -1;
}
path[x] = '/';
Expand Down

0 comments on commit 8eb4f40

Please sign in to comment.