]> granicus.if.org Git - neomutt/commitdiff
Add chmod() wrappers to library
authorReis Radomil <reisradomil@fake-box.com>
Sat, 18 Nov 2017 08:40:25 +0000 (09:40 +0100)
committerRichard Russon <rich@flatcap.org>
Mon, 20 Nov 2017 12:40:51 +0000 (12:40 +0000)
Add functions for changing/adding/removing permissions of files.

mutt_file_chmod          - set permissions
mutt_file_chmod_add      - add permissions
mutt_file_chmod_add_stat - add permissions
mutt_file_chmod_rm       - remove permissions
mutt_file_chmod_rm_stat  - remove permissions

The *_stat functions accept an additional optional stat structure
parameter, which must point to the stat structure of the file.
Using the *_stat functions can save a call to stat() as the stat
structure does not need to be queried by the mutt_file_chmod_* function.

mutt/file.c
mutt/file.h

index 74ccdcd359a2c10ff93c2216adbff30a849ed6f2..0b98c1729ce3fb200af2c68ef335ddb71dbbcff7 100644 (file)
  * | :---------------------------- | :-----------------------------------------------------------
  * | mutt_file_basename()          | Find the last component for a pathname
  * | mutt_file_check_empty()       | Is the mailbox empty
+ * | mutt_file_chmod()             | Set permissions of a file
+ * | mutt_file_chmod_add()         | Add permissions to a file
+ * | mutt_file_chmod_add_stat()    | Add permissions to a file
+ * | mutt_file_chmod_rm()          | Remove permissions from a file
+ * | mutt_file_chmod_rm_stat()     | Remove permissions from a file
  * | mutt_file_concatn_path()      | Concatenate directory and filename
  * | mutt_file_concat_path()       | Join a directory name and a filename
  * | mutt_file_copy_bytes()        | Copy some content from one file to another
@@ -972,6 +977,124 @@ void mutt_file_touch_atime(int f)
 #endif
 }
 
+/**
+ * mutt_file_chmod - change permissions of a file
+ * @param path Filename
+ * @param mode the permissions to set
+ * @retval int same as chmod(2)
+ *
+ * This is essentially chmod(path, mode), see chmod(2).
+ */
+int mutt_file_chmod(const char *path, mode_t mode)
+{
+  return chmod(path, mode);
+}
+
+/**
+ * mutt_file_chmod_add - add permissions to a file
+ * @param path Filename
+ * @param mode the permissions to add
+ * @retval int same as chmod(2)
+ * @see   mutt_file_chmod_add_stat()
+ *
+ * Adds the given permissions to the file. Permissions not mentioned in mode
+ * will stay as they are. This function resembles the `chmod ugoa+rwxXst`
+ * command family. Example:
+ *
+ *     mutt_file_chmod_add(path, S_IWUSR | S_IWGRP | S_IWOTH);
+ *
+ * will add write permissions to path but does not alter read and other
+ * permissions.
+ */
+int mutt_file_chmod_add(const char *path, mode_t mode)
+{
+  return mutt_file_chmod_add_stat(path, mode, NULL);
+}
+
+/**
+ * mutt_file_chmod_add_stat - add permissions to a file
+ * @param path Filename
+ * @param mode the permissions to add
+ * @param st   struct stat for the file (optional)
+ * @retval int same as chmod(2)
+ * @see   mutt_file_chmod_add()
+ *
+ * Same as mutt_file_chmod_add() but saves a system call to stat() if a
+ * non-NULL stat structure is given. Useful if the stat structure of the file
+ * was retrieved before by the calling code. Example:
+ *
+ *     struct stat st;
+ *     stat(path, &st);
+ *     // ... do something else with st ...
+ *     mutt_file_chmod_add_stat(path, S_IWUSR | S_IWGRP | S_IWOTH, st);
+ *
+ */
+int mutt_file_chmod_add_stat(const char *path, mode_t mode, struct stat *st)
+{
+  struct stat _st;
+
+  if (!st)
+  {
+    if (stat(path, &_st) == -1)
+      return -1;
+    st = &_st;
+  }
+  return chmod(path, st->st_mode | mode);
+}
+
+
+/**
+ * mutt_file_chmod_rm - remove permissions from a file
+ * @param path Filename
+ * @param mode the permissions to remove
+ * @retval int same as chmod(2)
+ * @see   mutt_file_chmod_rm_stat()
+ *
+ * Removes the given permissions from the file. Permissions not mentioned in
+ * mode will stay as they are. This function resembles the `chmod ugoa-rwxXst`
+ * command family. Example:
+ *
+ *     mutt_file_chmod_rm(path, S_IWUSR | S_IWGRP | S_IWOTH);
+ *
+ * will remove write permissions from path but does not alter read and other
+ * permissions.
+ */
+int mutt_file_chmod_rm(const char *path, mode_t mode)
+{
+  return mutt_file_chmod_rm_stat(path, mode, NULL);
+}
+
+/**
+ * mutt_file_chmod_rm_stat - remove permissions from a file
+ * @param path Filename
+ * @param mode the permissions to remove
+ * @param st   struct stat for the file (optional)
+ * @retval int same as chmod(2)
+ * @see   mutt_file_chmod_rm()
+ *
+ * Same as mutt_file_chmod_rm() but saves a system call to stat() if a non-NULL
+ * stat structure is given. Useful if the stat structure of the file was
+ * retrieved before by the calling code. Example:
+ *
+ *     struct stat st;
+ *     stat(path, &st);
+ *     // ... do something else with st ...
+ *     mutt_file_chmod_rm_stat(path, S_IWUSR | S_IWGRP | S_IWOTH, st);
+ *
+ */
+int mutt_file_chmod_rm_stat(const char *path, mode_t mode, struct stat *st)
+{
+  struct stat _st;
+
+  if (!st)
+  {
+    if (stat(path, &_st) == -1)
+      return -1;
+    st = &_st;
+  }
+  return chmod(path, st->st_mode & ~mode);
+}
+
 /**
  * mutt_file_lock - (try to) lock a file
  * @param path    Path to file
index a79b39420d6704a0088132033c623bfa2fa744c2..bec2dd382461264e3b9b05a85a241fa27aa3ba7c 100644 (file)
@@ -35,6 +35,11 @@ struct stat;
 
 const char *mutt_file_basename(const char *f);
 int         mutt_file_check_empty(const char *path);
+int         mutt_file_chmod(const char *path, mode_t mode);
+int         mutt_file_chmod_add(const char *path, mode_t mode);
+int         mutt_file_chmod_add_stat(const char *path, mode_t mode, struct stat *st);
+int         mutt_file_chmod_rm(const char *path, mode_t mode);
+int         mutt_file_chmod_rm_stat(const char *path, mode_t mode, struct stat *st);
 char *      mutt_file_concatn_path(char *dst, size_t dstlen, const char *dir, size_t dirlen, const char *fname, size_t fnamelen);
 char *      mutt_file_concat_path(char *d, const char *dir, const char *fname, size_t l);
 int         mutt_file_copy_bytes(FILE *in, FILE *out, size_t size);