comparison main.c @ 662:0c94af18f55e

Two more generic functions were moved to th-libs, so factor out the deprecated ones from here and convert to the changed APIs.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Feb 2016 03:02:12 +0200
parents eb3692d85ac2
children 0b81e5fdc7b5
comparison
equal deleted inserted replaced
661:eb3692d85ac2 662:0c94af18f55e
4 * (C) Copyright 2008-2014 Tecnic Software productions (TNSP) 4 * (C) Copyright 2008-2014 Tecnic Software productions (TNSP)
5 */ 5 */
6 #include "th_args.h" 6 #include "th_args.h"
7 #include "th_config.h" 7 #include "th_config.h"
8 #include "th_network.h" 8 #include "th_network.h"
9 #include "th_file.h"
9 #include "util.h" 10 #include "util.h"
10 #include "ui.h" 11 #include "ui.h"
11 #include <unistd.h> 12 #include <unistd.h>
12 #include <fcntl.h> 13 #include <fcntl.h>
13 #ifdef TH_PLAT_WINDOWS 14 #ifdef TH_PLAT_WINDOWS
1933 nn_log_close(win); 1934 nn_log_close(win);
1934 return nn_log_open(win); 1935 return nn_log_open(win);
1935 } 1936 }
1936 1937
1937 1938
1938 BOOL nn_stat_path(const char *path, BOOL *isDirectory, BOOL *isWritable, BOOL *isReadable)
1939 {
1940 #ifdef TH_PLAT_WINDOWS
1941 DWORD attr = GetFileAttributes(path);
1942
1943 *isDirectory = (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
1944 *isWritable = (attr & FILE_ATTRIBUTE_READONLY) == 0;
1945 *isReadable = TRUE;
1946 #else
1947 uid_t id = geteuid();
1948 struct stat sb;
1949 if (stat(path, &sb) < 0)
1950 return FALSE;
1951
1952 *isDirectory = (S_ISDIR(sb.st_mode));
1953 *isWritable = (id == sb.st_uid && (sb.st_mode & S_IWUSR));
1954 *isReadable = (id == sb.st_uid && (sb.st_mode & S_IRUSR));
1955 #endif
1956
1957 // THERR("'%s': dir=%d, wr=%d, rd=%d\n", path, *isDirectory, *isWritable, *isReadable);
1958 return TRUE;
1959 }
1960
1961
1962 BOOL nn_mkdir_rec(const char *cpath)
1963 {
1964 char save, *path = th_strdup(cpath);
1965 size_t start = 0, end;
1966 BOOL res = FALSE, exists, isDir, isWritable, isReadable;
1967
1968 THMSG(0, "Creating directory %s\n", cpath);
1969 do
1970 {
1971 for (save = 0, end = start; path[end] != 0; end++)
1972 if (path[end] == SET_DIR_SEPARATOR)
1973 {
1974 save = path[end];
1975 path[end] = 0;
1976 break;
1977 }
1978
1979
1980 if (path[start] != 0)
1981 {
1982 exists = nn_stat_path(path, &isDir, &isWritable, &isReadable);
1983 if (exists && !isDir)
1984 goto error;
1985
1986 if (!exists)
1987 {
1988 #ifdef TH_PLAT_WINDOWS
1989 if (!CreateDirectory(path, NULL))
1990 goto error;
1991 #else
1992 if (mkdir(path, 0x1c9) < 0)
1993 goto error;
1994 #endif
1995 }
1996 }
1997
1998 path[end] = save;
1999 start = end + 1;
2000 } while (save != 0);
2001
2002 res = TRUE;
2003
2004 error:
2005 th_free(path);
2006 return res;
2007 }
2008
2009
2010
2011
2012 int main(int argc, char *argv[]) 1939 int main(int argc, char *argv[])
2013 { 1940 {
2014 char *tmpStr; 1941 char *tmpStr;
2015 int index, updateCount = 0, ret; 1942 int index, updateCount = 0, ret;
2016 BOOL argsOK, colorSet = FALSE; 1943 BOOL argsOK, colorSet = FALSE;
2150 setHomeDir, SET_DIR_SEPARATOR, SET_LOG_DIR, SET_DIR_SEPARATOR); 2077 setHomeDir, SET_DIR_SEPARATOR, SET_LOG_DIR, SET_DIR_SEPARATOR);
2151 } 2078 }
2152 2079
2153 if (optLogEnable) 2080 if (optLogEnable)
2154 { 2081 {
2155 BOOL isDir, isWritable, isReadable; 2082 int flags;
2156 if (nn_stat_path(optLogPath, &isDir, &isWritable, &isReadable)) 2083 if (th_stat_path(optLogPath, &flags))
2157 { 2084 {
2158 if (!isDir) 2085 if ((flags & TH_IS_DIR) == 0)
2159 { 2086 {
2160 THERR("The log file path '%s' is not a directory.\n", 2087 THERR("The log file path '%s' is not a directory.\n",
2161 optLogPath); 2088 optLogPath);
2162 goto err_exit; 2089 goto err_exit;
2163 } 2090 }
2164 else 2091 else
2165 if (!isWritable) 2092 if ((flags & TH_IS_WRITABLE) == 0)
2166 { 2093 {
2167 #ifdef TH_PLAT_WINDOWS 2094 #ifdef TH_PLAT_WINDOWS
2168 if (!nn_mkdir_rec(optLogPath)) 2095 if (!th_mkdir_path(optLogPath, 0))
2169 { 2096 {
2170 THERR("Could not create log file directory '%s'.\n", 2097 THERR("Could not create log file directory '%s'.\n",
2171 optLogPath); 2098 optLogPath);
2172 goto err_exit; 2099 goto err_exit;
2173 } 2100 }
2177 goto err_exit; 2104 goto err_exit;
2178 #endif 2105 #endif
2179 } 2106 }
2180 } 2107 }
2181 else 2108 else
2182 if (!nn_mkdir_rec(optLogPath)) 2109 if (!th_mkdir_path(optLogPath, 0))
2183 { 2110 {
2184 THERR("Could not create log file directory '%s'.\n", 2111 THERR("Could not create log file directory '%s'.\n",
2185 optLogPath); 2112 optLogPath);
2186 goto err_exit; 2113 goto err_exit;
2187 } 2114 }