comparison main.c @ 550:504a8b9297a3

Fix log directory creation.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 12 Nov 2012 14:30:59 +0200
parents dd7b58eca06d
children 89fafb218396
comparison
equal deleted inserted replaced
549:dd7b58eca06d 550:504a8b9297a3
1577 th_free(win->logFilename); 1577 th_free(win->logFilename);
1578 win->logFilename = NULL; 1578 win->logFilename = NULL;
1579 } 1579 }
1580 1580
1581 1581
1582 BOOL nn_mkdir_rec(const char *cpath)
1583 {
1584 char save, *path = th_strdup(cpath);
1585 size_t start = 0, end;
1586 BOOL res = FALSE;
1587
1588 do
1589 {
1590 for (save = 0, end = start; path[end] != 0; end++)
1591 if (path[end] == SET_DIR_SEPARATOR)
1592 {
1593 save = path[end];
1594 path[end] = 0;
1595 break;
1596 }
1597
1598 if (path[start] != 0)
1599 {
1600 #ifdef __WIN32
1601 if (!CreateDirectory(path, NULL))
1602 goto error;
1603 #else
1604 if (mkdir(path, 711) < 0)
1605 goto error;
1606 #endif
1607 }
1608
1609 path[end] = save;
1610 start = end + 1;
1611 } while (save != 0);
1612
1613 res = TRUE;
1614
1615 error:
1616 th_free(path);
1617 return res;
1618 }
1619
1620
1621 BOOL nn_stat_path(const char *path, BOOL *isDirectory, BOOL *isWritable, BOOL *isReadable) 1582 BOOL nn_stat_path(const char *path, BOOL *isDirectory, BOOL *isWritable, BOOL *isReadable)
1622 { 1583 {
1623 #ifdef __WIN32 1584 #ifdef __WIN32
1624 DWORD attr = GetFileAttributes(path); 1585 DWORD attr = GetFileAttributes(path);
1625 1586
1636 *isWritable = (id == sb.st_uid && (sb.st_mode & S_IWUSR)); 1597 *isWritable = (id == sb.st_uid && (sb.st_mode & S_IWUSR));
1637 *isReadable = (id == sb.st_uid && (sb.st_mode & S_IRUSR)); 1598 *isReadable = (id == sb.st_uid && (sb.st_mode & S_IRUSR));
1638 #endif 1599 #endif
1639 return TRUE; 1600 return TRUE;
1640 } 1601 }
1602
1603
1604 BOOL nn_mkdir_rec(const char *cpath)
1605 {
1606 char save, *path = th_strdup(cpath);
1607 size_t start = 0, end;
1608 BOOL res = FALSE, exists, isDir, isWritable, isReadable;
1609
1610 do
1611 {
1612 for (save = 0, end = start; path[end] != 0; end++)
1613 if (path[end] == SET_DIR_SEPARATOR)
1614 {
1615 save = path[end];
1616 path[end] = 0;
1617 break;
1618 }
1619
1620
1621 if (path[start] != 0)
1622 {
1623 exists = nn_stat_path(path, &isDir, &isWritable, &isReadable);
1624 if (exists && !isDir)
1625 goto error;
1626
1627 if (!exists)
1628 {
1629 #ifdef __WIN32
1630 if (!CreateDirectory(path, NULL))
1631 goto error;
1632 #else
1633 if (mkdir(path, 0x1c9) < 0)
1634 goto error;
1635 #endif
1636 }
1637 }
1638
1639 path[end] = save;
1640 start = end + 1;
1641 } while (save != 0);
1642
1643 res = TRUE;
1644
1645 error:
1646 th_free(path);
1647 return res;
1648 }
1649
1650
1641 1651
1642 1652
1643 int main(int argc, char *argv[]) 1653 int main(int argc, char *argv[])
1644 { 1654 {
1645 char *tmpStr; 1655 char *tmpStr;
1767 { 1777 {
1768 optLogPath = th_strdup_printf("%s%c%s%c", 1778 optLogPath = th_strdup_printf("%s%c%s%c",
1769 setHomeDir, SET_DIR_SEPARATOR, SET_LOG_DIR, SET_DIR_SEPARATOR); 1779 setHomeDir, SET_DIR_SEPARATOR, SET_LOG_DIR, SET_DIR_SEPARATOR);
1770 } 1780 }
1771 1781
1772 // Parse command line arguments
1773 argsOK = th_args_process(argc, argv, optList, optListN,
1774 argHandleOpt, argHandleFile, FALSE);
1775
1776 if (optUserNameCmd != NULL)
1777 {
1778 THMSG(1, "Username set on commandline.\n");
1779 optUserName = optUserNameCmd;
1780 optPassword = optPasswordCmd;
1781 }
1782
1783 if (!argsOK)
1784 return -2;
1785
1786 // Allocate userhash
1787 if ((nnUsers = nn_userhash_new()) == NULL)
1788 {
1789 THERR("Could not allocate userhash. Fatal error.\n");
1790 return -105;
1791 }
1792
1793 // If no idle messages are set, add default
1794 if (setIdleMessages == NULL)
1795 {
1796 th_llist_append(&setIdleMessages, th_strdup("."));
1797 }
1798
1799 // Initialize network
1800 if (!nn_network_init())
1801 {
1802 THERR("Could not initialize network subsystem.\n");
1803 goto err_exit;
1804 }
1805
1806 // Initialize curses windowing
1807 if (!optDaemon && !nnwin_init(SET_DELAY))
1808 goto err_exit;
1809
1810 { 1782 {
1811 BOOL isDir, isWritable, isReadable; 1783 BOOL isDir, isWritable, isReadable;
1812 if (nn_stat_path(optLogPath, &isDir, &isWritable, &isReadable)) 1784 if (nn_stat_path(optLogPath, &isDir, &isWritable, &isReadable))
1813 { 1785 {
1814 if (!isDir) 1786 if (!isDir)
1815 { 1787 {
1816 errorMsg("The log file path '%s' is not a directory.\n", 1788 THERR("The log file path '%s' is not a directory.\n",
1817 optLogPath); 1789 optLogPath);
1818 goto err_exit; 1790 goto err_exit;
1819 } 1791 }
1820 else 1792 else
1821 if (!isWritable) 1793 if (!isWritable)
1822 { 1794 {
1823 errorMsg("The log file path '%s' is not writable.\n", 1795 THERR("The log file path '%s' is not writable.\n",
1824 optLogPath); 1796 optLogPath);
1825 goto err_exit; 1797 goto err_exit;
1826 } 1798 }
1827 } 1799 }
1828 else 1800 else
1829 if (!nn_mkdir_rec(optLogPath)) 1801 if (!nn_mkdir_rec(optLogPath))
1830 { 1802 {
1831 errorMsg("Could not create log file directory '%s'.\n", 1803 THERR("Could not create log file directory '%s'.\n",
1832 optLogPath); 1804 optLogPath);
1833 goto err_exit; 1805 goto err_exit;
1834 } 1806 }
1835 } 1807 }
1808
1809 // Parse command line arguments
1810 argsOK = th_args_process(argc, argv, optList, optListN,
1811 argHandleOpt, argHandleFile, FALSE);
1812
1813 if (optUserNameCmd != NULL)
1814 {
1815 THMSG(1, "Username set on commandline.\n");
1816 optUserName = optUserNameCmd;
1817 optPassword = optPasswordCmd;
1818 }
1819
1820 if (!argsOK)
1821 return -2;
1822
1823 // Allocate userhash
1824 if ((nnUsers = nn_userhash_new()) == NULL)
1825 {
1826 THERR("Could not allocate userhash. Fatal error.\n");
1827 return -105;
1828 }
1829
1830 // If no idle messages are set, add default
1831 if (setIdleMessages == NULL)
1832 {
1833 th_llist_append(&setIdleMessages, th_strdup("."));
1834 }
1835
1836 // Initialize network
1837 if (!nn_network_init())
1838 {
1839 THERR("Could not initialize network subsystem.\n");
1840 goto err_exit;
1841 }
1842
1843 // Initialize curses windowing
1844 if (!optDaemon && !nnwin_init(SET_DELAY))
1845 goto err_exit;
1846
1836 1847
1837 if (cursesInit) 1848 if (cursesInit)
1838 { 1849 {
1839 printMsg(NULL, "%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_desc); 1850 printMsg(NULL, "%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_desc);
1840 printMsg(NULL, "%s\n", th_prog_author); 1851 printMsg(NULL, "%s\n", th_prog_author);