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.

220 lines
4.7KB

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