Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
5.0KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008-2020 Jonathan Moore Liles (as "Non-Session-Manager") */
  3. /* */
  4. /* This file is part of New-Session-Manager */
  5. /* */
  6. /* New-Session-Manager is free software: you can redistribute it and/or modify */
  7. /* it under the terms of the GNU General Public License as published by */
  8. /* the Free Software Foundation, either version 3 of the License, or */
  9. /* (at your option) any later version. */
  10. /* */
  11. /* New-Session-Manager is distributed in the hope that it will be useful, */
  12. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  13. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  14. /* GNU General Public License for more details. */
  15. /* */
  16. /* You should have received a copy of the GNU General Public License */
  17. /* along with New-Session-Manager. If not, see <https://www.gnu.org/licenses/>.*/
  18. /*******************************************************************************/
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <sys/vfs.h>
  27. unsigned long
  28. modification_time ( const char *file )
  29. {
  30. struct stat st;
  31. if ( stat( file, &st ) )
  32. return 0;
  33. return st.st_mtime;
  34. }
  35. /** returns /true/ if /file1/ is newer than /file2/ (or file2 doesn't exist) */
  36. bool
  37. newer ( const char *file1, const char *file2 )
  38. {
  39. return modification_time( file1 ) > modification_time( file2 );
  40. }
  41. unsigned long
  42. size ( const char *file )
  43. {
  44. struct stat st;
  45. if ( stat( file, &st ) )
  46. return 0;
  47. return st.st_size;
  48. }
  49. int
  50. exists ( const char *name )
  51. {
  52. struct stat st;
  53. return 0 == stat( name, &st );
  54. }
  55. bool
  56. acquire_lock ( int *lockfd, const char *filename )
  57. {
  58. struct flock fl;
  59. fl.l_type = F_WRLCK;
  60. fl.l_whence = SEEK_SET;
  61. fl.l_start = 0;
  62. fl.l_len = 0;
  63. *lockfd = ::creat( filename, 0777 );
  64. if ( fcntl( *lockfd, F_SETLK, &fl ) != 0 )
  65. return false;
  66. return true;
  67. }
  68. void
  69. release_lock ( int *lockfd, const char *filename )
  70. {
  71. unlink( filename );
  72. ::close( *lockfd );
  73. *lockfd = 0;
  74. }
  75. int
  76. backwards_fgetc ( FILE *fp )
  77. {
  78. int c;
  79. if ( fseek( fp, -1, SEEK_CUR ) != 0 )
  80. return -1;
  81. c = fgetc( fp );
  82. fseek( fp, -1, SEEK_CUR );
  83. return c;
  84. }
  85. char *
  86. backwards_afgets ( FILE *fp )
  87. {
  88. size_t size = 0;
  89. char *s = NULL;
  90. size_t i = 0;
  91. int c;
  92. while ( ( c = backwards_fgetc( fp ) ) >= 0 )
  93. {
  94. if ( i > 0 && '\n' == c )
  95. break;
  96. if ( i >= size )
  97. {
  98. size += 256;
  99. s = (char*)realloc( s, size );
  100. }
  101. s[i++] = c;
  102. }
  103. if ( s )
  104. {
  105. s[i] = 0;
  106. int len = strlen(s) ;
  107. int c, i, j;
  108. for (i = 0, j = len - 1; i < j; i++, j--)
  109. {
  110. c = s[i];
  111. s[i] = s[j];
  112. s[j] = c;
  113. }
  114. }
  115. fseek( fp, 1, SEEK_CUR );
  116. return s;
  117. }
  118. /** update the modification time of file referred to by /fd/ */
  119. void
  120. touch ( int fd )
  121. {
  122. struct stat st;
  123. fstat( fd, &st );
  124. fchmod( fd, st.st_mode );
  125. }
  126. /** write a single string to a file */
  127. void
  128. write_line ( const char *dir, const char *name, const char *value )
  129. {
  130. char path[512];
  131. snprintf( path, sizeof( path ), "%s/%s", dir, name );
  132. FILE *fp = fopen( path, "w" );
  133. if ( ! fp )
  134. return;
  135. fputs( value, fp );
  136. fclose( fp );
  137. }
  138. /** write a single string to a file */
  139. char *
  140. read_line ( const char *dir, const char *name )
  141. {
  142. char path[512];
  143. snprintf( path, sizeof( path ), "%s/%s", dir, name );
  144. FILE *fp = fopen( path, "r" );
  145. if ( ! fp )
  146. return 0;
  147. char *value = (char*)malloc( 512 );
  148. value[0] = 0;
  149. fgets( value, 512, fp );
  150. fclose( fp );
  151. return value;
  152. }
  153. #include <sys/statvfs.h>
  154. /** return the number of blocks free on filesystem containing file named /file/ */
  155. fsblkcnt_t
  156. free_space ( const char *file )
  157. {
  158. struct statfs st;
  159. memset( &st, 0, sizeof( st ) );
  160. statfs( file, &st );
  161. return st.f_bavail;
  162. }
  163. /** return the total number of blocks on filesystem containing file named /file/ */
  164. fsblkcnt_t
  165. total_space ( const char *file )
  166. {
  167. struct statfs st;
  168. memset( &st, 0, sizeof( st ) );
  169. statfs( file, &st );
  170. return st.f_blocks;
  171. }
  172. /** return the percentage of usage on filesystem containing file named /file/ */
  173. int
  174. percent_used ( const char *file )
  175. {
  176. const double ts = total_space( file );
  177. const double fs = free_space( file );
  178. double percent_free = ( ( fs / ts ) * 100.0f );
  179. return (int) (100.0f - percent_free);
  180. }